Results 1 to 15 of 15

Thread: Access: Query Count

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    8

    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.

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

    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:
    1. 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 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
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Access: Query Count

    Being the database Guru that I am
    Hmm

    VB Code:
    1. 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:
    1. SELECT Count(*) As TotalRecs, Field1, Field2, Field3 FROM Table1 GROUP BY Field1, Field2, Field3

    or

    VB Code:
    1. 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:
    1. 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

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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.

    BOFH Now, BOFH Past, Information on duplicates

    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...

  5. #5
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    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

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

    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 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
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Access: Query Count

    Just Jesting...
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

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

    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 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
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Access: Query Count

    As I already quoted... Database Guru Indeed
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    8

    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.

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

    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 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

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    8

    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

  13. #13
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    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.

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

    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:
    1. Dim rs As Recordset
    2.     Set rs = Me.Recordset
    3.     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 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

  15. #15
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Access: Query Count

    Seeing as the recordset is opened in a cache.. even that may not return the complete results...

    try
    VB Code:
    1. Dim rs As Recordset
    2.   Set rs = Me.Recordset
    3.   Do While Not rs.EOF
    4.     rs.MoveLast
    5.     rs.MoveNext
    6.   Loop
    7.   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
  •  



Click Here to Expand Forum to Full Width