Solved

Why won't findfirst find the required record

Posted on 2013-10-24
6
485 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 50

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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 50

Expert Comment

by:Gustav Brock
ID: 39600104
You are welcome!

/gustav
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

679 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