Using Like in SQL Statement
Hi,
I am trying to use the like statement in an SQL statement but I am unable to!
This works without the "Like" command:
da.SelectCommand = New OleDbCommand("Select * from Customers Where Name= '" & Me.txtSearch.Text & "'", ocon)
This is what I am trying to get to work:
da.SelectCommand = New OleDbCommand("Select * from Customers Where Name= Like '" & Me.txtSearch.Text & "*" & "'", ocon)
Any Idea's
Darrell....
Thanks for feedback but cant get it to work
Thanks for your feedback but I cannot get it to work?
You will see below the original code that works and I have coded out the part that does not work.
Here is the full code:
Public Sub OpenSearch()
On Error Resume Next
Dim frm As New frmCustDet()
With frm
'Here we are going to Open the Customer Recordset for the Item
Dim ocon As OleDbConnection = New OleDbConnection(gc_Connect)
ocon.Open()
Dim ds As New DataSet()
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
'This part works fine
da.SelectCommand = New OleDbCommand("Select * from Customers Where Name= '" & Me.txtSearch.Text & "'", ocon)
'This part I cannot get to work
'da.SelectCommand = New OleDbCommand("Select * from Customers Where Name= Like '%" & Me.txtSearch.Text & "*" & "%'", ocon)
da.Fill(ds)
Dim dr As DataRow
'Now we have the Record - Hopefully
If ds.Tables(0).Rows.Count > 0 Then
dr = ds.Tables(0).Rows(0)
.lngPkid = dr("PKID")
.txtName.Text = dr("Name") & ""
.txtStreet.Text = dr("Street") & ""
.txtArea.Text = dr("Area") & ""
.txtTown.Text = dr("Town") & ""
.txtCounty.Text = dr("County") & ""
.txtPostCode.Text = dr("PostCode") & ""
.txtTelNo.Text = dr("TelNo") & ""
.txtDateAdded.Text = dr("DateAdded") & ""
.ChkActive.Checked = dr("ChkActive") & ""
End If
dr = Nothing
da.Dispose()
ds.Dispose()
ocon.Close()
.ShowDialog(Me)
'Me.RefreshCustomers()
End With
End Sub
Many Thanks
Darrell...
Works Great - One Other Question
Hi
Thanks for that, it now works. I have also changed the field name as you recommended.
Can you help with one other thing?
If the search brings up more than 1 customer with the same name the VB form will only show the first customer. In MS Access when you run the search if there is more than one customer returned you are able to scroll between records with first, next, last and add new buttons at the bottom of the form. Can this be done in VB.NET also?
Many Thanks
Darrell...