Solved

Why won't findfirst find the required record

Posted on 2013-10-24
6
458 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

920 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

18 Experts available now in Live!

Get 1:1 Help Now