Results 1 to 7 of 7
  1. #1
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87

    VBA: Run Query then Count number of Records then Display number on a form


    How do I go about doing this?

    I want to:
    Run a query, count the number of records that are in the query, and put that number in a sentence in a form

    For example a textbox that would say:
    # Cases have addition fees greater than $100

    Where # is the variable I'll be calculating using the process I said above.
    This sentence will appear on a form.
    Later oneI'll be creating a report that shows the actual "cases" but for now I just want to create the summary.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    21,712
    The control source of a textbox:

    =DCount("*", "QueryName") & " Cases have addition fees greater than $100"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    Where do you want to display the information, on a form? a report? in the query itself?

    If you have a summary query that shows the information you want

    let's say something like:

    Code:
    PersonID  Cases100
    1         5
    2         7
    You could just add this to your query:

    CaseSentence: [cases100] & " cases have addition fees greater than 100"

    domain functions (dsum, dcount, dlookup) can be useful but on large databases they an be very inefficient and I'd try to stay away from them if your database has the potential to be quite large.

  4. #4
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87
    Thanks for the responses.

    rpeare:
    My database has the potential of being quite large, but the query will run on small portions of data. Would dcount still be inefficient to use?

    pbaldy:
    I am trying to use the dcount function, but the problem I am having is that the query calls upon combo boxes from another form.


    For example:
    FORM1:
    user selects company, invoice type and invoice period
    FORM2:
    spits out the summary, i.e. the dcount function

    the query itself is just a select query which is filtered by what the user selects as company, invoice type and invocie period
    and has another criteria like svc_fee > 100

    So how do i get the dcount function to call on a query that uses combo boxes from another form?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    21,712
    You should still be able to run a DCount on the query, as long as the forms are open. If they're not, it will obviously error, as would the query (or more accurately it will throw parameter prompts).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87
    thanks so much got it to work

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    21,712
    No problem, glad we got it sorted out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. count the number of records from a query
    By Nixx1401 in forum Queries
    Replies: 4
    Last Post: 05-24-2011, 06:45 PM
  2. Replies: 11
    Last Post: 12-09-2010, 10:55 PM
  3. Replies: 3
    Last Post: 08-26-2010, 02:11 PM
  4. Replies: 7
    Last Post: 07-22-2010, 01:14 PM
  5. Replies: 3
    Last Post: 10-23-2009, 05:03 PM

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