Solved

Syntax error (missing operator) when using single and double apostrophe(' and ") with the input data

Posted on 2013-01-02
14
544 Views
Last Modified: 2013-01-03
Dear Experts, Getting Syntax error (missing operator) when inserting data with single and double apostrophe(' and ") .
if i use this way (""" & Me.Text_box & """) it will take single apostrophe but cannot resolve when double apostrophe came in.
Please help
Thanks
0
Comment
Question by:gtmathewDallas
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 30

Expert Comment

by:hnasr
ID: 38738368
Try:
Chr(34) & Me.Text_box & Chr(34)
0
 

Author Comment

by:gtmathewDallas
ID: 38738379
I tried but showing run time error 2471 :(
0
 

Author Comment

by:gtmathewDallas
ID: 38738382
DCount("*", "REVIEW_TYPE", "Review_Name = Chr(34) & Me.Text_RVTEdit.value & Chr(34) and Review_Type_Id <>  CLng('" & Me.Combo_RVEdit & "')")
0
 
LVL 1

Expert Comment

by:chopper_irl
ID: 38738385
it looks like you need to escape the characters, which is done by a single quote so
('I'll be there in a second O'Connor') will be ('I''ll be there in a second O''Connor')
or
(Quote:"Show me the money") would be (Quote':'"Show me the money'")

this is not the safest/best way to escape characters but its a start...
0
 
LVL 75
ID: 38738394
How about

Chr(34) & Replace(Replace(Replace(Me.Text_box, Chr(39),""),Chr(39) & Chr(39),""), Chr(34),"")   & Chr(34)
0
 

Author Comment

by:gtmathewDallas
ID: 38738396
Actually the user can be enter any type of key values (', ",&,$,% etc ) to the database, is there any way to allow this?
I was using the controls this way-  '" & Me.Text_Box & "' but its not working..
0
 
LVL 30

Expert Comment

by:hnasr
ID: 38738597
Try:

DCOUNT ("*", "REVIEW_TYPE", "Review_Name = "  & Chr(34) & Me.Text_RVTEdit.value & Chr(34)  & " and Review_Type_Id <>  CLng(" & Me.Combo_RVEdit & ")")
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38738642
<Getting Syntax error (missing operator) when inserting data with single and double apostrophe(' and ") .
Not sure how you are "Inserting", but this works fine for me:

CurrentDb.Execute "INSERT INTO YourTable (YourTextField) Values(" & "'" & Replace(Me.txtTextWithApostrophe, "'", "''") & "'" & ")", dbFailOnError
test.mdb
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38738645
There may be a way to "tighten up" the syntax in my above post, but again, it works fine for me...
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 38738667
I use a function when to format my text strings that are either SQL statements or criteria strings.  The function looks like:
Public Function Quotes(varTextToQuote As Variant) As String

   Quotes = Chr$(34) & Replace(Nz(varTextToQuote, ""), Chr$(34), Chr$(39)) & Chr$(34)
   
End Function

Open in new window

The function will wrap whatever value is passed to it with double quotes, and will replace instances of a " in the text string with double quotes (""), which will be interpreted as a single, embedded quote.  It is so much easier to read this than trying to wrap the text value in extra quotes or to use the chr$(34) in your code.

You use the function like:

DCount("*", "REVIEW_TYPE", "Review_Name = " & Quotes(Me.Text_RVTEdit) & " AND [Review_Type_Id] <>  CLng(" & Me.Combo_RVEdit & ")")

You really don't need to wrap the value from Me.Combo_RVEdit in single quotes, like you had, prior to using the clng() conversion function.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38739644
Any of the above posts should work, but here's another approach using the Parameters collection.  This Access VBA is similar to what you would use in .Net:

Private Sub cmdGetCount_Click()
    Dim qd As New DAO.QueryDef
    On Error GoTo EH
    CurrentDb.QueryDefs.Delete ("qtemp")
    Set qd = CurrentDb.CreateQueryDef("qtemp", "SELECT COUNT(*) AS TotReviews FROM REVIEW_TYPE WHERE Review_Name = @ReviewName AND Review_Type_Id <> @ID ")
    With qd
        .Parameters("@ReviewName") = Me.Text_RVTEdit
        .Parameters("@ID") = Me.Combo_RVEdit
    End With
    MsgBox qd.OpenRecordset.Fields("TotReviews")
    Set qd = Nothing
    Exit Sub
EH:
    If Err.Number = 3265 Then Resume Next
    MsgBox "ERROR " & Err.Number & ": " & Err.Description
   
End Sub

Open in new window


With the parameters collection, you don't need to worry about escaping special characters (because you're not using text or date delimiters), and if you ever upsize to a SQL platform, this method avoids SQL Injection problems in your action queries.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38740164
In Reviewing this post, I discovered I was looking at this from the wrong point of view
(SQL Insert)

So my syntax is valid, but not really specific for your situation (DCount)
:-(

Jeff
0
 

Author Closing Comment

by:gtmathewDallas
ID: 38740783
Thanks Fyed, Its really simple and a good solution, Thanks.

I want to thank you to all other experts who helped me in this issue...
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38741129
Glad to help.

I have another version of that function which allows you to identify the wrapper, so can choose a single quote (used with SQL Server pass through queries), double quote, or even # for wrapping numbers.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
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…

912 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