Solved

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

Posted on 2007-11-26
9
231 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ms access 2010 vba, copy table from db1 to db2 from db3 27 40
Modal form 11 29
export sql results to csv 6 34
SQL Server 2012 r2 - Varible Table 3 20
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. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 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

14 Experts available now in Live!

Get 1:1 Help Now