Microsoft KB Archive/286242

= ACC2002: Unable to Use FindFirst to Retrieve Value with Apostrophe =

Article ID: 286242

Article Last Modified on 11/6/2003

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q286242



Moderate: Requires basic macro, coding, and interoperability skills.

For a Microsoft Access 2000 version of this article, see 199056.



SYMPTOMS
When you use the FindFirst method, you receive a &quot;No match&quot; or a run-time error message.

This behavior occurs when you search for a value that contains an apostrophe ('). For example:

rst.FindFirst &quot;[CompanyName] = 'B's Beverages'&quot;

This behavior also occurs if you use the FindFirst method in the AfterUpdate event procedure of a combo box, and you search for a value that contains an apostrophe. For example:

Me.RecordsetClone.FindFirst &quot;[CompanyName] = &quot; & Me![ComboboxNN]



CAUSE
Searches for text strings that contain quotation marks (&quot;) or apostrophes (') require special syntax because both characters are used as delimiters in Visual Basic for Applications.



Workaround for Using a Text String
When you are typing your search argument, use double quotation marks in your search string to find values that contain single quotation marks and use single quotation marks in your search string to find values that contain quotation marks. For example, to search for a CompanyName that includes an apostrophe, replace the single quotation marks around the CompanyName value with two sets of double quotation marks, as in the following example:

rst.FindFirst &quot;[CompanyName] = &quot;&quot;B's Beverages&quot;&quot;&quot;

Workaround for Using a Combo Box
If you are using a combo box to find values that contain quotation marks or apostrophes, add the primary key to the combo box and make it the bound column. Hide the bound column. Then change your criteria so that it searches in the field that corresponds to the bound column in the combo box.



Steps to Reproduce the Behavior
 Open the sample database Northwind.mdb. Use the AutoForm:Columnar Wizard to create a new form based on the Customers table. Open the form in Design view, and add a command button to the form. Change the Name property for the command button to FindFirst.  Set the OnClick property of the command button to the following event procedure: Private Sub FindFirst_Click

Dim rst As DAO.Recordset Dim strCriteria As String

Set rst = Me.RecordsetClone rst.FindFirst &quot;[CompanyName] = 'B''s Beverages'&quot;

If rst.NoMatch Then MsgBox &quot;No match was found.&quot;

Else

Me.Bookmark = rst.Bookmark

End If  rst.Close

End Sub </li> Close the Visual Basic Editor.</li> Switch the form to Form view, and then click the command button. Note that you receive a message box that contains the &quot;No match was found&quot; message.</li> Change the FindFirst syntax in the code example to the following:

rst.FindFirst &quot;[CompanyName] = &quot;&quot;B's Beverages&quot;&quot;&quot;

</li> Switch the form to Form view, and then click the command button. Note that this time the record is found.</li></ol>

<div class="references_section">