By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,538 Members | 2,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,538 IT Pros & Developers. It's quick & easy.

Count query - how to use the result in VBA?

P: n/a
Hi All,

I have a query, Select Count(BoolField) from tblMyTable, Where BoolField =
true.

If I run the query by itself, it returns the number of true records
I want to use the result of that query in my VBA code, like this:

If (result of the query > 0) then
do something
end if

I don't want to have the query opened on the screen, it should run in the
background.

Maybe there is a different way of doing this. The data is stored into
tblMyTable, in BoolField. If I can count somehow the records who have that
value true and use the count value, that would be ok to me too. From what I
was reading on other postings, it could be done using RecordSet, but I am
not sure if I can do it better.

Regards,
Nicolae

Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I would use a recordset, or you could try using the DCount function.

--
Bob Darlington
Brisbane
"Nicolae Fieraru" <no****@please.com> wrote in message
news:40********@duster.adelaide.on.net...
Hi All,

I have a query, Select Count(BoolField) from tblMyTable, Where BoolField =
true.

If I run the query by itself, it returns the number of true records
I want to use the result of that query in my VBA code, like this:

If (result of the query > 0) then
do something
end if

I don't want to have the query opened on the screen, it should run in the
background.

Maybe there is a different way of doing this. The data is stored into
tblMyTable, in BoolField. If I can count somehow the records who have that
value true and use the count value, that would be ok to me too. From what I was reading on other postings, it could be done using RecordSet, but I am
not sure if I can do it better.

Regards,
Nicolae


Nov 13 '05 #2

P: n/a
Hi Chuck,

The "do something" is for opening a word document and merging the data from
another query with the word document.
If the query doesn't return any records, there is no point of opening the
word document and trying to merge inexistent data

Regards,
Nicolae
"Chuck Grimsby" <c.*******@worldnet.att.net.invalid> wrote in message
news:ee********************************@4ax.com...

Please expand on the "do something" part. What is it you are trying
to do?

If you want to, you can join the query with the count in it back to
the original query to just those who's count is greater then 1, or
just include those columns you want to work with in the count query.

On Mon, 12 Jul 2004 20:08:48 +1000, "Nicolae Fieraru"
<no****@please.com> wrote:
I have a query, Select Count(BoolField) from tblMyTable, Where BoolField =true.
If I run the query by itself, it returns the number of true records
I want to use the result of that query in my VBA code, like this:
If (result of the query > 0) then
do something
end if
I don't want to have the query opened on the screen, it should run in the
background.
Maybe there is a different way of doing this. The data is stored into
tblMyTable, in BoolField. If I can count somehow the records who have thatvalue true and use the count value, that would be ok to me too. From what Iwas reading on other postings, it could be done using RecordSet, but I am
not sure if I can do it better.

--
A Priest Gets Defrocked: Does A Sailor Get Disgusted?

Nov 13 '05 #3

P: n/a
Thank you Bob. I will look closer to the recordser and the DCount functiont,
never heard of that one(which isn't abnormal, considering my limited
experience with VBA :-) )

Regards,
Nicolae
Nov 13 '05 #4

P: n/a
DCount can be pretty slow (depending on the number of records to be
searched).
I use Trevor Best's solution (link below) which is a lot faster.
http://www.mvps.org/access/modules/mdl0012.htm
But creating the recordset yourself is not really so cumbersome once you get
used to it.
--
Bob Darlington
Brisbane
"Nicolae Fieraru" <no****@please.com> wrote in message
news:40******@duster.adelaide.on.net...
Thank you Bob. I will look closer to the recordser and the DCount functiont, never heard of that one(which isn't abnormal, considering my limited
experience with VBA :-) )

Regards,
Nicolae

Nov 13 '05 #5

P: n/a
Thank you to everybody for your help.
I managed to come with a simple solution, and that is:

I changed my Select Count query to a select query, which I tested
separately. It doesn't have parameters and that makes things even simpler

The VBA code is:

Dim db as Database
Dim rs as DAO.Recordset
dim qdf as QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("mySelectQuery")
Set rs = qdf.OpenRecordset()
if rs.EOF then
MsgBox "There are no records to be printed out"
else
' open the word document and mailmerge the records of a different query
end if

Regards,
Nicolae

Nov 13 '05 #6

P: n/a
Hi Chuck,

Thank you for the advice, I will keep that in mind.

Regards,
Nicolae

"Chuck Grimsby" <c.*******@worldnet.att.net.invalid> wrote in message
news:rg********************************@4ax.com...

In that case, open the recordset *before* you open the word document,
and look at the recordset's .count property. If it's not 0 then go
ahead and do the word merge. You really want to check for = 0 rather
then > 0 since Access will occasionally return -1 as the
recordset.count. (-1 = true meaning "Yes there are records".)

Personally, I prefer this method rather then running a count query and
then the "real" query, since it all happens in one simple procedure
and there's less to debug when something goes wrong. But there's no
question that running a count query works as well!

On Mon, 12 Jul 2004 23:54:22 +1000, "Nicolae Fieraru"
<no****@please.com> wrote:
The "do something" is for opening a word document and merging the data from
another query with the word document.
If the query doesn't return any records, there is no point of opening the
word document and trying to merge inexistent data

"Chuck Grimsby" <c.*******@worldnet.att.net.invalid> wrote in message
news:ee********************************@4ax.com.. .
Please expand on the "do something" part. What is it you are trying
to do?
If you want to, you can join the query with the count in it back to
the original query to just those who's count is greater then 1, or
just include those columns you want to work with in the count query.

On Mon, 12 Jul 2004 20:08:48 +1000, "Nicolae Fieraru"
<no****@please.com> wrote:
>I have a query, Select Count(BoolField) from tblMyTable, Where BoolField >= true.
>If I run the query by itself, it returns the number of true records
>I want to use the result of that query in my VBA code, like this:
>If (result of the query > 0) then
> do something
>end if
>I don't want to have the query opened on the screen, it should run in the >background.
>Maybe there is a different way of doing this. The data is stored into
>tblMyTable, in BoolField. If I can count somehow the records who have
>that value true and use the count value, that would be ok to me too. From what >I was reading on other postings, it could be done using RecordSet, but I am >not sure if I can do it better.

--
A Priest Gets Defrocked; Does A Ceo Get Downsized?

Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.