Solved

Single Quote as SQL String Delimiter

Posted on 2002-05-10
1
142 Views
Last Modified: 2011-10-03
I'm using VB6 against an sql server database.
I use a single quote to string together my sql statements and,as you can imagine, if a user enters a string with a single quote in it, the statement bombs in sql server.

For example,
Here's the completed string:

update Programs set agencycode = 'INSUR', programcode = 'XTS', description = 'Stock Workmen's Compensation Security Fund', sequence = '5', lastactionuser = 'Trapatsos      ', lastactiondate = getdate(), recordstatus = 'Active ' where id = 207

and the string chokes with:
"Incorrect syntax near 's'"

Does anyone have the best advice for stringing together sql statements with values that may contain single quotes?

Thanks in Advance

John
0
Comment
Question by:jtrapat1
1 Comment
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 100 total points
ID: 7002048
Replace single quotes with two quotes.

Dim sValue As string
sValue = "Stock Workmen's Compensation Security Fund"
sValue = Replace(sValue, "'", "''")

Anthony
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Extract Data and filename Macro 20 99
Use Multiple Forms 4 38
VBA error replacing data 6 35
vb6 connector to SQL Server 2 32
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now