I'm struggling to set up an ado search using the wildcard (*) character. It's an Access 2000 database, and I'm executing a command to return a recordset. The parameters to open the recordset are in a function. The user should be able enter a partial product name into a text box, hit return and a listview is populated w/ the results. I've managed to search for identical matches, but even w/ * I can't create a recordset for partials.
I've changed the commandtext every way I can think of, w/ no luck. If anyone can tell me what's wrong w/ my commandtext, it would be monumentally appreciated! Before I run screaming into the night!
Public Function OpenrsProducts()
Set rsProducts = New ADODB.Recordset
rsProducts.CursorType = adOpenStatic
rsProducts.CursorLocation = adUseClient
rsProducts.LockType = adLockPessimistic
rsProducts.Source = "Select * From Products"
rsProducts.ActiveConnection = cnApollo
rsProducts.Open
rsProducts.MoveFirst
End Function
Private Sub txtProd1_LostFocus()
Product5 = txtProd1.Text
Call OpenrsProducts
Set cmd = New ADODB.Command
cmd.ActiveConnection = cnApollo
cmd.CommandText = "Select * from Products Where ProductName like '" & "*" & Product5 & "*" & "'"
Set rsProducts = cmd.Execute
If rsProducts.EOF = True Or rsProducts.BOF = True Then
MsgBox "There were no matches for the given search criteria.", vbOKOnly & _
vbInformation, "No Matches"
Exit Sub
Else
rsProducts.MoveFirst
For ProdIndex5 = 1 To rsProducts.RecordCount
Set lvwListItem0 = lvwProd1.ListItems.Add()
lvwListItem0.Text = ""
lvwListItem0.SubItems(1) = rsProducts!ProductName
lvwListItem0.SubItems(2) = Format(rsProducts!UnitPrice, "currency"
lvwListItem0.SubItems(3) = rsProducts!UnitsInStock
rsProducts.MoveNext
Next ProdIndex5
End If
End Sub
I've changed the commandtext every way I can think of, w/ no luck. If anyone can tell me what's wrong w/ my commandtext, it would be monumentally appreciated! Before I run screaming into the night!
Public Function OpenrsProducts()
Set rsProducts = New ADODB.Recordset
rsProducts.CursorType = adOpenStatic
rsProducts.CursorLocation = adUseClient
rsProducts.LockType = adLockPessimistic
rsProducts.Source = "Select * From Products"
rsProducts.ActiveConnection = cnApollo
rsProducts.Open
rsProducts.MoveFirst
End Function
Private Sub txtProd1_LostFocus()
Product5 = txtProd1.Text
Call OpenrsProducts
Set cmd = New ADODB.Command
cmd.ActiveConnection = cnApollo
cmd.CommandText = "Select * from Products Where ProductName like '" & "*" & Product5 & "*" & "'"
Set rsProducts = cmd.Execute
If rsProducts.EOF = True Or rsProducts.BOF = True Then
MsgBox "There were no matches for the given search criteria.", vbOKOnly & _
vbInformation, "No Matches"
Exit Sub
Else
rsProducts.MoveFirst
For ProdIndex5 = 1 To rsProducts.RecordCount
Set lvwListItem0 = lvwProd1.ListItems.Add()
lvwListItem0.Text = ""
lvwListItem0.SubItems(1) = rsProducts!ProductName
lvwListItem0.SubItems(2) = Format(rsProducts!UnitPrice, "currency"
lvwListItem0.SubItems(3) = rsProducts!UnitsInStock
rsProducts.MoveNext
Next ProdIndex5
End If
End Sub