Connecting Tech Pros Worldwide Forums | Help | Site Map

Count query - how to use the result in VBA?

Nicolae Fieraru
Guest
 
Posts: n/a
#1: Nov 13 '05
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





Bob Darlington
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Count query - how to use the result in VBA?


I would use a recordset, or you could try using the DCount function.

--
Bob Darlington
Brisbane
"Nicolae Fieraru" <nospam@please.com> wrote in message
news:40f26332$1@duster.adelaide.on.net...[color=blue]
> 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[/color]
I[color=blue]
> was reading on other postings, it could be done using RecordSet, but I am
> not sure if I can do it better.
>
> Regards,
> Nicolae
>
>
>
>
>[/color]


Nicolae Fieraru
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Count query - how to use the result in VBA?


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.grimsby@worldnet.att.net.invalid> wrote in message
news:ee25f0pjf5op9hbvel2ih1ino6sk2h2l5b@4ax.com...[color=blue]
>
> 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"
> <nospam@please.com> wrote:[color=green]
> >I have a query, Select Count(BoolField) from tblMyTable, Where BoolField[/color][/color]
=[color=blue][color=green]
> >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[/color][/color]
that[color=blue][color=green]
> >value true and use the count value, that would be ok to me too. From what[/color][/color]
I[color=blue][color=green]
> >was reading on other postings, it could be done using RecordSet, but I am
> >not sure if I can do it better.[/color]
>
>
> --
> A Priest Gets Defrocked: Does A Sailor Get Disgusted?
>[/color]


Nicolae Fieraru
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Count query - how to use the result in VBA?


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


Bob Darlington
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Count query - how to use the result in VBA?


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" <nospam@please.com> wrote in message
news:40f2988d@duster.adelaide.on.net...[color=blue]
> Thank you Bob. I will look closer to the recordser and the DCount[/color]
functiont,[color=blue]
> never heard of that one(which isn't abnormal, considering my limited
> experience with VBA :-) )
>
> Regards,
> Nicolae
>
>[/color]


Nicolae Fieraru
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Count query - how to use the result in VBA?


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



Nicolae Fieraru
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Count query - how to use the result in VBA?


Hi Chuck,

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

Regards,
Nicolae

"Chuck Grimsby" <c.grimsby@worldnet.att.net.invalid> wrote in message
news:rgl7f01hgtkj7s0tm06av23ju4m37anrp3@4ax.com...[color=blue]
>
> 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"
> <nospam@please.com> wrote:[color=green]
> >The "do something" is for opening a word document and merging the data[/color][/color]
from[color=blue][color=green]
> >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[/color]
>[color=green]
> >"Chuck Grimsby" <c.grimsby@worldnet.att.net.invalid> wrote in message
> >news:ee25f0pjf5op9hbvel2ih1ino6sk2h2l5b@4ax.com.. .[color=darkred]
> >> 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.[/color][/color]
>[color=green][color=darkred]
> >> On Mon, 12 Jul 2004 20:08:48 +1000, "Nicolae Fieraru"
> >> <nospam@please.com> wrote:
> >> >I have a query, Select Count(BoolField) from tblMyTable, Where[/color][/color][/color]
BoolField[color=blue][color=green][color=darkred]
> >> >= 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[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> >> >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.[/color][/color][/color]
From what[color=blue][color=green][color=darkred]
> >> >I was reading on other postings, it could be done using RecordSet, but[/color][/color][/color]
I am[color=blue][color=green][color=darkred]
> >> >not sure if I can do it better.[/color][/color]
>
>
> --
> A Priest Gets Defrocked; Does A Ceo Get Downsized?
>[/color]


Closed Thread