Solved

Access Form VB.  Error MSG when using an apostrophe in text value via SQL

Posted on 2007-11-26
9
229 Views
Last Modified: 2013-11-28
I am using a variable to capture the text that the user enters.  This is used in the SQL per the appropriate field and works without issue unless;  an apostrophe is used in the text.  Example:

dim variable1 as string
 
variable1 = "  hello there Sidney" - this works fine
variable1 = " hello it's Sidney" - this gives me the error.

variable1 = '" & variable1 &"' in the SQL


Thank you.
0
Comment
Question by:ellenjbr
  • 4
  • 4
9 Comments
 
LVL 57
ID: 20352561
Instead of:

variable1 = '" & variable1 &"' in the SQL

  Do this:

variable1 = chr$(34) & variable1 & chr$(34)

JimD
0
 
LVL 8

Expert Comment

by:Chumad
ID: 20352608
You can also do a replace on the string to turn in the single quote into a double single quote if you want to preserve it in the database as a single quote:

variable1 = " hello it's Sidney"

'replaces all single quotes with a double-single quote
variable1 = variable1.Replace("'", "''")
0
 

Author Comment

by:ellenjbr
ID: 20352642
jimd .... it doesn't work.
0
 

Author Comment

by:ellenjbr
ID: 20352790
chumad:  What you posted, is that a practical example or if not, please post one.  Thank you.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 8

Expert Comment

by:Chumad
ID: 20352825
Actually, it would be practical if that were VB.NET and not Access. :)  

Let me dig a bit and look up the replace function in access. I'll post back here when I find it.
0
 
LVL 8

Expert Comment

by:Chumad
ID: 20352833
ok, here's a page that show's how to use replace in access:  http://www.techonthenet.com/access/functions/string/replace.php

One thing I'd do first though just to test this out - change your string manually to 'hello it''s sidney' (note that those are two single quotes, not a double quote). If it does indeed save for you, go ahead with the replace to find all single quotes and convert them to two single quotes.
0
 

Author Comment

by:ellenjbr
ID: 20352981
Thank you, I will look at it and get back with you.
0
 
LVL 8

Accepted Solution

by:
Chumad earned 500 total points
ID: 20398221
Did you have any luck?
0
 

Author Comment

by:ellenjbr
ID: 20404726
hi ... not yet, this is a hectic week ... I will attempt this or next week and get back with you ... thank you
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 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

17 Experts available now in Live!

Get 1:1 Help Now