Connecting Tech Pros Worldwide Forums | Help | Site Map

how does LIKE operator work?

Newbie
 
Join Date: Nov 2006
Posts: 23
#1: Apr 29 '07
I want to write sql sentence with LIKE operator. I wrote this code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command8_Click()
  2. List1.Clear
  3. Set db = OpenDatabase("the path of my database")
  4. Set rec = db.OpenRecordset("SELECT * From asmenu_info WHERE asmenu_info.pavarde LIKE '%Pet%';")
  5. While Not rec.EOF
  6. List1.AddItem rec!ID & "  " & rec!Vardas & "   " & rec!pavarde & "  " & rec!Data
  7. rec.MoveNext
  8. Wend
  9. rec.Close
  10. db.Close
  11. End Sub
and my program does nothing. I don't undestand how does LIKE work?

markmcgookin's Avatar
Moderator
 
Join Date: Dec 2006
Location: Northern Ireland / England
Posts: 546
#2: Apr 29 '07

re: how does LIKE operator work?


Quote:

Originally Posted by Eglute

I want to write sql sentence with LIKE operator. I wrote this code:

Private Sub Command8_Click()
List1.Clear
Set db = OpenDatabase("the path of my database")
Set rec = db.OpenRecordset("SELECT * From asmenu_info WHERE asmenu_info.pavarde LIKE '%Pet%';")
While Not rec.EOF
List1.AddItem rec!ID & " " & rec!Vardas & " " & rec!pavarde & " " & rec!Data
rec.MoveNext
Wend
rec.Close
db.Close
End Sub

and my program does nothing. I don't undestand how does LIKE work?


Hi,

I developed a VB app in the compact framework for a PDA and using SQL Sever CE and not MS access, but when I was doing something like this, I used this format:

I am assuming that you have assigned "Pet" as a string variable or something

i.e.
Expand|Select|Wrap|Line Numbers
  1. Dim Pet As String
  2. Pet = "Dog"
  3.  
  4. etc...
  5.  
  6. Set rec = db.OpenRecordset("SELECT * From asmenu_info WHERE asmenu_info.pavarde LIKE ' " & Pet & " ' ")
  7.  
But I am not sure if that would be the same for an Access DB
Newbie
 
Join Date: Nov 2006
Posts: 23
#3: Apr 29 '07

re: how does LIKE operator work?


I didn't assign Pet as a variable because I think that it is only a string line I want to find in my database. Why need I to assign Pet as a variable? When I wrote this code in Access I wrote:

Set rec = db.OpenRecordset("SELECT * From asmenu_info WHERE asmenu_info.pavarde LIKE "*Pet*"; ")

and the querry was good, but with visual basic it doesn't work. I don't understand why?
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#4: Apr 29 '07

re: how does LIKE operator work?


Quote:

Originally Posted by Eglute

I didn't assign Pet as a variable because I think that it is only a string line I want to find in my database. Why need I to assign Pet as a variable? When I wrote this code in Access I wrote:

Set rec = db.OpenRecordset("SELECT * From asmenu_info WHERE asmenu_info.pavarde LIKE "*Pet*"; ")

and the querry was good, but with visual basic it doesn't work. I don't understand why?

Actually, I think you have the Like clause correct. It should work exactly the same from VB as when used directly in Access. However, I note that in your original post you have converted the asterisks to percent symbols. If you are still talking to an Access database, I believe you still need to use the asterisks.
Reply