?
Solved

Problem with quote marks in SQL string (Access VBA)

Posted on 2010-11-13
4
Medium Priority
?
851 Views
Last Modified: 2012-05-10
I have a problem getting the quote marks right in a SQL string in Access VBA. The string I currently have is as follows

CurrentDb.Execute "INSERT INTO " & mcstrGroupTable & " (Group) VALUES ("""" & JetSQLFixup(pstrname) & """");"

Open in new window


where mcstrGroupTable = "tblGroup" and pstrName is a string that can contain any text (including any number of single and double quote marks).

This gives me a "syntax error in the SQL statement" when executed, even when pstrName contains no quote marks at all.

JetSQLFixUp is a helper routine that does the following.

    
    strTemp= Replace(pstrname, "'", "''")
    strTemp = Replace(strTemp, "|", "' & chr(124) & '")
    JetSQLFixup = Replace(strTemp, """", """""")

Open in new window


tblGroup consists of two fields: An Autonumber field GroupID which is the primary key, and a text Field Group.

No doubt the answer will be screamingly obvious but I'm going VBA-blind right now.
0
Comment
Question by:colevalleygirl
  • 2
4 Comments
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 34127109
Your 4 double quotes need to be 2 only:
CurrentDb.Execute "INSERT INTO " & mcstrGroupTable & " (Group) VALUES ("" & JetSQLFixup(pstrname) & "");"

Open in new window

0
 
LVL 31

Accepted Solution

by:
hnasr earned 500 total points
ID: 34127146
Try using single quotes, after checking Qlemo's comment.

CurrentDb.Execute "INSERT INTO " & mcstrGroupTable & " (Group) VALUES ('" &  JetSQLFixup(pstrname) & ')"
0
 

Author Comment

by:colevalleygirl
ID: 34127190
Working from both suggestions, I've got the following to work -- almost:

"INSERT INTO tblGroup (Group) VALUES (" & """" & JetSQLFixup(pstrName) & """" & ");"

It give a SQL string: INSERT INTO tblGroup (Group) VALUES ("sometext");

However, it still doesn't execute so there's something else wrong. That might be another question however!
0
 

Author Comment

by:colevalleygirl
ID: 34127222
For the benefit for anyone else reading thus the final working string is:

INSERT INTO tblGroup ([Group]) VALUES (" & """" & JetSQLFixup(pstrName) & """" & ");
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

850 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