Article ID: 178070
Article Last Modified on 2/12/2007
SQL = "SELECT * FROM Employees WHERE LastName='" & LName & "'"SQL now contains:
SELECT * FROM Employees WHERE LastName='O'Brien'this can result in the following error message when you execute the SQL statement: One solution is to replace the apostrophe delimiter with quotes ("), such as:
SQL = "SELECT * FROM Employees WHERE LastName=""" & LName & """"However, the user could easily type O"Brien by mistake (forgetting to release the SHIFT key when typing the apostrophe) and the problem reappears. In addition, SQL Server uses " to delimit table and field names. If the user-supplied value exceeds the maximum length of an identifier name, SQL Server will return a syntax error.
SELECT * FROM Employees WHERE LastName='O''Brien'
LastName = 'O'Brien'Criteria that doesn't work with FindFirst:
LastName = 'O''Brien'Criteria that does work with FindFirst:
LastName = 'O' & chr(39) & 'Brien'
SQL = "SELECT * FROM SecurityLevel WHERE UID='" & UserID & "'" SQL = SQL & " AND PWD='" & Password & "'"SQL now contains:
SELECT * FROM SecurityLevel WHERE UID='JohnDoe' AND PWD='A2|45'and you are querying a Jet database, it can cause either the "Syntax Error" given above or the following error: The pipe symbol causes problems because Jet uses pipe symbols to delimit field or parameter names embedded in a literal string, such as:
SELECT "|LastName|, |FirstName|" FROM EmployeesThis was considered easier for beginner users to learn than concatenation when building ad hoc queries through the Access Query designer. However, when used inadvertently in building a SQL statement, it can result in an error.
SELECT * FROM SecurityLevel WHERE UID='JohnDoe' AND PWD='A2' & chr(124) & '45'
Function ReplaceStr (TextIn, ByVal SearchStr As String, _
ByVal Replacement As String, _
ByVal CompMode As Integer)
Dim WorkText As String, Pointer As Integer
If IsNull(TextIn) Then
ReplaceStr = Null
Else
WorkText = TextIn
Pointer = InStr(1, WorkText, SearchStr, CompMode)
Do While Pointer > 0
WorkText = Left(WorkText, Pointer - 1) & Replacement & _
Mid(WorkText, Pointer + Len(SearchStr))
Pointer = InStr(Pointer + Len(Replacement), WorkText, _
SearchStr, CompMode)
Loop
ReplaceStr = WorkText
End If
End Function
Function SQLFixup(TextIn)
SQLFixup = ReplaceStr(TextIn, "'", "''", 0)
End Function
Function JetSQLFixup(TextIn)
Dim Temp
Temp = ReplaceStr(TextIn, "'", "''", 0)
JetSQLFixup = ReplaceStr(Temp, "|", "' & chr(124) & '", 0)
End Function
Function FindFirstFixup(TextIn)
Dim Temp
Temp = ReplaceStr(TextIn, "'", "' & chr(39) & '", 0)
FindFirstFixup = ReplaceStr(Temp, "|", "' & chr(124) & '", 0)
End Function
SQLFixup should be used if your SQL statement is going to be used with Jet
SQL pass-through queries or with ODBCDirect, RDO, or ADO to a non-Jet back-
end database:
SQL = "SELECT * FROM Employees WHERE LastName='" & _
SQLFixup(LName)& "'"
SQL now contains:
SELECT * FROM Employees WHERE LastName='O''Brien'JetSQLFixup should be used if Jet is your database back-end, or if doing a non-Pass-through query to an ODBC datasource:
SQL = "SELECT * FROM SecurityLevel WHERE UID='" & _
JetSQLFixup(UserID) & "' AND PWD='" & JetSQLFixup(Password) & "'"
SELECT * FROM SecurityLevel WHERE UID='JohnDoe' AND PWD='A2' & chr(124)&'4''5'
Criteria = "LastName = '" & FindFirstFixup(LName) & "'"Criteria now contains: LastName = 'O' & chr(39) & 'Brien'
Keywords: kbhowto KB178070