Results 1 to 2 of 2
  1. #1
    cwf is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2008
    Location
    SouthEast
    Posts
    9

    Looping through a tbl to find a certain value

    I rum XP and Access 2007

    Private Sub Command0_Click()

    'this code should open the Contacts tbl and search the LastName field till it finds a match from the if statement there are no controls on the form that use the contacts table or any of its fields.

    Dim RS As ADODB.Recordset
    Set RS = New ADODB.Recordset
    RS.ActiveConnection = CurrentProject.Connection
    RS.Source = "tblContacts"
    RS.CursorType = adOpenDynamic
    RS.LockType = adLockOptimistic

    RS.Open

    'Do While Not RS.EOF

    If tblContacts.LastName = "brown" Then THIS REFERENCE DOESN"T WORK

    MsgBox "The Last Name is " & tblContacts.LastName


    Else
    Msgbox "Your search found no match!"
    End If

    RS.MoveNext 'This does not move the recordset? Why?
    'Loop

    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,524
    The direct answer to your question is that it needs to be this:

    If RS!LastName = "brown" Then

    That said, that's a horribly inefficient way to find out if there's a matching record. I'd open the recordset on an SQL string that included a criteria (in your case, "SELECT * FROM tblContacts WHERE LastName = 'Brown'"). Then test for EOF; if True, there were no records.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Need help to find a function
    By lil in forum Access
    Replies: 7
    Last Post: 03-24-2010, 12:14 PM
  2. Looping code for printing reports
    By Lockrin in forum Access
    Replies: 2
    Last Post: 02-09-2010, 05:48 AM
  3. Find tables
    By Jaime in forum Access
    Replies: 2
    Last Post: 11-04-2009, 01:52 PM
  4. Looping syntax
    By fadiaccess in forum Access
    Replies: 1
    Last Post: 10-23-2009, 02:57 AM
  5. How to Find a Record
    By lschlitt in forum Programming
    Replies: 2
    Last Post: 09-09-2006, 06:24 PM

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