Article ID: 178070 - Last Review: February 12, 2007 - Revision: 4.4 How To Handle Quotes and Pipes in Concatenated SQL LiteralsThis article was previously published under Q178070
For a Microsoft Visual Basic .NET version of this article, see 311023
(http://support.microsoft.com/kb/311023/EN-US/
)
.
On This PageSUMMARY
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.
NOTE: Microsoft Visual Basic 6.0 and later products and Microsoft Office 98 and later products come with a Replace function built-in MORE INFORMATION
When 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
Run-time error 3075
Syntax error in query expression '...'
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: User Types the Pipe SymbolIf the user types the pipe symbol (|), such as:Password contains: A2|45
Run-time error 3061
Too few parameters. Expected n.
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. LName contains: O'Brien 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" REFERENCES
Microsoft Jet Database Engine Programmer's Guide
Visual Basic Help topic: InStr For additional information, please see the following article in the Microsoft Knowledge Base: 147687 How To Query for Literal Special Characters in a Where Clause (http://support.microsoft.com/kb/147687/EN-US/ ) APPLIES TO
| Article Translations
|
Back to the top
