473,587 Members | 2,548 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 2788
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,566 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
2645
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. I've been having some problems with database performance... Several threads are constantly attempting INSERTs of new records into a large table -...
1
1383
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 2003 has a different native format? I'd just like to get a good idea of the different issues that might come up. Thanks for any guidance, E
5
3528
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 do the update the changed parentid in the child table fails to change. No error is given its just that the change is not written to the Database. ...
4
6238
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 the field has a single quote in it like "O'Toole", how can I construct the string ? Do While drSQL.Read sSQL = "insert into " & sTableName & "...
4
2257
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 << "fieldname = " << basepointer->fieldname << std::endl; for every field, I'd like to do it with a short macro, e.g.
2
1561
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 is the SQL from the query. Thanks in advance. Bender INSERT INTO PillweightArchive ( , NDC, , Single_Label, ) SELECT DISTINCT...
25
3905
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 Dim sCriteria As String Set db = DBEngine.Workspaces(0).Databases(0) Set rs = db.OpenRecordset("tblTest", dbOpenDynaset)
1
1863
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 To look like this: John L. Doe StreetName RD City, Pa 11111
4
1925
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. I've been attempting to loop through the report using the following in the OnFormat Event: For Each ctl In Me.Section(0).Controls If...
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5394
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
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 we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.