|
-
Feb 15th, 2005, 01:29 PM
#1
Thread Starter
Frenzied Member
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
-
Feb 15th, 2005, 01:34 PM
#2
Re: Identefying the last record...
You can do a loop like this.
VB Code:
rs.MoveFirst
Do While rs.EOF = False
'Do stuff
rs.MoveNext
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 15th, 2005, 01:46 PM
#3
Thread Starter
Frenzied Member
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?
-
Feb 15th, 2005, 02:08 PM
#4
Re: Identefying the last record...
Oops sorry I didnt realize that this was a table bound to your form.
VB Code:
Do While Me.CurrentRecord <= Me.MaxRecords
'Do stuff
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 15th, 2005, 02:10 PM
#5
Frenzied Member
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:
strSQL = "SELECT * FROM tblTable ORDER BY fldLName"
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
rs.MoveFirst
rs.MoveLast
MsgBox "Last record is: " & rs!fldFName & " " & rs!fldLname
Else
MsgBox "No records found"
End If
Tengo mas preguntas que contestas
-
Feb 15th, 2005, 02:35 PM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 15th, 2005, 02:42 PM
#7
Thread Starter
Frenzied Member
Re: Identefying the last record...
 Originally Posted by RobDog888
Oops sorry I didnt realize that this was a table bound to your form.
VB Code:
Do While Me.CurrentRecord <= Me.MaxRecords
'Do stuff
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.
-
Feb 15th, 2005, 02:49 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 15th, 2005, 02:53 PM
#9
Thread Starter
Frenzied Member
Re: Identefying the last record...
 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.
-
Feb 15th, 2005, 02:59 PM
#10
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:
strSQL = "SELECT Count(*) As MaxRecs FROM tblTable;"
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
MsgBox "Max Records is: " & rs!MaxRecs
Else
MsgBox "No records found"
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 15th, 2005, 03:04 PM
#11
Thread Starter
Frenzied Member
Re: Identefying the last record...
 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:
strSQL = "SELECT Count(*) As MaxRecs FROM tblTable;"
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
MsgBox "Max Records is: " & rs!MaxRecs
Else
MsgBox "No records found"
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|