I've got a database with a table [AddressBook] with fields like [FirstName], [LastName], [Address], [PhoneNo], etc. And I've built a form with a textbox [txtSearch], a command button that runs the code below, and a listbox [lstSearchResults] where the results should appear. There's more to it than all this but (I hope) I've included all the pertinent information. I'm trying to make it so that if a user clicks on the button while the search field is blank the listbox displays all the records in the table (for now; that's just step one). If they enter some text first, I want it to catch all the matches and display them in the listbox and I want the user to be able to enter partial names and still get matches. It seems, however, that my asterisks are not being treated as wildcards. All the recordsets return a count of zero. What do I need to do to make this work?
Code:
Dim RecSet As New ADODB.Recordset
Dim strSearchString As String
If IsNull([txtSearch]) Or [txtSearch] = "" Then
strSearchString = "'*'"
Else
strSearchString = "'*" & [txtSearch] & "*'"
End If
RecSet.Open "SELECT * FROM [AddressBook] WHERE [LastName] LIKE " & strSearchString & " ORDER BY [LastName];", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
RecSet.MoveFirst
Do While Not RecSet.EOF
[lstSearchResults].AddItem RecSet("First Name") & " " & RecSet("Last Name")
RecSet.MoveNext
Loop
Let me know if anything needs clarification.
Thanks.
Last edited by a moderator: