Results 1 to 8 of 8
  1. #1
    shod90 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    134

    loop through table

    Dear Gents,
    Um facing a situation that i need to check if the serialNumber i entered on the form is available in the serialsTables or no , If exist then continue code if not then prompt me a msg "This serial not valid for this Itemcode"


    Here is the code but i think there will be bunch of updates on it to work properly.
    Code:
    Dim strSQL As StringDim rs As DAO.Recordset
    strSQL = "Serials"
    Set rs = CurrentDb.OpenRecordset(strSQL)
    With rs
        If Not .BOF And Not .EOF Then
            .MoveLast
            .MoveFirst
            While (Not .EOF)
            If Combo450 = DLookup("SerialNumber", "Serials", "ItemID = '" & [Form_Add an Order and Details].Child12.Form!Code & "'") Then
            Else
            End If
            .MoveNext
            Wend
        End If
        .Close
        End With

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    Quote Originally Posted by shod90 View Post
    i need to check if the serialNumber i entered on the form is available in the serialsTables or no
    Why not use a combo box?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    shod90 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    134
    Because i have a lot of serial numbers i don't want to chose from the list , I need to scan the serial and go to another field

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    You could just use a DCount and if not 0 than do your message. A loop may be overkill for the task.

    In your code you are opening a recordset but not using it.

    to do it with a loop you would do something like this:
    Code:
    Sub TestSerial(Var As String)  ' Var being the value you want to test
    
    
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim strSql As String
    
    
        strSql = "select SerialNumber from  tblSerials"
    
    
        Set db = CurrentDb()
        Set rs = db.OpenRecordset(strSql)
    
    
        If rs.BOF And rs.EOF Then
            GoTo MyExit
        End If
    
        Do Until rs.EOF
    
            If rs!SerialNumber = Var Then
                MsgBox "Some message"
                Exit Do
            End If
    
            rs.MoveNext
        Loop
    
    
    MyExit:
        rs.Close
        Set rs = Nothing
        Set db = Nothing
    End Sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Isn't the recordset and loop overkill? Why not just
    Code:
    If DCount("SerialNumber", "Serials", "ItemID = '" & [Form_Add an Order and Details].Child12.Form!Code & "'") > 0 Then
      msgbox "This serial not valid for this Itemcode"
      'Do something?
    End If
    Last edited by Micron; 11-12-2020 at 03:15 PM.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    @Micron- Wouldn't >1 mean the msgbox wouldn't fire until the dcount was 2?

    And I agree that dcount is the way to go especially if its within another block of code.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Wouldn't >1 mean the msgbox wouldn't fire until the dcount was 2?
    Just testing to see if anyone was paying attention!
    Edited now.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 03-07-2017, 02:49 PM
  2. Replies: 12
    Last Post: 06-05-2015, 04:27 PM
  3. Replies: 9
    Last Post: 05-08-2015, 02:36 PM
  4. Replies: 17
    Last Post: 04-07-2014, 07:48 PM
  5. Replies: 3
    Last Post: 03-10-2013, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums