Solved

Return # of Records from Query

Posted on 2014-12-05
9
162 Views
Last Modified: 2014-12-07
Hello,

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

Ernest
0
Comment
Question by:Ernest Grogg
  • 4
  • 2
  • 2
  • +1
9 Comments
 
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
0
 

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...
0
 
LVL 49

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
    rst.MoveLast
End If
lngRecordCount = rst.RecordCount
rst.Close
Set rst = Nothing
Set qdy = Nothing

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

/gustav
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 47

Assisted Solution

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

"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?
0
 

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 work...do I put this parameter on the button to show on this first idea...making the field visible?
0
 
LVL 49

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

/gustav
0
 
LVL 1

Accepted Solution

by:
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 & "#")
0
 

Author Comment

by:Ernest Grogg
ID: 40485448
OK....

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

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

Thanks!

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.

Thanks!
Everyone deserves the credit...
0
 

Author Closing Comment

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

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 5 54
How can I create a Message box thru a Macro action for yes / No? 26 59
Calculation in Access 5 26
Binding recordsets to a form 6 24
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

773 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