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%"

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.Refresh
Else
adobooksearch.RecordSource = "SELECT * from tblbooks" 'where Author like '%" & "#" & "%'"
adobooksearch.Refresh
End If
End Sub

Edited 1 Year Ago by lyca

HERE:

adobooksearch.Refresh

Edited 1 Year Ago by lyca

Hi

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.

HTH

This is a query

SELECT * FROM tblbooks WHERE author LIKE 'Smi%'

This is not a query

adobooksearch.Refresh

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

This question has already been answered. Start a new discussion instead.