Solved

Return # of Records from Query

Posted on 2014-12-05
9
163 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

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.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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