|
-
Jul 26th, 2005, 05:37 PM
#1
Thread Starter
New Member
Access: Query Count
I am trying to find a command that will allow me to be able to provide a count of the number of results returned from a query in Access. I can't, however, figure out what to place a counter on.
Thanks.
-
Jul 26th, 2005, 10:58 PM
#2
Re: Access: Query Count
Being the database Guru that I am (sarcasism ) I believe if your passing or executing a SQL statement query you can add a aliased field with an agregrate function "Count"
VB Code:
SELECT Count(*) As TotalRecs, Field1, Field2, Field3 FROM Table1
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 
-
Jul 27th, 2005, 01:57 AM
#3
Re: Access: Query Count
Being the database Guru that I am
Hmm
VB Code:
SELECT Count(*) As TotalRecs, Field1, Field2, Field3 FROM Table1
Access won't like this as it doesn't know what to do with field1 - field3
You either need to group the remaining fields, or just use the count..
VB Code:
SELECT Count(*) As TotalRecs, Field1, Field2, Field3 FROM Table1 GROUP BY Field1, Field2, Field3
or
VB Code:
SELECT Count(*) As TotalRecs FROM Table1
This first query will return a count per grouping, the second query will return a count of all records..
Now The stinger..
VB Code:
SELECT DCount("Field1","Table1") AS TotalRecs, Field1, Field2, Field3 FROM Table1
Will return a count of all records and each of the fields without the grouping..
Database Guru Indeed
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Jul 27th, 2005, 09:34 AM
#4
Re: Access: Query Count
Stay away from dcount - v slow.... 
well.. I guess you'd have to do some tests, but as the tables grow it slows rapidly (so I found).
Use it if you wish it is you who are the developer and it is your choice.
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jul 27th, 2005, 10:06 AM
#5
Re: Access: Query Count
Not one I would use normally, just an option...
It was a dig at robdog
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Jul 27th, 2005, 11:26 AM
#6
Re: Access: Query Count
I missed the GROUP BY. 
Its been a tough week already with everybody picking on me.
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 
-
Jul 27th, 2005, 11:27 AM
#7
Re: Access: Query Count
Just Jesting...
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Jul 27th, 2005, 11:34 AM
#8
Re: Access: Query Count
I know (smilie indicators ) Its hard being at the top you know.
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 
-
Jul 27th, 2005, 11:43 AM
#9
-
Aug 2nd, 2005, 05:24 PM
#10
Thread Starter
New Member
Re: Access: Query Count
Hey, I appreciate all the responses, but I have still been unable to get this working. What I'm Trying to do is fill in a textbox with " X records returned".
The closest that I have been able to get is a count of the objects on the form!? Could you please provide a more detailed example - I am specifically having problems with the Count(*) parameters and confused because TotalRecs doesn't appear in the option list.
Thanks.
-
Aug 2nd, 2005, 05:42 PM
#11
Re: Access: Query Count
How are you executing this query and from where?
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 
-
Aug 3rd, 2005, 09:18 AM
#12
Thread Starter
New Member
Re: Access: Query Count
I am executing the query from a form. By having a user press a search button once they have completed search fields of their choice. The results are then displayed on a subform which is contained in the main form. The query code behind the search button is as follows:
Me.subForm1.Form.Filter = "Field1 like '" & Me.txtfield1 & "' " & "and Field2 like '" & Me.txtfield2 & "' "
Me.subForm1.Form.FilterOn = True
Me.subForm1.Requery
-
Aug 3rd, 2005, 12:04 PM
#13
Frenzied Member
Re: Access: Query Count
I am not sure of the exact code but you could probably get away with moving to the last record of the recordset and I think it is me.currentrecord that would return the number.
-
Aug 3rd, 2005, 12:52 PM
#14
Re: Access: Query Count
Yes, .CurrentRecord will get the recordcount if on the last record but will be inacurate if the recordset allows new rows to be added. If it does then the .CurrentRecord will off by one if its at the EOF. 
The most reliable and accurate way is to use the .RecordCount property of the recordset that is bound to the form.
VB Code:
Dim rs As Recordset
Set rs = Me.Recordset
MsgBox rs.RecordCount
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 
-
Aug 3rd, 2005, 01:08 PM
#15
Re: Access: Query Count
Seeing as the recordset is opened in a cache.. even that may not return the complete results...
try
VB Code:
Dim rs As Recordset
Set rs = Me.Recordset
Do While Not rs.EOF
rs.MoveLast
rs.MoveNext
Loop
MsgBox rs.RecordCount
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
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
|