How to Handle Apostrophe?

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
 
K

Ken Snell [MVP]

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) & "'"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top