Solved

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

Posted on 2007-11-26
9
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ellenjbr
ID: 20352790
chumad:  What you posted, is that a practical example or if not, please post one.  Thank you.
0
 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

730 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