I goofed in the VBA step example. And, I've rewritten the function so that
you can have just a single function to do it all.
strSQL = "SELECT * FROM TableName WHERE FieldName Like '" & _
SQLAddBrackets(Me.NameOfControl.Value) & "';"
Public Function SQLAddBrackets(ByVal xstrReplaceStringValue) As String
' *** THIS FUNCTION SURROUNDS "[", "*", "#", AND "?"
' *** CHARACTERS WITH [ AND ] CHARACTERS
' *** IN A TEXT STRING. ITS USE IS FOR TEXT STRINGS THAT
' *** ARE USED AS PARAMETERS IN VARIOUS QUERIES' "WHERE"
' *** STATEMENTS WHEN "LIKE" IS USED INSTEAD OF "=".
' *** THIS FUNCTION SHOULD BE USED WHEN "LIKE" IS USED
' *** TO MATCH A TEXT STRING.
' *** THIS FUNCTION IS NOT TO BE USED WHEN "=" OR "<>" IS
' *** USED TO MATCH A TEXT STRING.
' xstrReplaceStringValue is variant (string) variable that contains the
' text string that needs to be converted
On Error GoTo Err_ZZZZAddBrackets
xstrReplaceStringValue = Replace(Nz(xstrReplaceStringValue, ""), _
"[", "[[]", 1, -1, vbTextCompare)
xstrReplaceStringValue = Replace(Nz(xstrReplaceStringValue, ""), _
"*", "[*]", 1, -1, vbTextCompare)
xstrReplaceStringValue = Replace(Nz(xstrReplaceStringValue, ""), _
"#", "[#]", 1, -1, vbTextCompare)
xstrReplaceStringValue = Replace(Nz(xstrReplaceStringValue, ""), _
"?", "[?]", 1, -1, vbTextCompare)
SQLAddBrackets = xstrReplaceStringValue
Err.Clear
Exit Function
Err_ZZZZAddBrackets:
SQLAddBrackets = xstrReplaceStringValue
Resume Next
End Function
--
Ken Snell
<MS ACCESS MVP>
Ken Snell (MVP) said:
You can use the Replace function to delimit the ampersand character with
[ ] characters, and then you can handle the text string in VBA and query.
I use a function as a wrapper for whenever I am concatenating strings into
SQL query statements etc. in code:
< snipped >