473,377 Members | 1,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

Replacing apostrophe in a recordset for SQL insert

21
Hello!

I am attempting to take data from one table and insert it into another. I am encountering problems where data in a recordset may contain an apostrophe, which completely kills my SQL Insert.

Expand|Select|Wrap|Line Numbers
  1. DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES('" & rs![oldtext] & "')"
Problem is, some of these records may also be Null, so that kills a Replace() call.

I've found a really sloppy way of getting around this, but it's horrendous!

Expand|Select|Wrap|Line Numbers
  1. oldvar = ""
  2. if(Len(rs![oldtext]) > 0 then
  3. oldvar = rs![oldtext]
  4. oldvar = Replace(oldvar, "'", "''")
  5. end if
  6.  
  7. DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES('" & oldvar & "')"
  8.  
Please help. Been trying to figure this out for over an hour, and each time I think I find a solution, some funky record comes up and knocks me down!

Thanks!
Apr 27 '07 #1
14 14438
MMcCarthy
14,534 Expert Mod 8TB
Try this ...

Expand|Select|Wrap|Line Numbers
  1. DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES(" & "" & rs![oldtext] & "" & ")"
Apr 27 '07 #2
Himmel
21
That returns an error on the first value it tries to write: "Syntax error (missing operator) in query expression."

In Debug, I can hover over the expression it is saying is in error, and it (along with all other expressions) contain the data that is expected.
Apr 27 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
Try this ...

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL as String
  2.  
  3.    strSQL = "INSERT INTO NEWTABLE (Record1) VALUES(" & "" & rs![oldtext] & "" & ")"
  4.  
  5.    DoCmd.RunSQL strSQL
  6.  
Apr 27 '07 #4
pks00
280 Expert 100+
try this


DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES(" & chr$(34) & NZ(rs![oldtext],"") & chr$(34) & ")"

nulls, hmm yes. If u ensure that the field allows nulls and is not mandatory then it should work
Apr 27 '07 #5
MMcCarthy
14,534 Expert Mod 8TB
try this


DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES(" & chr$(34) & NZ(rs![oldtext],"") & chr$(34) & ")"

nulls, hmm yes. If u ensure that the field allows nulls and is not mandatory then it should work
Nice one, should have thought of that. ;)
Apr 27 '07 #6
pks00
280 Expert 100+
Nice one, should have thought of that. ;)
Its cos your overworked as a Administrator and ensuring orphaned questions get cleared :)
Apr 27 '07 #7
MMcCarthy
14,534 Expert Mod 8TB
Its cos your overworked as a Administrator and ensuring orphaned questions get cleared :)
LOL, thanks for that.
Apr 27 '07 #8
pks00
280 Expert 100+
No probs :)
Apr 27 '07 #9
Himmel
21
Thanks guys. :) This is starting to shape up nicely.

I hate to throw a wrench into the works, but now I'm getting an operand error when the SQL encounters a record that contains a quotation mark. And again, the field may be Null.

Any suggestions on what to do next?
Apr 27 '07 #10
pks00
280 Expert 100+
What code are you using
Apr 28 '07 #11
Himmel
21
What code are you using
I took a page out of your book:

Expand|Select|Wrap|Line Numbers
  1. DB.Execute "INSERT INTO NEWTABLE (Record1) VALUES(" & chr$(34) & NZ(rs![oldtext],"") & chr$(34) & ")"
This handles fields that have an apostrophe. But if any have a quotation, it breaks.
May 1 '07 #12
I can advise you where to look.

Not 100% sure how..

But I'd search on how SQL insert statements handle apostrophes in inserting data.

Then I'd use the technique to 'replace' apostrophes with whatever the syntax SQL is appropriate for the database/SQL.

e.g.
Expand|Select|Wrap|Line Numbers
  1. tempStr = "String with apost'rophe"
  2. newStr = replace(tempStr,"'","''") ''DOUBLE APOSTROPHE
  3.  
Might work.. Dunno.
Nov 2 '07 #13
BigJ
1
Replace the apostrophe in values that are encased in single quotes (e.g. the O'Brien in 'O'Brien') as follows: O' & "'" & 'Brien

Use the following code snippet:
Expand|Select|Wrap|Line Numbers
  1. Replace(rs![field1], " ' ", " ' " & " & " & """" & " ' " & """" & " & ' ") & "'" )
NOTE I have added a space between the single and double quotes to make it easier to tell them apart. In the actual code you use, you should not have these spaces.

For example

Instead of
Expand|Select|Wrap|Line Numbers
  1. Docmd.RunSQL ("INSERT INTO Tablename (DestinationField) SELECT '" & rs![field1] & "'")
use
Expand|Select|Wrap|Line Numbers
  1. Docmd.RunSQL ("INSERT INTO Tablename (DestinationField) SELECT '" & Replace(rs![field1], "'", "'" & " & " & """" & "'" & """" & " & '") & "'" )
This worked for me and allowed me to use VBA to insert values containing apostrophes (single quote marks) into a table using SQL insert statements
Feb 6 '13 #14
NeoPa
32,556 Expert Mod 16PB
This is really a lot more straightforward than people give it credit for. Doubling up quotes of either form can be used to represent the character that is used as the string delimiter. See SQL Injection Attack for some very simple code, and also why it can be very important to get right, of course.
Feb 7 '13 #15

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

Similar topics

5
by: Les Juby | last post by:
A client needs a routine to alert him as to which memo records in an Access-2000 database have had double apostrophes inserted in the text. These are stopping a Java mouseover from executing. ...
3
by: Peter Choe | last post by:
i am trying to add a record of a name. the name has an apostrophe (O'Neil). i am doing this through a servlet and wrote the code to use PreparedStatement. when i do: .... PrepareStatement...
2
by: CSDunn | last post by:
Hello, I have a combo box designed to look up records in a subform based on the selection made in the combo box. The Record Source for the combo box is a SQL Server 2000 View. There is one bound...
13
by: Richard Hollenbeck | last post by:
To prevent future apostrophe bugs and errors, isn't it just simpler to forbid an apostrophe from being entered into a text field? For example, couldn't "Alice's Restaurant" be changed to "Alices...
11
by: Trevor | last post by:
Hi, I currently have the following code in a subform. If Not rstPDAddresses.EOF And Not rstPDAddresses.BOF Then With rstPDAddresses txtautoAddressID.ControlSource = "='" & Nz(!autoAddressID,...
5
by: Thomas Beyerlein | last post by:
I have been looking for how to INSERT text with an apostrophe in it into SQL example: dim TEXT as string text = "TEST'S" INSERT INTO tbl_Test (String) VALUSES('" & TEXT & "')" This doesn't...
10
by: chuckzter | last post by:
Hey guys, I would like to know how to replace values in your database via ADODB. Example: (I would want to replace the value LOCATION of ID 1001 which is Oliver to Laguna.) ID NAME...
1
by: tsilcyc03 | last post by:
Ok, so I'm super new at this stuff and I looked at the example on this site on how to do it but I can't get it to work with my statement. How do I replace the apostrophe with a double apostrophe in...
3
by: pitchblack408 | last post by:
Hello I am using excel as my database and when I do an insert there is an apostrophe that appears in the cell where a string was inserted. For example '(474)343-3433 It appears that the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.