Dcount on SQL query

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am curious about using the Dcount function on a query that isn't "stored"
and named...
the query runs in the AfterUpdate of a textbox... the user enters the search
criteria and the results are returned on a subform. this works fine.
now, how to count the records in the results that have a CategoryID=1?

when I try to use the DCount function, what do I use as the domain??

....Dcount("[CategoryID]", "WHAT HERE???", "[CategoryID]=1")
 
DCount will work only for stored tables and stored queries. You likely will
need to open a recordset based on the desired SQL string, move to the last
record, and then get the .RecordCount value.
 
Hi,
What do you mean with isn't "stored" and named? if you have only sql - then
you can reate w a new querydef, store a query and then run dcount. or better
open recordset, and pass sql with count there
 
Thanks Ken... I will make another attempt with your advice in mind.

Ken Snell said:
DCount will work only for stored tables and stored queries. You likely will
need to open a recordset based on the desired SQL string, move to the last
record, and then get the .RecordCount value.

--

Ken Snell
<MS ACCESS MVP>

Matt K. said:
I am curious about using the Dcount function on a query that isn't "stored"
and named...
the query runs in the AfterUpdate of a textbox... the user enters the
search
criteria and the results are returned on a subform. this works fine.
now, how to count the records in the results that have a CategoryID=1?

when I try to use the DCount function, what do I use as the domain??

...Dcount("[CategoryID]", "WHAT HERE???", "[CategoryID]=1")
 
To get a count of the records in your subform's RecordSource, just add a
footer to the subform and add a control with the following ControlSource:
=Count(*)

The footer doesn't need to be visible on the main form if it will look
awkward. You can get the value of this count field and display it on the
mainform as follows:
=subformname.Form.controlname

No code, no query :-)
 
the user enters the search
criteria and the results are returned on a subform. this works fine.
now, how to count the records in the results that have a CategoryID=1?

if you still have access to the original SQL, then it's not too hard to
wrap it in a count():


Set rst = CurrentDb().OpenRecordset("select count(*) from (" & _
mySource & _
") where CategoryID=1", _
dbOpenSnapshot, dbForwardOnly)

Debug.Print rst.Fields(0).Value
rst.Close

Otherwise, you'll just have to hack into

me.controls("mysubform").form.recordsetclone

and apply a .Filter; and then .moveLast it and read its .RecordCount, or
something like that.

If you have access to the basic search criteria, I think I'd build a
proper DCount() criterion string or, at worst, a whole SQL COUNT() query.

Hope that helps

Tim F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Access Dcount (multiple criteria) 3
Access Dcount function in access 0
Access Control on subform / combo box requesting parameter 0
Access MS Access DCount function problem 0
Dcount operation 2
Code breaking with no breakpoint 2
DCount problem 12
DCount alwaysreturning 1 0

Back
Top