Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Runtime error 3075 - Syntax error in query expression 3

Status
Not open for further replies.

micang

Technical User
Aug 9, 2006
626
US
Access 2003

Hi All,

I am running the following code - it inserts data from a combobox and a textbox into a table.

Code:
Dim pid as Integer
Dim infotodo as String

pid = combopid.Value

infotodo = txtboxtodo.Value

DoCmd.RunSQL "insert into info_todo(Priority,info)values ('" & pid & "','" & infotodo & "')"

I get a "Runtime error 3075 - Syntax error in query expression" error if the txtbox(infotodo) has an apostrophe(') in the txt. I works fine if there is no apostrophe, but as soon as there is one, this error occurs.

eg: "Hello, you are great!" - works fine, but
"Hello, you're great!" - does not work.

I am really stumped and would appreciate any info in the right direction.

Thank you.

Michael

 
I had the same problem with a look-up combo box with last names; e.g., D'Lorenzo.

Solution was to create a module with the following code:
Public Function fHandleApostrophe(strPass As String) As String
Dim strRet As String

strRet = strPass
If InStr(1, strRet, "'", vbTextCompare) > 0 Then
strRet = "'" & Replace(strRet, "'", "''", , , vbTextCompare) & "'"
Else
strRet = "'" & strRet & "'"
End If
fHandleApostrophe = strRet

End Function


Hope this helps.
 
MADDPM, thank you very much, work's 100%, appreciate your prompt assistance.

Michael
 
why not simply this ?
Code:
DoCmd.RunSQL "insert into info_todo(Priority,info)values ('" & Me!combopid & "','" & Replace(Me!txtboxtodo, "'", "''") & "')"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV,a great solution.

Michael
 
How are ya micanguk . . .

If you want to keep apostrophe's in your text fields, construct [blue]substrings[/blue] instead:
Code:
[blue]   Dim DQ as String

   DQ = """"

   DoCmd.RunSQL "INSERT INTO info_todo (Priority, info) " & _
	            "VALUES (" & [purple][b]DQ[/b][/purple] & Me!combopid & [purple][b]DQ[/b][/purple] & "," & [purple][b]DQ[/b][/purple] & Me!txtboxtodo & [purple][b]DQ[/b][/purple] & ")"[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Hi TheAceMan1,

Awesome stuff, thanks to you and all for your solutions, appreciate it.

Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top