combo box issue

S

shank

I have a combo box on a form to find the company I'd like to display on that
form for editing. The problem is when the company has an apostrophe in the
name. It causes a script error. What edits are needed on the below code to
make the combo box work with apostrophes?

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

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

thanks!
 
J

John Vinson

I have a combo box on a form to find the company I'd like to display on that
form for editing. The problem is when the company has an apostrophe in the
name. It causes a script error. What edits are needed on the below code to
make the combo box work with apostrophes?

Use " to delimit the string rather than ': to insert a doublequote
into a string delimited by doublequotes, double the doublequote (how's
that for doubletalk!):

rs.FindFirst "[Company] = """ & Me![Combo62] & """"

Alternatively - and you'll need to do this if the Company name might
have " in it, such as Joe's "Good Times" Bar - you can use the Replace
function to replace each ' by two consecutive ' marks, which will
search correctly:

rs.FindFirst "[Company] = '" & Replace(Me![Combo62],"'", "''") & "'"


John W. Vinson[MVP]
 

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