Solved

Problem with quote marks in SQL string (Access VBA)

Posted on 2010-11-13
4
813 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 69

Assisted Solution

by:Qlemo
Qlemo earned 125 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 30

Accepted Solution

by:
hnasr earned 125 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mysql Left Join Case 10 55
How to use three values with DATEDIFF 3 24
SQL Use Distinct with two fields 3 11
Switch 5 17
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

773 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