Syntax Error (Missing Opeator) in Query Expression (1 Viewer)

JithuAccess

Member
Local time
Today, 05:26
Joined
Mar 3, 2020
Messages
297
Hello Guys,

I was trying to add the Values in a Combo Box on the fly. I have put the following code:



Private Sub Combo41_NotInList(NewData As String, Response As Integer)

Dim StrSQL As String
Dim i As Integer
Dim Msg As String

If NewData = "" Then Exit Sub

Msg = "'" & NewData & "' is not currently in the list." & vbCr & vbCr
Msg = Msg & "Do you want to add it?"

i = MsgBox(Msg, vbQuestion + vbYesNo, "Add New?")
If i = vbYes Then
StrSQL = "Insert Into [My Table] ([My Field]) " & _
"values ('" & NewData & "');"
CurrentDb.Execute StrSQL, dbFailOnError
Response = acDataErrAdded
Else
Response = acDataErrContinue
End If



End Sub



It was working fine. But if I add a Text like "Children's Hospital" I am getting an error like below:

1611869489364.png


The problem is with the Apostrophe in the Word Childrens'. Could you kindly let me know how to resolve this. Or do I have to avoid the Apostrophe ?

Thanks a lot
 

Attachments

  • 1611869433646.png
    1611869433646.png
    6.8 KB · Views: 561

Ranman256

Well-known member
Local time
Today, 07:26
Joined
Apr 9, 2015
Messages
4,337
use a query, it doesnt get syntax wrong.
you just run: docmd.openquery "qaInsertMyRec"

you used an apostropye in the name "children's" . you cant surround a quote with quotes.
you CAN use dbl quotes to surround single quotes.

but what if they enter a word with dbl-quote? You will need to replace them using REPLACE(word, oldChar, newChar)
THEN add the new word.

or just strip out all quotes before adding.
 

Isaac

Lifelong Learner
Local time
Today, 04:26
Joined
Mar 14, 2017
Messages
8,777
Change this line
Code:
"values ('" & NewData & "');"
to
Code:
"values ('" & replace(NewData,"'","''") & "');"
...God I hate how AWF code tags adds a bunch of crap to my quotation marks and parenthesis.

Hope I got that right.

Your problem is the syntax single quote. Every programming language has a way to 'escape' syntactical stuff. In SQL, it's to "double up"
 

JithuAccess

Member
Local time
Today, 05:26
Joined
Mar 3, 2020
Messages
297
Change this line
Code:
"values ('" & NewData & "');"
to
Code:
"values ('" & replace(NewData,"'","''") & "');"
...God I hate how AWF code tags adds a bunch of crap to my quotation marks and parenthesis.

Hope I got that right.

Your problem is the syntax single quote. Every programming language has a way to 'escape' syntactical stuff. In SQL, it's to "double up"
Million Thanks. It worked Perfectly (y)(y)(y)
 

Ranman256

Well-known member
Local time
Today, 07:26
Joined
Apr 9, 2015
Messages
4,337
i have a global constant Q to rep dbl-quotes

public const Q = """"

then its
sSql ="insert..... values(" & Q & txtbox & Q & ")"
 

JithuAccess

Member
Local time
Today, 05:26
Joined
Mar 3, 2020
Messages
297
use a query, it doesnt get syntax wrong.
you just run: docmd.openquery "qaInsertMyRec"

you used an apostropye in the name "children's" . you cant surround a quote with quotes.
you CAN use dbl quotes to surround single quotes.

but what if they enter a word with dbl-quote? You will need to replace them using REPLACE(word, oldChar, newChar)
THEN add the new word.

or just strip out all quotes before adding.
Thanks a lot for your time and Effort
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 12:26
Joined
Sep 12, 2006
Messages
15,653
I actually use chr(34) rather than a string of " characters. I find it much easier.
 

Users who are viewing this thread

Top Bottom