Results 1 to 11 of 11

Thread: Identefying the last record...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Identefying the last record...

    Hey,
    I am working with Access VBA and looping through a table. how would I get out of the loop on the last record?

    Thanks

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Identefying the last record...

    You can do a loop like this.
    VB Code:
    1. rs.MoveFirst
    2. Do While rs.EOF = False
    3.     'Do stuff
    4.     rs.MoveNext
    5. Loop
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Identefying the last record...

    ok I would have to dim rs to a recordset and set it to the table? Or do I have to do all that seeing the form was made from the table using the wizard?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Identefying the last record...

    Oops sorry I didnt realize that this was a table bound to your form.
    VB Code:
    1. Do While Me.CurrentRecord <= Me.MaxRecords
    2.         'Do stuff
    3.     Loop
    Note: if you are not on the first records then you will not be iterating through the entire table.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Identefying the last record...

    If you leave the navigation buttons on the form, just click the >>| symbol down where it reads Record 1 of 650. That will take you to the last record.
    But there's no guarantee that the data in a table will be sorted, so the last record may not be the one you think it would be. You could base your form on a query that has an ORDER BY clause to do that.
    As an alternative to RobDog's method, if you just want to find the last record, you could do something like:
    VB Code:
    1. strSQL = "SELECT * FROM tblTable ORDER BY fldLName"
    2. Set rs = db.OpenRecordset(strSQL)
    3. If rs.RecordCount > 0 Then
    4.    rs.MoveFirst
    5.    rs.MoveLast
    6.    MsgBox "Last record is: " & rs!fldFName & " " & rs!fldLname
    7. Else
    8.    MsgBox "No records found"
    9. End If
    Tengo mas preguntas que contestas

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Identefying the last record...

    Correct. But you could place sorting on the forms property. Then when you go to the last record using
    the nav buttons it will be the last record according to the sorting. Conversly, if you had sorting on the
    form, it would be different then the table so you wouldnt be on the last record. I think thats is also
    what you said.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Identefying the last record...

    Quote Originally Posted by RobDog888
    Oops sorry I didnt realize that this was a table bound to your form.
    VB Code:
    1. Do While Me.CurrentRecord <= Me.MaxRecords
    2.         'Do stuff
    3.     Loop
    Note: if you are not on the first records then you will not be iterating through the entire table.

    HTH

    Tried this and got the following error:

    You have entereed an expression that has and invalid reference to the property MaxRecords.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Identefying the last record...

    Did you place the code in the Forms code module?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Identefying the last record...

    Quote Originally Posted by RobDog888
    Did you place the code in the Forms code module?
    yeah it is returning a value when using me.CurrentRecord but me.MaxRecords is getting the error.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Identefying the last record...

    What version of Access are you running? Maybe its not supported in your version.
    I am running 2003. You may need to do like salvelinus posted in order to get
    the max number of records or like this to get the max records no matter what
    the sorting is.

    VB Code:
    1. strSQL = "SELECT Count(*) As MaxRecs FROM tblTable;"
    2. Set rs = db.OpenRecordset(strSQL)
    3. If rs.RecordCount > 0 Then
    4.    MsgBox "Max Records is: " & rs!MaxRecs
    5. Else
    6.    MsgBox "No records found"
    7. End If
    Yea, 8000 posts
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Identefying the last record...

    Quote Originally Posted by RobDog888
    What version of Access are you running? Maybe its not supported in your version.
    I am running 2003. You may need to do like salvelinus posted in order to get
    the max number of records or like this to get the max records no matter what
    the sorting is.

    VB Code:
    1. strSQL = "SELECT Count(*) As MaxRecs FROM tblTable;"
    2. Set rs = db.OpenRecordset(strSQL)
    3. If rs.RecordCount > 0 Then
    4.    MsgBox "Max Records is: " & rs!MaxRecs
    5. Else
    6.    MsgBox "No records found"
    7. End If
    Yea, 8000 posts

    I am using 2k3 also... strange maybe I will try updating will also try salvelinus' code.

    grats on 8k

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width