473,770 Members | 5,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Count query - how to use the result in VBA?

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
6 11502
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.adelaid e.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
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.*******@worl dnet.att.net.in valid> wrote in message
news:ee******** *************** *********@4ax.c om...

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
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
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******@d uster.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
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("m ySelectQuery")
Set rs = qdf.OpenRecords et()
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
Hi Chuck,

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

Regards,
Nicolae

"Chuck Grimsby" <c.*******@worl dnet.att.net.in valid> wrote in message
news:rg******** *************** *********@4ax.c om...

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.*******@worl dnet.att.net.in valid> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2417
by: Lin Ma | last post by:
Greetings, In my search application, user can type a number to search. I use LIKE in my query. If a query result generates over 10,000 recordsets, it may several minutes to run. Is there a way to only query certain recordset at a time?
2
4500
by: Wei Wang | last post by:
Hi, I want to do a select in dynamic command, something like: TRIGGER FUNCTION DECLARE table_name_suffix text; temp_result RECORD; temp_result2 RECORD;
2
3122
by: Martin Sarsale | last post by:
Dear All: Im looking for solutions (Free Software is better) to do query result caching. Thanks to the people from #postgresql I know that postgres doesn't do that by himself and the solution should be some kind of middle ware. This is our problem: we are executing the same set of SELECT SP/queries hundreds of times, without updating the DB. We would like to cache the result of this SP/queries so the next time somebody runs one of...
12
22793
by: briansmccabe | last post by:
Does anyone have a good approach to displaying in PHP a simple COUNT query that is performed on a table in a MySQL db? Thanks
6
5075
by: dBNovice | last post by:
Hey group, I am trying to do a count of the number of papers in a table. The table has a PaperID that differentiates each paper , e.g. 004.1. Some papers are reused. The reused paper is given a new PaperID. The PaperID includes 3 new numbers appended to the original PaperID, e.g. 664.004.1. When I do a count, I do not want to count the reused paper. I set up a count query and had the criteria { Not Like "***.***.*" }. I have also
1
2595
by: RookieDan | last post by:
Greetings fellow Accessers! Im new but in Access, but I have some background in different coding. I have a programme loading customer data into Access belonging to BMW dealers in Europe. Every dealer reports several customers and I have today a query that sorts out how many customer data each BMW dealer sends in to us. The query is also referring to a startdate and enddate (to be filled in in a messagebox) so that i can choose time...
3
4876
WyvsEyeView
by: WyvsEyeView | last post by:
This seems like it should be so easy to do. I have a table, called tblTopics. Each topic can have one or more instances, contained in a table called tblTopicInst. tblTopics is bound to a form called frmTopics and tblTopicInst is bound to a form called frmTopicInst. From frmTopics, you access frmTopicInst via a button. I have also created a query, qryCountInst, that counts the number of instances for the topic currently displayed on frmTopics. I...
1
1757
ddtpmyra
by: ddtpmyra | last post by:
how can I capture the query result in PHP? I have two queries below: # Fetch the file information $query ="update filestorage set approved ='Y' where FileID = {$id}"; $query1 ="select members.email from members, filestorage where filestorage.author = members.username and FileID = {$id}" ; then execute the query using command below: $result = @mysql_query($query) or die("Error! Query failed: <pre>". mysql_error($dbLink)...
2
3319
by: reeba | last post by:
I want to store the query result, in an servlet, into an xml file and display the contents of the xml file on the browser...... my code is as follows: public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ // List dataList = new ArrayList(); response.setContentType("text/html"); PrintWriter pw = response.getWriter();
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.