PDA

View Full Version : Solved: Problem with apostrophes when using FindFirst



vikingfreddy
02-19-2010, 09:24 AM
Hi, hope someone can help. I am using to following code to select a record from a combo box:

Private Sub cboFindName_AfterUpdate()

' Find the record that matches the control.
Dim rs As Object

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

End Sub

This works fine until there is an apostrophe in the record when it fails with:

Runtime error '3077': Syntax error (missing operator) in expression.

This is obviously due to using single quotes after [Name] = and then putting the closing single quote inside a pair of double quotes after the [cboFindName], the apostrophe in the record is being read as the closing quote. I am totally lost as to what sequence of quotes to use so that this error doesn't occur.
Can anyone point me in the right direction?

OBP
02-20-2010, 05:27 AM
You could try using Double quotes as in
rs.FindFirst "[Name] = """ & Me![cboFindName] & """"
Or possibly
rs.FindFirst "[Name] = " & Chr(32) & Me![cboFindName] & Chr(32)

vikingfreddy
02-20-2010, 07:02 AM
You could try using Double quotes as in
rs.FindFirst "[Name] = """ & Me![cboFindName] & """"
Or possibly
rs.FindFirst "[Name] = " & Chr(32) & Me![cboFindName] & Chr(32)

Many, many thanks for your help. The double quotes option you gave works perfectly. I had been going round in circles trying all sorts of combinations of quotes; when you see the solution it is a real 'Homer' moment: "doh!"