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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to handle an apostrophe inside an SQL statement? 3

Status
Not open for further replies.

steve728

Programmer
Mar 16, 2003
536
0
0
US
The following .Fields("Description").Value has several apostrophes and # symbols that must remain. How do I change my syntax so that I can insert these contents into
the destination field???

CurrentProject.Connection.Execute "INSERT INTO tblName (RequestNumber,[description]) Values (" & .Fields("RequestNumber").Value & ", '" & .Fields("Description").Value & "', '" & .Fields("RequestStatus").Value & "', '" _
)", , 129

Steve728
 
Replace this:
& .Fields("Description").Value &
with this:
Code:
& Replace(.Fields("Description").Value, "'", "''") &

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is the actual SQL statement that I am trying to work with. I made the change below but still get the same syntax run-time error. Can you tell me where I am still going wrong?

CurrentProject.Connection.Execute "INSERT INTO tblISRProblems (RequestNumber,[Type],RequestedBy," _
& "RequestDate,[area],[description],[status],AffectedGroup,[Comments],DateClosed) Values " _
& "( " & .Fields("RequestNumber").Value & ", '" & strThisType & "', '" & .Fields("RequestedBy").Value _
& "', #" & .Fields("RequestDate").Value & "#, '" & .Fields("AffectedGroup").Value & "', " _
& Replace(.Fields("Description").Value, "'", "''") & ", '" & .Fields("RequestStatus").Value & "', '" _
& .Fields("AffectedGroup").Value & "', '" & .Fields("Comments").Value _
& "', #" & .Fields("DateClosed").Value & "#)", , 129

Steve728
 
You probably have to do the same sanity checking for "Comments".
Anyway, you forgot to enclose the description value with single quotes:
& [!]"'" & [/!]Replace(.Fields("Description").Value, "'", "''") & "[!]'[/!], '" & .Fields("RequestStatus").Value & "', '" _

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Replace? that's a new one to me, thanks for that

my solution would have been:

CurrentProject.Connection.Execute "INSERT INTO tblName (RequestNumber,[description]) Values (" & .Fields("RequestNumber").Value & ", " & """"[/color red] & .Fields("Description").Value & """"[/color red] & ", '" & .Fields("RequestStatus").Value & "', '" _
)", , 129


 
Vacant, your suggestion will fail if the Description contains double-quote too ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I'm learning more by the minute! Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top