The information in this article applies to:
SUMMARY
Building concatenated SQL statements based on user-typed text values can
result in invalid SQL statements. This article provides a solution to the
problem of building concatenated SQL.
MORE INFORMATIONWhen building concatenated SQL statements, you can run into the following problems based on incorporating user-typed text into the SQL statement: User Types the Delimiter CharacterIf the user types the same character you use to delimit the text field, such as:LName contains: O'Brien
SQL now contains:
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:
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.
The solution is to replace the apostrophe in the variable with two apostrophes so that SQL contains:
FindFirst Method and DelimitersThe previous section shows the technique of doubling the delimiter when building SQL strings. However, this technique does not work when passing search criteria to the FindFirst method. You have to replace the embedded quote with an expression concatenating chr(39) into the literal:Original criteria that doesn't work:
Criteria that doesn't work with FindFirst:
Criteria that does work with FindFirst:
User Types the Pipe SymbolIf the user types the pipe symbol (|), such as:Password contains: A2|45
SQL now contains:
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:
This 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.
The solution is to replace the pipe symbol with a concatenated expression so that SQL contains:
Implementing the SolutionThe solution to these problems can be addressed through substring replacement. The sample functions, ReplaceStr, SQLFixup, JetSQLFixup, and FindFirstFixup are provided below to illustrate the technique.WARNING: Microsoft provides code examples for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This code is provided "as is" and Microsoft does not guarantee that the following code can be used in all situations. Microsoft does not support modifications of the code to suit customer requirements for a particular purpose. NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. For product versions that don't not support the line-continuation character, remove the underscore and combine that line with the next lines as a single statement when re- creating this code.
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:
LName contains: O'Brien
SQL now contains:
JetSQLFixup should be used if Jet is your database back-end, or if doing a
non-Pass-through query to an ODBC datasource:
UserID cntains: JohnDoe Password contains: A2|4'5
SQL now contains:
FindFirstFixup should be used if using the FindFirst method of a DAO Recordset. It can also be used wherever the JetSQLFixup function is used: LName contains: "O'Brien"
Criteria now contains: LastName = 'O' & chr(39) & 'Brien'
REFERENCES
Microsoft Jet Database Engine Programmer's Guide
© Microsoft Corporation 1999, All Rights Reserved. Additional query words:
Keywords : kbAccess kbAccess100 kbAccess200 kbAccess97 kbVBp300 kbVBp400 kbVBp500 kbVBp600 kbGrpVB kbGrpVBDB kbDSupport |
Last Reviewed: January 5, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |