im a complete newbie in visual basic and im currently working on my first project which is the book borrowing system. the project is almost done except with this error.

Syntax error(missing operator) in query expression 'Title like %Mathew Henry's Commentary:GEnesis to Revelation%"

Recommended Answers

All 7 Replies

Here is the complete query of the error.
I have a combobox, a txtbox namely txtsearch and a datalistfield in which all the titles of the book will appear.

Private Sub txtsearchbook_Change()

If cmbsearchbook.Text <> "" Then
adobooksearch.RecordSource = "SELECT * from tblbooks where " & cmbsearchbook & " like '%" & txtsearchbook.Text & "%'"
adobooksearch.RecordSource = "SELECT * from tblbooks" 'where Author like '%" & "#" & "%'"
End If
End Sub

What you posted was the code that generates the query. What I want to see is the query that is causing the error.




It looks like your WHERE criteria contains a value with an apostraphe so your SQL statement is breaking as the criteria itself needs to be enclosed within apostraphes.

You can either use parameterised queries or escape your search criteria. For example "SELECT * from tblbooks where " & cmbsearchbook & " like '%" & Replace(txtsearchbook.Text, "'", "''") & "%'". Note how the replace function has been used to take one apostraphe and replace it with two, thus escaping it.


This is a query

SELECT * FROM tblbooks WHERE author LIKE 'Smi%'

This is not a query


If you want help debugging the query then post your query.

It works djjeavons.
Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.