PC Review


Reply
Thread Tools Rate Thread

Bitten by the apostrophe/SQL bug

 
 
Brian
Guest
Posts: n/a
 
      13th Oct 2008
Well, I'm a dunce. I just ran into what has to be one of the oldest problems
around, and while I have a couple ways of getting past it, I'm interested in
the best or most efficient way.

I was using Docmd.RunSQL to insert records but forgot that some names have
apostrophes in them.

BatchID being Long, CustomerName being String, and Amount being Currency:

Dim strSQL as String
'fields to insert
strSQL = "INSERT INTO MyTable(BatchID,CustomerName,Amount)"
'insert this data
strSQL = strSQL & " SELECT " & BatchID & ",'" & CustomerName & "'," & Amount
Docmd.RunSQL strSQL

Of course, it worked fine for the Smiths but choked on O'Donnell. (This is
in the middle of a long loop through a recordset, not just a single record.)

Should I:

1. Use a query & pass it the data as parameters?
2. Do a Replace (CustomerName,"'","")

Some more clever idea?
 
Reply With Quote
 
 
 
 
Allen Browne
Guest
Posts: n/a
 
      13th Oct 2008
Suggestions:

1. Use the double-quote rather than the single quote.
Double-quotes are quite uncommon in names.
This kind of thing:
strSQL = strSQL & " SELECT " & BatchID & _
", """ & CustomerName & """, " & Amount

If the quotes don't make sense, see:
Quotation marks within quotes
at:
http://allenbrowne.com/casu-17.html

2. Use Execute with dbFailOnError:
This gives you much better information about the resuts than RunSQL.

Details in:
Action queries: suppressing dialogs, while knowing results
at:
http://allenbrowne.com/ser-60.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Brian" <(E-Mail Removed)> wrote in message
news:97C5CD2B-A4FB-476C-8A89-(E-Mail Removed)...
> Well, I'm a dunce. I just ran into what has to be one of the oldest
> problems
> around, and while I have a couple ways of getting past it, I'm interested
> in
> the best or most efficient way.
>
> I was using Docmd.RunSQL to insert records but forgot that some names have
> apostrophes in them.
>
> BatchID being Long, CustomerName being String, and Amount being Currency:
>
> Dim strSQL as String
> 'fields to insert
> strSQL = "INSERT INTO MyTable(BatchID,CustomerName,Amount)"
> 'insert this data
> strSQL = strSQL & " SELECT " & BatchID & ",'" & CustomerName & "'," &
> Amount
> Docmd.RunSQL strSQL
>
> Of course, it worked fine for the Smiths but choked on O'Donnell. (This is
> in the middle of a long loop through a recordset, not just a single
> record.)
>
> Should I:
>
> 1. Use a query & pass it the data as parameters?
> 2. Do a Replace (CustomerName,"'","")
>
> Some more clever idea?


 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      13th Oct 2008
On Sun, 12 Oct 2008 21:10:01 -0700, Brian <(E-Mail Removed)>
wrote:

>Well, I'm a dunce. I just ran into what has to be one of the oldest problems
>around, and while I have a couple ways of getting past it, I'm interested in
>the best or most efficient way.
>
>I was using Docmd.RunSQL to insert records but forgot that some names have
>apostrophes in them.
>
>BatchID being Long, CustomerName being String, and Amount being Currency:
>
>Dim strSQL as String
>'fields to insert
> strSQL = "INSERT INTO MyTable(BatchID,CustomerName,Amount)"
>'insert this data
> strSQL = strSQL & " SELECT " & BatchID & ",'" & CustomerName & "'," & Amount
>Docmd.RunSQL strSQL
>
>Of course, it worked fine for the Smiths but choked on O'Donnell. (This is
>in the middle of a long loop through a recordset, not just a single record.)
>
>Should I:
>
>1. Use a query & pass it the data as parameters?
>2. Do a Replace (CustomerName,"'","")
>
>Some more clever idea?


Third option: delimit with " instead of with '.

strSQL = strSQL & " SELECT " & BatchID & ",""" & CustomerName & """," & Amount

strSQL will resemble

SELECT 123, "O'Donnell", 55.00

and will insert the name with no quibbles.
--

John W. Vinson [MVP]
 
Reply With Quote
 
Brian
Guest
Posts: n/a
 
      13th Oct 2008
Thank you, Allen (and Chris & John).

Every day, I learn something new. Both the double-quotes in this context &
the Execute method of the DB using the SQL string are new to me.

"Allen Browne" wrote:

> Suggestions:
>
> 1. Use the double-quote rather than the single quote.
> Double-quotes are quite uncommon in names.
> This kind of thing:
> strSQL = strSQL & " SELECT " & BatchID & _
> ", """ & CustomerName & """, " & Amount
>
> If the quotes don't make sense, see:
> Quotation marks within quotes
> at:
> http://allenbrowne.com/casu-17.html
>
> 2. Use Execute with dbFailOnError:
> This gives you much better information about the resuts than RunSQL.
>
> Details in:
> Action queries: suppressing dialogs, while knowing results
> at:
> http://allenbrowne.com/ser-60.html
>
> --
> Allen Browne - Microsoft MVP. Perth, Western Australia
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
> "Brian" <(E-Mail Removed)> wrote in message
> news:97C5CD2B-A4FB-476C-8A89-(E-Mail Removed)...
> > Well, I'm a dunce. I just ran into what has to be one of the oldest
> > problems
> > around, and while I have a couple ways of getting past it, I'm interested
> > in
> > the best or most efficient way.
> >
> > I was using Docmd.RunSQL to insert records but forgot that some names have
> > apostrophes in them.
> >
> > BatchID being Long, CustomerName being String, and Amount being Currency:
> >
> > Dim strSQL as String
> > 'fields to insert
> > strSQL = "INSERT INTO MyTable(BatchID,CustomerName,Amount)"
> > 'insert this data
> > strSQL = strSQL & " SELECT " & BatchID & ",'" & CustomerName & "'," &
> > Amount
> > Docmd.RunSQL strSQL
> >
> > Of course, it worked fine for the Smiths but choked on O'Donnell. (This is
> > in the middle of a long loop through a recordset, not just a single
> > record.)
> >
> > Should I:
> >
> > 1. Use a query & pass it the data as parameters?
> > 2. Do a Replace (CustomerName,"'","")
> >
> > Some more clever idea?

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Needed, May have bitten off more than I can chew Vacuum Sealed Microsoft Excel Programming 35 17th Jan 2011 03:11 PM
xtort.net bitten the dust? mArk Freeware 5 1st Jan 2006 12:12 PM
bitten nails RoS Windows XP New Users 4 25th Jan 2005 08:39 PM
IE6 seems to have been bitten by the Bugbear Mr.Debug Windows XP Internet Explorer 2 14th Sep 2003 12:50 PM
Bitten in butt by DFS / FRS problem! Daniel Billingsley Microsoft Windows 2000 File System 6 26th Aug 2003 03:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:18 AM.