Results 1 to 14 of 14
  1. #1
    russdwright is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    30

    Angry Form using multiple queries

    I have an Access file that has 18 different queries I created, all of which have been created to get a count of records.

    I would like to setup a form that can be opened to see a "digest view" of all these numbers, but then clicking on the listed item would bring up the detailed report showing the breakdown of the count between each category.



    Everything I have read says to create sub-forms, but every sub-form I create makes the entire form look big and clunky when I only need a little space for each data point.

    Any and all input is greatly appreciated!
    Last edited by russdwright; 09-12-2018 at 12:37 PM. Reason: Said Excel by accident :)

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    You can get a recordcount for a query with this function:
    Code:
    Public Function qCount(arg As String) As Long
        Dim qd As DAO.Recordset
        Set qd = CurrentDb.OpenRecordset(arg)
        qCount = qd.RecordCount
    End Function
    Then
    Code:
    sub Form_Load()
         txtBox1 = qCount("qName1")
         txtBox2 = qCount("qName2")
         .
         .
         txtBox18 = qCount("qName18")
    end sub
    The doubleClick event on the textboxes could be coded to open the query or open a form that displays the query contents.

  3. #3
    russdwright is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    30
    Do I create a blank form, add the text boxes then input the code in VBA to run on the entire form?

    I don't want to use a DoubleClick event to show the counts; I'd rather have the summary appear immediately upon opening the form and have an option that, when selected, shows another set of information. I then want to setup code for when the heading for the query in question is clicked on, the report for that query loads to give more detailed information.

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    Do I create a blank form, add the text boxes then input the code in VBA to run on the entire form?
    Yes. The counts will appear when you open the form.

    The double click is to view any one of the 18 count details after the form shows the counts.

  5. #5
    russdwright is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    30
    OK, where do I place these functions in VBA? Do I put the Public Function in General and the Private Sub in the Form area?

    I've done this for a few of the fields under that assumption and they all show "1" as the result.

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    You can put both in the form area.
    they all show "1" as the result
    Is that the correct sum? If not, add the two red lines.
    Code:
    Public Function qCount(arg As String) As Long
        Dim qd As DAO.Recordset
        Set qd = CurrentDb.OpenRecordset(arg)
        qd.movelast
        qd.movefirst 
        qCount = qd.RecordCount
    End Function

  7. #7
    russdwright is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    30
    The change worked, but I am getting this when it runs (even though the counts successfully load):

    Code:
    Run-time error '3061':
    
    Too few parameters. Expected 1.
    I'm assuming I need to add something to the (arg) parenthetical, but I'm not sure what.

  8. #8
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    Check the query names for correct spelling that you are plugging in.

  9. #9
    russdwright is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    30
    The query and text box names are being copied directly from Access, so I have verified everything there

    The highlighted line when I attempt to Debug is Set qd = CurrentDb.OpenRecordset(arg)and data stops loading after the 3rd text box.

    I tried DCount instead and instead get a Run-time error ‘2471’. Again, when I debug, it is stopped at the third text box. I try using REM and every line beyond the second one brings back the error.

  10. #10
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    and data stops loading after the 3rd text box.
    Then there's something wrong about the 4th query. The error indicated is an error in the query itself.
    What happens when you try to run that query standalone?
    Here's a code mod that will handle queries that return zero records:
    Code:
    Public Function qCount(arg As String) As Long
        Dim qd As DAO.Recordset
        Dim db As DAO.Database
        Set db = CurrentDb
        Set qd = db.OpenRecordset(arg)
        On Error Resume Next
        qd.MoveLast
        qd.MoveFirst
        On Error GoTo 0
        qCount = qd.RecordCount
        Set qd = Nothing
    End Function

  11. #11
    russdwright is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    30
    I REM out everything except that query and the same thing happens. It also happens when I REM any other line except one, not including the first two queries.

    Again, the Debug points to this line: Set qd = CurrentDb.OpenRecordset(arg)

  12. #12
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    I REM out everything except that query and the same thing happens.
    Again, what happens when you run that query (and all the others) from the Navigation Pane? Does it run without errors and show you a datasheet of its results?

  13. #13
    russdwright is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    30
    OK, I double-checked the query in question and it turns out it was missing a field. I added the field and that resolved the issue.

  14. #14
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    and that resolved the issue.
    Good deal. The DCount function that you mentioned should work as well.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Fields from Multiple queries on one form
    By thecsmith70 in forum Forms
    Replies: 7
    Last Post: 09-25-2014, 11:13 AM
  2. Running multiple queries in form
    By daniejm in forum Forms
    Replies: 3
    Last Post: 01-02-2013, 08:45 AM
  3. Linking multiple queries to one form
    By Menno in forum Forms
    Replies: 5
    Last Post: 08-27-2012, 06:44 AM
  4. multiple queries in form
    By geraldk in forum Forms
    Replies: 5
    Last Post: 12-26-2011, 02:22 PM
  5. Form queries. Multiple checkboxes.
    By radink in forum Queries
    Replies: 4
    Last Post: 04-27-2011, 07:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums