473,807 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sql Select Count result to variable ?

In VBA, how do you see the results of an sql count statement like the
following ?

Select Count (*) as Total from tblCustomer
Result = DoCmd.RunSql("S elect Count (*) as Total from tblCustomer")
gives an error

I need to get the count into a variable.
Oct 17 '06 #1
5 76381
<air code>

Dim rst As Recordset, x as Long

Set rst = CurrentDb.OpenR ecordset("SELEC T Count(*) AS Total FROM
tblCustomer;")
x = rst!Total
rst.Close
Set rst = nothing

If no records exist, x = 0; otherwise x = number of records in
tblCustomer.

Oct 17 '06 #2
Karl Irvin wrote:
In VBA, how do you see the results of an sql count statement like the
following ?

Select Count (*) as Total from tblCustomer
Result = DoCmd.RunSql("S elect Count (*) as Total from tblCustomer")
gives an error

I need to get the count into a variable.
Three ways:
1. is fast.
2. is portable to SQL Server / ASP (somewhat) etc
3. is ... whatever

Sub temp1()
Dim c&
c = CurrentDb.Table Defs("tblCustom er").RecordCoun t
Debug.Print c '91
End Sub

Sub temp2()
Dim c&
c = CurrentProject. Connection.Exec ute("SELECT COUNT(*) FROM
tblCustomer").C ollect(0)
Debug.Print c '91
End Sub

Sub temp3()
Dim c&
c = CurrentDb.OpenR ecordset("SELEC T COUNT(*) FROM
tblCustomer").F ields(0).Value
Debug.Print c '91
End Sub

Oct 17 '06 #3
"Karl Irvin" <88********@com cast.netwrote in
news:uu******** *************** *******@comcast .com:
In VBA, how do you see the results of an sql count statement
like the following ?

Select Count (*) as Total from tblCustomer
Result = DoCmd.RunSql("S elect Count (*) as Total from
tblCustomer") gives an error

I need to get the count into a variable.
Result = dCount("*","tbl Customer","opti onal Where Clause")

Some people badmouth as slow the domain aggregate features, dSum(),
dCount(), dAvg(), etc but they work well when used in moderation

--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com

Oct 17 '06 #4
On Oct 17, 4:12 pm, Bob Quintal <rquin...@sPAmp atico.cawrote:
Some people badmouth as slow the domain aggregate features, dSum(),
dCount(), dAvg(), etc but they work well when used in moderation
They could be very slow years and years ago, but now they seem fine. I
got into the habit of substituting SQL for them and I still do. But
that doesn't make them inadequate.

Oct 17 '06 #5
Bob Quintal wrote:
"Karl Irvin" <88********@com cast.netwrote in
news:uu******** *************** *******@comcast .com:
In VBA, how do you see the results of an sql count statement
like the following ?

Select Count (*) as Total from tblCustomer
Result = DoCmd.RunSql("S elect Count (*) as Total from
tblCustomer") gives an error

I need to get the count into a variable.
Result = dCount("*","tbl Customer","opti onal Where Clause")

Some people badmouth as slow the domain aggregate features, dSum(),
dCount(), dAvg(), etc but they work well when used in moderation

--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com
I think you can even use:

Result = DLookup("Count( *)", "tblCustome r", "optional Where Clause")

I use aggregate functions like those when I'm in a hurry, then use real
SQL later, like Lyle. Also, like Lyle, I don't scold anyone for using
them. I should make sure I have fixed the log :-) of inefficiencies in
my own code before picking at a speck of sawdust in someone else's code
-- and that hasn't happened yet.

James A. Fortune
CD********@Fort uneJames.com

Obtaining knowledge is relatively cheap. Not having the knowledge you
need when you need it is relatively expensive.

Oct 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1329
by: - | last post by:
i have a query as follows and is unsure whether it is mysql or php: $result = mysqli_query("SELECT COUNT(*) > @foobar AS status ....") where it's suppose to get a one row value of either '0' or '1' (false or true) if i were to do a SELECT COUNT(*) FROM... i can use the php $row to refer to the index. if i were to do a SELECT COUNT(*) AS status FROM... i can use the php $row to refer to the index.
12
39692
by: Bing Wu | last post by:
Hi, Has anyone got problem with this on large table. It seems it take ages to return the result ( half an hour :-( ). SELECT COUNT(*) FROM COORDINATE A strange thing is the tempory space (System Managed) keeps growing during the query: 14346747904 Dec 2 16:22 SQL00002.TDA
4
26572
by: Alec Christie | last post by:
Hi All, I want to Count the number of members in my database that have specific criteria, such as Blue eyes, or Brown hair. I have fields named Hair and Eyes, and others ofcourse, but I just want my VBA code to count the number with the specified value, ie Blue Eyes, and assign it to a variable that I can use, that is already defined. Would SELECT COUNT be the correct way? If so, how would I assign it to the variable? If not, how...
2
5227
by: Ben | last post by:
Hi, I have a problem with this query: $link = mysql_connect("localhost", "root", "pw"); $dbsel = mysql_select_db('mydb', $link); $lol='bibi'; $sql = "select count(*) as totuur from studres where where dag>= curdate()
22
12499
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
1
3619
by: zafm86 | last post by:
Hi everyone! I'm sure my problem is pretty easy to solve but I've been working on it for a long and my my brain is not working correctly anymore. I'm working with an AS400 and I mhave to do an "interface" in Access. It's like a little system to make a production plan. It must get information about part numbers, description, set where the part number belongs, quantity of part numbers that the set uses, stock number(this is a text value, it...
5
9650
by: FFMG | last post by:
Hi, I was running a test on a table with 50000 rows. When I do: $sql = "SELECT * FROM TABLE"; $result = mysql_query($sql); $total = mysql_num_rows(result); I get a 'run out of memory error', (the limit is set low on the test
1
6104
by: Luke Davis | last post by:
What is the quickest way to get the count of rows in a table into an int variable? for example how would I make this happen? int remaining = SELECT Count(*) FROM states.dbo.cities
3
2613
by: karpalmera | last post by:
We are trying to do select count(*) and select * from the same table. We have executed the following: when we execute SELECT COUNT(*) FROM TABLE1; the result is 0 but when we execute SELECT *
0
9721
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
9600
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
10628
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
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5547
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...
1
4331
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
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.