Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set a count(*) SQL statement = to a VB variable?

Status
Not open for further replies.

Kooda

MIS
Dec 18, 2001
26
0
0
US
Basically the subject line said it. I need to actually find out the number that the count query returns.

thanks
 
Something like the following would do it. Assume that conn is an ADODB.Connection that you've already set up and opened.
Code:
Dim RS As ADODB.Recordset
Dim myvar As Long
Set RS = New ADODB.Recordset
RS.Open "SELECT count(*) AS myval FROM mytable", DB
myvar = RS.Fields("myval")
Basically, aggregate operations like count and sum just return a single column in the recordset. I usually alias them to make referencing easier.
 
all I'm trying to do is find out if a customer ID is in the field somewhere. If it isn't, I will tell the user.
If the count is 0, then the ID does not exist.

Can't I do this without a recordset? I'm having problems with some ADO functions and our Sybase SQL server.

thanks
 
Thanks a ton adaHacker..you rule.
A question on the code: myvar = RS.Fields("myval")

it seems that this was the important part the counted the results. What does the "Fields" part do?

thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top