How to Handle Apostrophe?

  • Thread starter Thread starter bw
  • Start date Start date
B

bw

When CoName has an apostrophe in it like "MyCompany's Name", the procedure
below produces the following error:
Syntax errror (missing operator) in query expression
'[CoName]='MyCompany's Name'".
Note in the error message that I have copied the single and double quotes
acurately.)

Anyone have a fix for this problem?
Thanks,
Bernie

Private Sub AllAddresses_Click()
On Error GoTo Err_AllAddresses_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Addresses"
'Activate the next line to Open with the same name
stLinkCriteria = "[CoName]=" & "'" & Me![CoName] & "'"
MsgBox stLinkCriteria
DoCmd.Close
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_AllAddresses_Click:
Exit Sub
Err_AllAddresses_Click:
MsgBox Err.Description
Resume Exit_AllAddresses_Click
End Sub
 
Use the Replace function to double-up the ' characters -- ACCESS reads '' as
' when embedded in a text string:

stLinkCriteria = "[CoName]=" & "'" & Replace(Me![CoName], "'", "''", 1, -1,
vbTextCompare) & "'"
 
Back
Top