Return # of Records from Query

Posted on 2014-12-05
Last Modified: 2014-12-07

I have a form that has
2 fields

Date Field
Combo Box

I have a query that has these two fields with this in the two fields respectively:

[Forms]![Copy Of QRY_MG_3055-1]![Inspected]
[Forms]![Copy Of QRY_MG_3055-1]![DateEntered]

I have a button to click Run Query

I want a field on my form to return the total number of Records the Query comes up with.

right now  the query comes up and I don't want that anymore....I just want a field to be hidden and then visible when the button is  clicked to reveal the # of records.

Any help is appreciated...

Question by:Ernest Grogg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 250 total points
ID: 40484108
Create an unbound control (txt_RecCount) on your form.  Set it's Visible property to No (and that of the label that goes with it).

In the buttons click event, use:

Private Sub cmd_QueryCount_Click

    me.txt_RecCount = DCount("*", "yourQueryName")
    me.lbl_RecCount.Visible = true
    me.txt_RecCount.Visible = true

End if

Author Comment

by:Ernest Grogg
ID: 40484125
Seems to run, but I am getting zero Records?

I know that there is records, I can't figure out why

The date field may be wrong because if I do this manually it works.

How should the date field look like unbound?  I want the date picker to come up...
LVL 50

Assisted Solution

by:Gustav Brock
Gustav Brock earned 125 total points
ID: 40484475
That's because run this way, the query has no idea of what [Forms]![Copy Of QRY_MG_3055-1]![Inspected] and
 [Forms]![Copy Of QRY_MG_3055-1]![DateEntered] are.

You could do like this:

Dim qdy As DAO.QueryDef
Dim rst As DAO.Recordset
Dim lngInspected As Long
Dim datDateEntered As Date
Dim lngRecordCount As Long

lngInspected = Val([Forms]![Copy Of QRY_MG_3055-1]![Inspected])
datDateEntered = DateValue([Forms]![Copy Of QRY_MG_3055-1]![DateEntered])

Set qdy = CurrentDb.QueryDefs("NameOfYourQuery")
qdy.Parameters(0).Value = lngInspected
qdy.Parameters(1).Value = datDateEntered
Set rst = qdy.OpenRecordset()
If rst.RecordCount > 0 Then
End If
lngRecordCount = rst.RecordCount
Set rst = Nothing
Set qdy = Nothing

Me!txtRecordCount.Value = lngRecordCount
Me!txtRecordCount.Visible = True

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 250 total points
ID: 40484850

"That's because run this way, the query has no idea of what [Forms]![Copy Of QRY_MG_3055-1]![Inspected] and [Forms]![Copy Of QRY_MG_3055-1]![DateEntered] are."

Do you mean when run via a DCount("*", "queryName")?

As long as those form fields are declared as parameters in the saved query, the DCount( ) domain function should work.

What is the SQL of the query?

Author Comment

by:Ernest Grogg
ID: 40484913
I can say I had those fields on the SQL declared as I listed them respectively...I was trying Gustavs but still trying to get it to work, but nothing ...same thing zero records ....

Perhaps DCount will I put this parameter on the button to show on this first idea...making the field visible?
LVL 50

Expert Comment

by:Gustav Brock
ID: 40484918
You may need to reverse the sequence of the parameters:

 qdy.Parameters(1).Value = lngInspected
 qdy.Parameters(0).Value = datDateEntered


Accepted Solution

Declan_Basile earned 125 total points
ID: 40485179
"I have a form that has
 2 fields

 Date Field
 Combo Box

 I have a query that has these two fields with this in the two fields respectively:

 [Forms]![Copy Of QRY_MG_3055-1]![Inspected]
 [Forms]![Copy Of QRY_MG_3055-1]![DateEntered]

 I have a button to click Run Query

Do you mean that you have a form named "Copy Of QRY_MG_3055-1" that has two controls named "Inspected" (which is a combobox) and "DateEntered" (which is a textbox), and that you have a query in which you selected two fields of a table to output, and you have " [Forms]![Copy Of QRY_MG_3055-1]![Inspected]" set as the criteria for one field and " [Forms]![Copy Of QRY_MG_3055-1]![DateEntered]" set as the criteria for the other field?  Also, you have a button on the form labeled "Run Query" that used to open the query when it was clicked, but now you want the number of records in the query to display on the form when the button is clicked, correct?  If so, are the controls on the form bound or unbound?

If I understand correctly how your program is set up and what you are asking, I'd first run the query after entering values in the form to confirm that the query results have at least one row.  If so, the DCount("*", "queryname") should work just fine.  I do that sort of thing all the time.  You can also specify your criteria in code and DCount records in the base table.  Ex. DCount("*", "tablename", "Field1 = " & Me!Inspected & " And Field2 = #" & Me!DateEntered & "#")

Author Comment

by:Ernest Grogg
ID: 40485448

So this morning I finally felt a little clearer on reading everything here.

Here is what I have:

1 Form with 3 fields

Field 1: DateEntered (with date picker)
Field 2: ComboBox (using a list, yes, or no)
Field 3: TextBox (unbound)

Then I have a

Command Button...

Here is what I finally came up with using everyone's ideas:

Private Sub RunQry_Click()
If DCount("Inspected", "qry_MG3055-1") = 0 Then

MsgBox "there are no files for that day, try again"
   Exit Sub

  Me.txt_RecCount = DCount("Inspected", "qry_MG3055-1")
  Me.lbl_RecCount.Visible = True
  Me.txt_RecCount.Visible = True
End If
End Sub

It finally works just as I have wanted.  What I am understanding from the comments above is that that query will run first based off of the fields on the form and output the data on the unbound textbox I have hidden and unhide that if there is data in the query.

On the query I have the original
[Forms]![Copy Of QRY_MG_3055-1]![Inspected]
[Forms]![Copy Of QRY_MG_3055-1]![DateEntered]

listed in each field as appropriate.  That way the data is being pulled from the form.


Later I will be adding the report and I will output the total # of Records based on the selection on a report that lists the records.

Everyone deserves the credit...

Author Closing Comment

by:Ernest Grogg
ID: 40485452
Great work and fantastic in asking questions and getting me to think through more...

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question