Find record problem

G

Guest

I’m using this code to find a record and it works OK if the record has no
single quote mark like Tiny’s bar or Mark’s place. If any record has the
quote mark I get a run time error 3077
Syntax error missing operator in expression.
Is their a way to fix this or do I have to change all my records?

Private Sub Combo5_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[Name] = '" & Me![Combo5] & "'"
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub
 
R

RoyVidar

Gus Chuch said:
I’m using this code to find a record and it works OK if the record
has no single quote mark like Tiny’s bar or Mark’s place. If any
record has the quote mark I get a run time error 3077
Syntax error missing operator in expression.
Is their a way to fix this or do I have to change all my records?

Private Sub Combo5_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[Name] = '" & Me![Combo5] & "'"
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub

Try one of the following

rs.FindFirst "[Name] = """ & Me![Combo5] & """"

or

rs.FindFirst "[Name] = '" & Replace(Me![Combo5], "'", "''") & "'"
 
M

Marshall Barton

Gus said:
I’m using this code to find a record and it works OK if the record has no
single quote mark like Tiny’s bar or Mark’s place. If any record has the
quote mark I get a run time error 3077
Syntax error missing operator in expression.
Is their a way to fix this or do I have to change all my records?

Private Sub Combo5_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[Name] = '" & Me![Combo5] & "'"
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub

The old quoted quote problem. If none of your records has a
double quote character, you can use:

rs.FindFirst "[Name] = """ & Me![Combo5] & """"

If that's not guaranteed, then you can use either:

rs.FindFirst "[Name] = '" & Replace(Me!Combo5, "'", "''") &
"'"
Be sure to count those double quotes and apostrophes
carefully.

This alternative uses only double quotes and might be easier
to count:

rs.FindFirst "[Name] = """ & Replace(Me!Combo5, """",
"""""") & """"
 

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

Top