Question:
Syntax error (missing operator) in query expression (Visual Basic 6)?
>>Rock God<<
2010-04-22 13:28:40 UTC
IN VB6

Hi,

When I run a simple search on a database I get the following error:

Syntax error (missing operator) in query expression 'Lesson= B Monday 12:00pm'.
(B Monday 12:00pm is my search data)

I then get the following error:

Method 'Refresh' of object 'IAdodc' failed

The code is as follows:

Private Sub search_cmd_Click()
If (TxtSearch = "") Then MsgBox "You need to select a lesson"
Adodc1.RecordSource = "SELECT * FROM booking WHERE Lesson= " & TxtSearch.Text & ""
Adodc1.Refresh
End Sub

On debug it highlights Adodc1.Refresh

Thanks!
Six answers:
Nick
2010-04-23 06:00:08 UTC
Everyone is right in what they are saying. You are missing the quote. SQL requires strings encased within single quotes.



Try this



If (TxtSearch = "") Then MsgBox "You need to select a lesson"

Adodc1.RecordSource = "SELECT * FROM booking WHERE Lesson= '" & TxtSearch.Text & "'"

Adodc1.Refresh

End Sub



Note that it is not easy to see in this editor so I'll space it out for you.

.........WHERE Lesson= ' " & TxtSearch.Text & " ' "



Just as a note. The search will only work if there is a lesson that matchs your exact search string. If you use LIKE instead you can add some wild cards % at the front, end or both of your search string to find all lessons 'like' Lesson= B.



For example:



If (TxtSearch = "") Then MsgBox "You need to select a lesson"

Adodc1.RecordSource = "SELECT * FROM booking WHERE Lesson LIKE '%" & TxtSearch.Text & "%'"

Adodc1.Refresh

End Sub
Ratchetr
2010-04-22 13:35:23 UTC
You're missing the opening quote around the text in TxtSearch.Text. Add a ' before the " Lesson=
Longhorn_Hookem
2010-04-22 13:32:41 UTC
Looks like you need to add ' around your search string. So it should be:



SELECT * FROM booking WHERE Lesson= 'B Monday 12:00pm'



NOT



SELECT * FROM booking WHERE Lesson= B Monday 12:00pm
anonymous
2016-12-11 20:58:53 UTC
Vb6 Operator
anonymous
2016-11-01 06:08:30 UTC
hi what you doing, Article table doesnt have column Magazine_ID. you would be able to desire to connect those 2 tables to get the end result. go with count quantity (different Magazine_ID) From magazine inner connect Article ON magazine.call = Article.call the place Article.call = 'J. ok. Rowling' you will get rid of different clause from go with question if magazine column is regular key and its one to a minimum of one relationship between magazine and Article. Regards
Jonas
2010-04-22 13:30:22 UTC
It should be

Adodc1.RecordSource = "SELECT * FROM booking WHERE Lesson= '" & TxtSearch.Text & "' "



(With apostrophes)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...