Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Navigate to a record using a combo box

I have a form which contains a combo box.  When the user selects a record from the combo box I want that record to be displayed on the form.  But I'm getting a type mismatch error.  Here's the code.

    Dim rs As Object

    Set rs = Me.Recordset.Clone
    rs.FindFirst "[PartN] = " & Str(Nz(Me![Combo34], 0))
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark

PartN is a text field by the way.  What is wrong with the code?
Avatar of mbizup
mbizup
Flag of Kazakhstan image

You need to use quotes to delimit text:

  Set rs = Me.Recordset.Clone
    rs.FindFirst "[PartN] = " & chr(34) &  & Str(Nz(Me![Combo34], 0)) & chr(34)
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark

Chr(34) are double quotes.

Single quotes also work:

Set rs = Me.Recordset.Clone
    rs.FindFirst "[PartN] = '"  & Str(Nz(Me![Combo34], 0)) & "'"
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark



But they will cause errors if your data contains single quotes ('), such as in names like O'Brien.
And I would recommend using the rs.nomatch method to determine whether there was a match.

Set rs = me.recordsetclone
Rs.findfirst "[PartN] = '" & nz(me.combo34,0) & """"
If rs.nomatch = false then me.bookmark = rs.bookmark
Avatar of SteveL13

ASKER

When I copy/paste this code I get red copy on the 2nd line...


  Set rs = Me.Recordset.Clone
    rs.FindFirst "[PartN] = " & chr(34) &  & Str(Nz(Me![Combo34], 0)) & chr(34)
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark

Sorry.. I meant type mismatch
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did it.  Thanks.