Solved

Why won't findfirst find the required record

Posted on 2013-10-24
6
447 Views
Last Modified: 2013-10-25
I am running the following code and the findfirst step always finds the first record even though the value of lDealer does change from 1 to 2. It never finds rs!ID = 2 even though it does exist. What am I doing wrong? Id is an autonumber field and therefore should be long. I have tried it in both MS Access 2010 and 2007

sub mySub
    Static lDealer As Long
    Dim rs As DAO.Recordset
 
    Set rs = CurrentDb.OpenRecordset("Select Contacts.ID from Contacts order by id", dbOpenDynaset)
   
    If lDealer = 0 Then
        rs.MoveFirst
    Else
        rs.MoveFirst
       
        rs.FindFirst rs!ID = lDealer

        rs.MoveNext
        If rs.EOF Then
            rs.MoveFirst
        End If
    End If
   
    lDealer = rs!ID
    rs.Close
    Set rs = Nothing

end sub
0
Comment
Question by:Rob4077
6 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39596902
How are you calling this sub?

If it is only called once, the code will execute one time, find the record corresponding to the current lDealer and exit.

There is no looping in the sub itself to find another record, so unless you are somehow making multiple calls to the sub, it will stop at the first record.

What are you trying to do with the code?
0
 

Author Comment

by:Rob4077
ID: 39597198
The code is called by a timer loop.

When I test it the value of lDealer does increase but for some reason it never finds anything but the first record.

The reason I am using it is to display a rolling ToDo list. It is meant to find the first ID record, display a screen with all the records for that person and scroll through the list. When it gets to the end of the list it finds the next person and repeats the process. When it gets to the last person it starts again
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 39597230
I guess it should read:

Sub mySub
    Static lDealer As Long
    Dim rs As DAO.Recordset
 
    Set rs = CurrentDb.OpenRecordset("Select Contacts.ID from Contacts order by id", dbOpenDynaset)
   
    If lDealer = 0 Then
        rs.MoveFirst
    Else
        rs.FindFirst "ID = " & lDealer & ""
        rs.MoveNext
        If rs.EOF Then
            rs.MoveFirst
        End If
    End If
    lDealer = rs!ID
    rs.Close
    Set rs = Nothing

End Sub

/gustav
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39597447
Is ID a numeric or text value?  It sounds like it is numeric, but I'm not sure.  It is also not clear whether this is actually supposed move your focus in your form to the record that corresponds to the lDealer value on your form.  You also never actually set the value of lDealer in this subroutine.  If your intent is to enter a DealerID in a textbox, and cause the form to move to that dealers records, I would not normally use a timer event to perform this operation; I would use the txt_DealerIDFilter_AfterUpdate (usually in the form header) or a command button located next to that control.  Lets assume this is in a command button cmd_GotoRecord

Try this:

Sub cmd_GotoRecord_Click

    With me.Recordsetclone
   
        .FindFirst "ID = " & NZ(me.txt_DealerIDFilter, 0)

        if .NoMatch then
            msgbox "ID value not found"
            .MoveFirst
        Endif
        me.bookmark = .Bookmark

    End With

End Sub
0
 

Author Comment

by:Rob4077
ID: 39600095
Hi fyed, thanks for your suggestion but it's not quite what I am trying to do. Actually I have put together what I want to do, it's just the findfirst command that I couldn't get to work.

gustav your solution solved the problem. I didn't realise that the syntax of the command required the field and value being tested had to be inside double quotes. Thanks.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39600104
You are welcome!

/gustav
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
WebBrowser Control - ms/ACCESS 9 31
Access 2010 3 17
How hide #Error in a form field 2 10
Multiple queries for a form 14 20
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

707 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

12 Experts available now in Live!

Get 1:1 Help Now