Results 1 to 4 of 4

Thread: [RESOLVED] Finding names using LIKE

  1. #1

    Thread Starter
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180

    [RESOLVED] Finding names using LIKE

    Hi,

    I'm using this code to list all names from a table into a listbox:
    VB Code:
    1. Dim path As String
    2. Dim strQuery As String
    3. Dim i As Integer
    4. Dim r As Integer
    5. path = App.path & "\persons.mdb"
    6. Set db = OpenDatabase(path)
    7. strQuery = "SELECT * FROM Names where Name Like '" & txtSearch.Text & "*'"
    8. Set rs = db.OpenRecordset(strQuery, dbOpenDynaset)
    9. r = rs.RecordCount
    10. If r <> 0 Then
    11. List1.Clear
    12. For i = 0 To r
    13. List1.AddItem rs.Fields("Name").Value
    14. rs.MoveNext
    15. Next
    16. End If
    17. rs.Close
    18. End Sub
    The code above is run in the change event of my search textbox.
    Say I have two usernames in the table: "Pete" and "Peter".
    It works fine if I type like "Pe" or "Pet" or "Pete", they are listed in the listbox. But as soon as I type "Peter" I get the error "No current Record".
    What am I doing wrong?
    Last edited by Michael_Kamen; Oct 13th, 2002 at 12:50 PM.

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    the problem is probably the recordcount.

    it is not correct unless you do a movelast movefirst before counting... MS does it again

    VB Code:
    1. rs.MoveLast
    2.         rs.MoveFirst

    after that, the recordcount will be correct.
    -= a peet post =-

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    this is how I would do it

    VB Code:
    1. Set db = OpenDatabase(path)
    2.     strQuery = "SELECT * FROM Names where Name Like '" & txtSearch.Text & "*'"
    3.    
    4.     Set rs = db.OpenRecordset(strQuery, dbOpenDynaset)
    5.     If rs.RecordCount > 0 Then
    6.         List1.Clear        
    7.         While Not rs.EOF
    8.             List1.AddItem rs.Fields("Name").Value
    9.             rs.MoveNext
    10.         Wend
    11.     End If
    12.     rs.Close

    this way I do not have to rely on the recordcount
    -= a peet post =-

  4. #4

    Thread Starter
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180

    Thumbs up That does the trick

    Thanx peet

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width