473,511 Members | 15,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Acc2K - SQL Fails due to Single Quote in Field

MindBender77
234 New Member
Hello All,
The following SQL string fails because astrOrdernotes(8) has a single quote in the field. Example Henry O'Smith. The data type for this field is a string but, I keep getting a syntax error starting with this field.

Expand|Select|Wrap|Line Numbers
  1. strSQL = "INSERT Into tbl_shipping_label ([Our Street],[Our City],[Address Service]," & _
  2. "[First Class],[Postage],[Our City2],[Permit],[Pt Name],[Pt Street],[Pt City],[Pt CC],[Pt Orderno])" & _
  3. "values ('" & astrOrdernotes(1) & "','" & astrOrdernotes(2) & "','" & astrOrdernotes(3) & "'," & _
  4. "'" & astrOrdernotes(4) & "','" & astrOrdernotes(5) & "','" & astrOrdernotes(6) & "','" & astrOrdernotes(7) & "'," & _
  5. "'" & astrOrdernotes(8) & "','" & astrOrdernotes(9) & "','" & astrOrdernotes(10) & "','" & astrOrdernotes(11) & "'," & _
  6. "'" & astrOrdernotes(12) & "');"
  7. CurrentDb.Execute strSQL, dbFailOnError
  8.  
Any suggestions would be appreciated,
Bender
Dec 17 '08 #1
3 2779
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hiya Bender. Problem is that you are using single quotes as string delimiters for the VALUES part of your string, and when a single quote is encountered within the value it ends the string too early (as in 'O'Smith').

Access will allow the use of double-quotes in the SQL string, but you cannot type these in directly without them getting really messy in appearance (from the repeats that are necessary). The example below uses an approach of substituting another uncommon character for the double quotes in building the string (an @ in this case), then replacing these with double quotes after building:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "INSERT Into tbl_shipping_label ([Our Street],[Our City],[Address Service]," & _
  2. "[First Class],[Postage],[Our City2],[Permit],[Pt Name],[Pt Street],[Pt City],[Pt CC],[Pt Orderno])" & _
  3. "values (@" & astrOrdernotes(1) & "@,@" & astrOrdernotes(2) & "@,@" & astrOrdernotes(3) & "@," & _
  4. "@" & astrOrdernotes(4) & "@,@" & astrOrdernotes(5) & "@,@" & astrOrdernotes(6) & "@,@" & astrOrdernotes(7) & "@," & _
  5. "@" & astrOrdernotes(8) & "@,@" & astrOrdernotes(9) & "@,@" & astrOrdernotes(10) & "@,@" & astrOrdernotes(11) & "@," & _
  6. "@" & astrOrdernotes(12) & "@);"
  7. strSQL = Replace(strSQL, "@", Chr(34))
In testing this I substituted for the array references to make sure it worked as expected, and certainly the string built was valid.

I could not execute the statement you supplied, but I did test the approach on a simple table with string values containing single quotes and it worked as expected.

-Stewart
Dec 17 '08 #2
MindBender77
234 New Member
Thank you very much, Stewart, this problem is solved.

The most valuable information you've given was the explanation of why this problem was occurring. I modified the delimiter here and then later on in the module for the same issue. It now works flawlessly......so far.

Good day to you and again, Thank You,

Bender
Dec 17 '08 #3
NeoPa
32,557 Recognized Expert Moderator MVP
See Quotes (') and Double-Quotes (") - Where and When to use them.

My advice (normally very much in line with Stewart's but diverges somewhat here) would be to apply a quote doubler to the data. A wrapper function that replaces single-quotes (') with two single-quotes ('') :
Expand|Select|Wrap|Line Numbers
  1. ..."','" & DWrapper(astrOrderNotes(9)) & "','" & ...
There are other ways of handling the issue, but handling the string terminators in SQL in a non-SQL-standard way may cause problems if you ever try to move out of the Access arena.

That may be entirely trivial in your circumstances, so you judge whether this is worth worrying about or not. It certainly works in Access.
Dec 23 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2632
by: Joe | last post by:
Hey, I'm going to give some background on my situation in case anyone can point out a way around my problem altogether... for the problem itself, please skip to the bottom of the post. thanks....
1
1380
by: | last post by:
Can anyone point me to a good resource to show me what potential problems I'll have converting from Access 2000 to Access 2003? I understand that 2003 can read and work with the 2000 files, but that...
5
3518
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
4
6232
by: fniles | last post by:
I am looping thru DataReader and constructing a sql query to insert to another database. When the data type of the field is string I insert the field value using a single quote. When the value of...
4
2253
by: Chris | last post by:
Hello all, Using g++ 3.3.1 on Linux ("Linux From Scratch") I have a data structure SCSIParams_t that I wish to print out, field-by-field. Rather than code a long line of the form std::cout <<...
2
1554
MindBender77
by: MindBender77 | last post by:
Hello all, I get an error when trying to run an append query that states duplicate output to a field name. I think because I'm trying to append records to a table that is already in the use. Below...
25
3887
by: Rick Collard | last post by:
Using DAO 3.6 on an Access 2002 database, I'm getting unexpected results with the FindFirst method. Here's the simple code to test: Public Sub FindIt() Dim db As Database, rs As Recordset...
1
1861
MindBender77
by: MindBender77 | last post by:
Hello Again All, I have a memo field that is backslash delimited. I am attempting to break up this field into individual fields. Ex (memo field). \John L. Doe\\StreetName RD\\City, Pa 11111 ...
4
1919
MindBender77
by: MindBender77 | last post by:
Hello All, I'm not sure if this is possible and have been unsuccessful in my attempts. Scenerio: I have a report with several fields. If the data in field X = Y then shade the entire row. ...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.