Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Return # of Records from Query

Posted on 2014-12-05
9
Medium Priority
?
175 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
[X]
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
9 Comments
 
LVL 48

Assisted Solution

by:Dale Fye
Dale Fye earned 1000 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 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 500 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 48

Assisted Solution

by:Dale Fye
Dale Fye earned 1000 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 52

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

636 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