|
-
Oct 13th, 2002, 08:12 AM
#1
Thread Starter
Banned
[RESOLVED] Finding names using LIKE
Hi,
I'm using this code to list all names from a table into a listbox:
VB Code:
Dim path As String
Dim strQuery As String
Dim i As Integer
Dim r As Integer
path = App.path & "\persons.mdb"
Set db = OpenDatabase(path)
strQuery = "SELECT * FROM Names where Name Like '" & txtSearch.Text & "*'"
Set rs = db.OpenRecordset(strQuery, dbOpenDynaset)
r = rs.RecordCount
If r <> 0 Then
List1.Clear
For i = 0 To r
List1.AddItem rs.Fields("Name").Value
rs.MoveNext
Next
End If
rs.Close
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.
-
Oct 13th, 2002, 08:48 AM
#2
-
Oct 13th, 2002, 08:49 AM
#3
-= B u g S l a y e r =-
this is how I would do it
VB Code:
Set db = OpenDatabase(path)
strQuery = "SELECT * FROM Names where Name Like '" & txtSearch.Text & "*'"
Set rs = db.OpenRecordset(strQuery, dbOpenDynaset)
If rs.RecordCount > 0 Then
List1.Clear
While Not rs.EOF
List1.AddItem rs.Fields("Name").Value
rs.MoveNext
Wend
End If
rs.Close
this way I do not have to rely on the recordcount
-
Oct 13th, 2002, 09:39 AM
#4
Thread Starter
Banned
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|