Solved

Return # of Records from Query

Posted on 2014-12-05
9
161 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now