Solved

Navigate to a record using a combo box

Posted on 2011-10-01
7
403 Views
Last Modified: 2013-11-05
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?
0
Comment
Question by:SteveL13
  • 3
  • 3
7 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 36896508
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

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36896528
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.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 36898267
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
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:SteveL13
ID: 36903969
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

0
 

Author Comment

by:SteveL13
ID: 36903981
Sorry.. I meant type mismatch
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 36904102
Sorry...

Correct it like this:

rs.FindFirst "[PartN] = " & chr(34)  & Str(Nz(Me![Combo34], 0)) & chr(34)

Also, I believe the str() is unneeded:

rs.FindFirst "[PartN] = " & chr(34) &  Nz(Me![Combo34], 0) & chr(34)

Finally, is PartN text or numeric?  The above assume it is text.  If it is numeric, the syntax would be quite different:

rs.FindFirst "[PartN] = " &  Nz(Me![Combo34], 0)
0
 

Author Closing Comment

by:SteveL13
ID: 36932074
That did it.  Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now