> When I open the recordset using Like '%[']%' the error reads: syntax
> error in query expression. I am actually trying to search for a single
> quote not a double,
This works for me:
SELECT SerialNum, FName, LName
FROM dbo.Children
WHERE (LName LIKE N'%['']%')
> My original plan was to be able to enter a character or
> set of characters into a textbox and search a field returning all the
> results to a table.
strSQL = "... LIKE " & QuoteText(Me.txtTextbox.Value) & "..."
Public Function QuoteText(SomeText As Variant) As String
' use a variant in case it's passed a control.Value property
Const c_wSQ = 39 ' = asc("'")
Dim strTemp As String
If IsNull(SomeText) Then
strTemp = String$(2, c_wSQ)
Else
strTemp = Chr(c_wSQ) & _
Replace(SomeText, String$(1, c_wSQ), String$(2, c_wSQ)) & _
Chr$(c_wSQ)
End If
QuoteText = strTemp
End Function
> I am opening an ADO recordset based on my SQL
> statement then inserting one line at a time the recordset information.
> (if I insert all at once my process doesn't work for date or number
> fields, a different issue though).
I have to say that I don't use ADO much... inserting one line of what at
a time into what?
B Wishes
Tim F
Rizza - 24 May 2005 16:42 GMT
It works! I understand how the quotetext function works but I don't
understand why. It appears to me to pass four single quotes. How is the
recordset finding data containing one single quote?
> I have to say that I don't use ADO much... inserting one line of what at
> a time into what?
I am inserting one line at a time the table name, field name, and data found
in the table.field based on the search text box.
> WHERE (LName LIKE N'%['']%')
I also don't understand what N is for. Is it a typo?
> > When I open the recordset using Like '%[']%' the error reads: syntax
> > error in query expression. I am actually trying to search for a single
[quoted text clipped - 39 lines]
>
> Tim F
Tim Ferguson - 24 May 2005 17:17 GMT
>> Public Function QuoteText(SomeText As Variant) As String
>> ' use a variant in case it's passed an empty control.Value
>> ' property
>> strTemp = Chr(c_wSQ) & _
>> Replace(SomeText, String$(1, c_wSQ), String$(2, c_wSQ)) & _
>> Chr$(c_wSQ)
>> End If
>> QuoteText = strTemp
> I understand how the quotetext function works but I don't
> understand why. It appears to me to pass four single quotes.
No: it doubles them, that's all. By accident, I came across this
documented in the SQL Books On Line so I now know it's true rather than
assuming it. The example there is:
SELECT @au_name = 'O''Leary'
so there you are!!
>> WHERE (LName LIKE N'%['']%')
>
> I also don't understand what N is for. Is it a typo?
(ahem) I don't either really. It's not a typo: the Access GUI sticks them
in front of all quoted strings so I tend to leave them there. My guess
it's something to do with forcing a Unicode string rather than ANSI, like
NCHAR() and NVARCHAR() and so on.
All the best
Tim F
Rizza - 24 May 2005 17:58 GMT
Wonderful help!
Thank you.