Solved

Why won't findfirst find the required record

Posted on 2013-10-24
6
490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

734 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