473,422 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

Can I perform a DCount of records returned by SQL statement (not a saved query)

MLH
Suppose I have this simple SQL string...

SELECT tblDrivers.DriverID, tblDrivers.DName
FROM tblDrivers
WHERE (((tblDrivers.DName) Like "N*"))
ORDER BY tblDrivers.DriverID;

And suppose that its not a saved querydef -
just an SQL string that I cooked up in code
and assigned to a global var strMySQL.

How best to count the records returned by
the query? DCount doesn't seem to be the
tool to do it with.
Nov 13 '05 #1
13 6539

"MLH" <CR**@NorthState.net> wrote in message
news:i3********************************@4ax.com...
Suppose I have this simple SQL string...

SELECT tblDrivers.DriverID, tblDrivers.DName
FROM tblDrivers
WHERE (((tblDrivers.DName) Like "N*"))
ORDER BY tblDrivers.DriverID;

And suppose that its not a saved querydef -
just an SQL string that I cooked up in code
and assigned to a global var strMySQL.

How best to count the records returned by
the query? DCount doesn't seem to be the
tool to do it with.


That query will select all records from tblDrivers where DName starts with
'N'. The practical solution would be simply:

SELECT Count(*)
FROM tblDrivers
WHERE DName Like 'N*'

If for some reason you absolutely must use that original query, you could
use:

"SELECT Count(*) From (" & strMySQL & ")"

HTH,
Randy

Nov 13 '05 #2
MLH <CR**@NorthState.net> wrote in
news:i3********************************@4ax.com:
Suppose I have this simple SQL string...

SELECT tblDrivers.DriverID, tblDrivers.DName
FROM tblDrivers
WHERE (((tblDrivers.DName) Like "N*"))
ORDER BY tblDrivers.DriverID;

And suppose that its not a saved querydef -
just an SQL string that I cooked up in code
and assigned to a global var strMySQL.

How best to count the records returned by
the query? DCount doesn't seem to be the
tool to do it with.

Since DCount opens a separate instance of the query, it can't be
done if the sql isn't saved. You can create a recordset, that
uses an SQL string as its source, and get the number of records
from that.

set rs = currentdb.openRecordset(strMySQL, dbOpenSnapshot)
rs.movelast
msgbox "the query returns " & rs.recordcount & " Records"
--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #3
MLH
<snip>

That query will select all records from tblDrivers where DName starts with
'N'. The practical solution would be simply:

SELECT Count(*)
FROM tblDrivers
WHERE DName Like 'N*' Sounds like a plan. If X is my variable dim'd as Long,
what VBA syntax is used to assign X its value of the
number of records in tblDrivers WHERE DName Like 'N*'?

If for some reason you absolutely must use that original query, you could
use:

Not at all, Randy. I like the above - as long as I can assign the
count to the variable, X. 'preciate it.
Nov 13 '05 #4

"MLH" <CR**@NorthState.net> wrote in message
news:rq********************************@4ax.com...
<snip>

That query will select all records from tblDrivers where DName starts with'N'. The practical solution would be simply:

SELECT Count(*)
FROM tblDrivers
WHERE DName Like 'N*'

Sounds like a plan. If X is my variable dim'd as Long,
what VBA syntax is used to assign X its value of the
number of records in tblDrivers WHERE DName Like 'N*'?

If for some reason you absolutely must use that original query, you could
use:

Not at all, Randy. I like the above - as long as I can assign the
count to the variable, X. 'preciate it.


In ADO, you could use something like this:

Dim varCount
Dim SQL as string
Dim lngX
Dim cn as New ADODB.Connection

Set cn = CurrentProject.Connection

SQL = "Select Count(*) " & _
"From tblDrivers " & _
"Where DName Like 'N*'"

varCount = cn.Execute(SQL).GetString
lngX = Nz(varCount, 0)

Some comments:
I'm in the habit of using a var with .GetString. I do that in case the query
returns a Null value. Anything other than a var will get an error. In this
particular case, it wouldn't be necessary to do so, because a Count always
returns a value, even if it's 0. If you prefer, you could simply use:

lngX = cn.Execute(SQL).GetString

HTH,
Randy

Nov 13 '05 #5
MLH
Thank-you, Randy. To my knowledge, this is my first look
at ADO. Mind you, I've done my homework in DAO and this
looks similar. 'preciate the advice. I was beginning to think
that DAO might be the way to go and I'm glad to get an
introduction to DAO via this issue.

In ADO, you could use something like this:

Dim varCount
Dim SQL as string
Dim lngX
Dim cn as New ADODB.Connection

Set cn = CurrentProject.Connection

SQL = "Select Count(*) " & _
"From tblDrivers " & _
"Where DName Like 'N*'"

varCount = cn.Execute(SQL).GetString
lngX = Nz(varCount, 0)

Some comments:
I'm in the habit of using a var with .GetString. I do that in case the query
returns a Null value. Anything other than a var will get an error. In this
particular case, it wouldn't be necessary to do so, because a Count always
returns a value, even if it's 0. If you prefer, you could simply use:

lngX = cn.Execute(SQL).GetString

HTH,
Randy

I like that extra bit of effort. It may seem redundant to some (like
putting a forward or backward slash at the end when you're DIRing
for a directory at the command prompt to specify a directory rather
than a file). I'm always questioning my work and any little thing to
provide reassurance that stuff is working helps me sleep better.
Nov 13 '05 #6
Personally, I find this DAO/ADO dichotomy absolutely maddening. I curse
Microsoft for the stop - start - misdirection of the whole mess. A lot of
the guru types that post here effectively use them together. I find it
confusing to do so. I stick with ADO - not because it's better - but,
because it is what I am comfortable with.

Perhaps someone else would post some DAO-centric code that will accomplish
the same thing?
"MLH" <CR**@NorthState.net> wrote in message
news:b4********************************@4ax.com...
Thank-you, Randy. To my knowledge, this is my first look
at ADO. Mind you, I've done my homework in DAO and this
looks similar. 'preciate the advice. I was beginning to think
that DAO might be the way to go and I'm glad to get an
introduction to DAO via this issue.

In ADO, you could use something like this:

Dim varCount
Dim SQL as string
Dim lngX
Dim cn as New ADODB.Connection

Set cn = CurrentProject.Connection

SQL = "Select Count(*) " & _
"From tblDrivers " & _
"Where DName Like 'N*'"

varCount = cn.Execute(SQL).GetString
lngX = Nz(varCount, 0)

Some comments:
I'm in the habit of using a var with .GetString. I do that in case the queryreturns a Null value. Anything other than a var will get an error. In thisparticular case, it wouldn't be necessary to do so, because a Count alwaysreturns a value, even if it's 0. If you prefer, you could simply use:

lngX = cn.Execute(SQL).GetString

HTH,
Randy

I like that extra bit of effort. It may seem redundant to some (like
putting a forward or backward slash at the end when you're DIRing
for a directory at the command prompt to specify a directory rather
than a file). I'm always questioning my work and any little thing to
provide reassurance that stuff is working helps me sleep better.


Nov 13 '05 #7
Dim varCount
Dim SQL as string
Dim lngX
Dim rs as DAO.Recordset

SQL = "Select Count(*) As Records " & _
"From tblDrivers " & _
"Where DName Like 'N*'"

Set rs = CurrentDb.CreateRecordset(SQL)
If rs.EOF = False
lngX = Nz(rs!Records, 0)
End If

rs.Close
Set rs = Nothing

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"Randy Harris" <ra***@SpamFree.com> wrote in message
news:Mm**************@newssvr17.news.prodigy.com.. .
Personally, I find this DAO/ADO dichotomy absolutely maddening. I curse
Microsoft for the stop - start - misdirection of the whole mess. A lot of
the guru types that post here effectively use them together. I find it
confusing to do so. I stick with ADO - not because it's better - but,
because it is what I am comfortable with.

Perhaps someone else would post some DAO-centric code that will accomplish
the same thing?
"MLH" <CR**@NorthState.net> wrote in message
news:b4********************************@4ax.com...
Thank-you, Randy. To my knowledge, this is my first look
at ADO. Mind you, I've done my homework in DAO and this
looks similar. 'preciate the advice. I was beginning to think
that DAO might be the way to go and I'm glad to get an
introduction to DAO via this issue.
>
>In ADO, you could use something like this:
>
>Dim varCount
>Dim SQL as string
>Dim lngX
>Dim cn as New ADODB.Connection
>
>Set cn = CurrentProject.Connection
>
>SQL = "Select Count(*) " & _
> "From tblDrivers " & _
> "Where DName Like 'N*'"
>
>varCount = cn.Execute(SQL).GetString
>lngX = Nz(varCount, 0)
>
>Some comments:
>I'm in the habit of using a var with .GetString. I do that in case the query >returns a Null value. Anything other than a var will get an error. In this >particular case, it wouldn't be necessary to do so, because a Count always >returns a value, even if it's 0. If you prefer, you could simply use:
>
>lngX = cn.Execute(SQL).GetString
>
>HTH,
>Randy

I like that extra bit of effort. It may seem redundant to some (like
putting a forward or backward slash at the end when you're DIRing
for a directory at the command prompt to specify a directory rather
than a file). I'm always questioning my work and any little thing to
provide reassurance that stuff is working helps me sleep better.

Nov 13 '05 #8
MLH
I have a sneaky suspicion that CreateRecordset isn't
going to work for me in A97. Am I wrong about that?
Or, is my hunch correct?
I'm guessing that OpenRecordset is the method I'll
use in A97. Here's what I tried but 'yikes' it isn't
working...
MySQL = "SELECT DISTINCTROW qryVehiclesNowners5.SerialNum,
qryVehiclesNowners5.VDescr, qryVehiclesNowners5.Owner,
qryVehiclesNowners5.VehicleJobID , tblVehicleJobs.Reclaimed,
tblVehicleJobs.VSaleID, tblVehicleJobs.ENF262Written FROM
qryVehiclesNowners5 INNER JOIN tblVehicleJobs ON
qryVehiclesNowners5.VehicleJobID = tblVehicleJobs.VehicleJobID WHERE
(tblVehicleJobs.Reclaimed=No AND tblVehicleJobs.VSaleID Is Null AND
tblVehicleJobs.ENF262Written=True);"

Dim dbs As Database, rst As Recordset

' Return reference to current database.
10 Set dbs = CurrentDb
20 Set rst = dbs.OpenRecordset(MySQL)
30 rst.MoveLast
40 Me!TheCount = rst.RecordCount
50 rst.Close
60 Set dbs = Nothing

But error handler says line #20 is a problem...
"The following unexpected error occurred in Sub Form_Open, Line #20,
CBF on frmVehicleChooserForm. 3061: "Too few parameters, Expected 5."
Kind-a-blows my mind. A simple piece of SQL works. For instance: If
MySQL = "select VehicleJobID from tblVehicleJobs;" - there's no
error. Any ideas why? The SQL works find pasted into a query and ran.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x

Dim varCount
Dim SQL as string
Dim lngX
Dim rs as DAO.Recordset

SQL = "Select Count(*) As Records " & _
"From tblDrivers " & _
"Where DName Like 'N*'"

Set rs = CurrentDb.CreateRecordset(SQL)
If rs.EOF = False
lngX = Nz(rs!Records, 0)
End If

rs.Close
Set rs = Nothing


Nov 13 '05 #9

"MLH" <CR**@NorthState.net> wrote in message
news:a4********************************@4ax.com...
I have a sneaky suspicion that CreateRecordset isn't
going to work for me in A97. Am I wrong about that?
Or, is my hunch correct?
I'm guessing that OpenRecordset is the method I'll
use in A97. Here's what I tried but 'yikes' it isn't
working...
MySQL = "SELECT DISTINCTROW qryVehiclesNowners5.SerialNum,
qryVehiclesNowners5.VDescr, qryVehiclesNowners5.Owner,
qryVehiclesNowners5.VehicleJobID , tblVehicleJobs.Reclaimed,
tblVehicleJobs.VSaleID, tblVehicleJobs.ENF262Written FROM
qryVehiclesNowners5 INNER JOIN tblVehicleJobs ON
qryVehiclesNowners5.VehicleJobID = tblVehicleJobs.VehicleJobID WHERE
(tblVehicleJobs.Reclaimed=No AND tblVehicleJobs.VSaleID Is Null AND
tblVehicleJobs.ENF262Written=True);"

Dim dbs As Database, rst As Recordset

' Return reference to current database.
10 Set dbs = CurrentDb
20 Set rst = dbs.OpenRecordset(MySQL)
30 rst.MoveLast
40 Me!TheCount = rst.RecordCount
50 rst.Close
60 Set dbs = Nothing

But error handler says line #20 is a problem...
"The following unexpected error occurred in Sub Form_Open, Line #20,
CBF on frmVehicleChooserForm. 3061: "Too few parameters, Expected 5."
Kind-a-blows my mind. A simple piece of SQL works. For instance: If
MySQL = "select VehicleJobID from tblVehicleJobs;" - there's no
error. Any ideas why? The SQL works find pasted into a query and ran.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x


It's likely that you've got a syntax error in the WHERE clause. Try running
the code after removing the entire WHERE clause.

What datatypes are Reclaimed and ENF262Written?

Nov 13 '05 #10
MLH:

The 'Too few parameters' error usually means that a query referenced in
a recordset has parameters that you aren't feeding to the query. My
guess is that your query qryVehiclesNowners5 has 5 parameters in
it...First, you'll need create a temporary querydef that is based on
your SQL statement. Then you'll need to pass the parameters to the
querydef object. Once that is done, you can then set your recordset to
the results of the querydef. Here's some sample code:

Dim dbs As Database
Dim rst As Recordset
Dim qdf As QueryDef
Dim MySQL As String

MySQL = "SELECT DISTINCTROW qryVehiclesNowners5.SerialNum,
qryVehiclesNowners5.VDescr, qryVehiclesNowners5.Owner,
qryVehiclesNowners5.VehicleJobID , tblVehicleJobs.Reclaimed,
tblVehicleJobs.VSaleID, tblVehicleJobs.ENF262Written FROM
qryVehiclesNowners5 INNER JOIN tblVehicleJobs ON
qryVehiclesNowners5.VehicleJobID = tblVehicleJobs.VehicleJobID WHERE
(tblVehicleJobs.Reclaimed=No AND tblVehicleJobs.VSaleID Is Null AND
tblVehicleJobs.ENF262Written=True);"

Set dbs = CurrentDb
Set qdf = dbs.CreateQueryDef("qryTemp", MySQL)

'repeat once for each parameter, with the ENTIRE parameter within
'one set of brackets. If your parameter is grabbed from a form, use
'something like this:
'qdf![Forms!FormName!FormField] = Forms!FormName!FormField
'otherwise, use something like these:

qdf![Parameter1] = "Whatever you want fed to the first parameter"
qdf![Parameter2] = "Whatever you want fed to the second parameter"

Set rst = qdf.OpenRecordset
rst.MoveLast
Me!TheCount = rst.RecordCount

dbs.QueryDefs.Delete qdf.NAME
rst.close
Set dbs = Nothing

The deleting of the querydef is VERY IMPORTANT, or it will error out
the second time you run it and tell you that the querydef already
exists. Remember that you'll need to wrap date parameters with #
signs, strings with single quotes, etc.

HTH,

Jana

Nov 13 '05 #11
MLH
They are both boolean yes-no types
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Fri, 14 Oct 2005 13:53:39 GMT, "Randy Harris" <ra***@SpamFree.com>
wrote:
What datatypes are Reclaimed and ENF262Written?


Nov 13 '05 #12
MLH
Sounds promising. I'll give it a shot and
give you a report.
Nov 13 '05 #13
MLH:

Did this work for you? Just curious...

Jana

Nov 13 '05 #14

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

Similar topics

7
by: jdph40 | last post by:
I posted this problem previously and received excellent help from Wayne Morgan. However, I still have an unanswered question. My form (frmVacationWeeks) is opened from the OnClick event of a...
5
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
4
by: Will | last post by:
Hi, I had a DCount within a module on records in a table where CustSuffix = 0. I now need to DCount where CustSuffix = 0 and the type of cost Suffix (Suffix in table) = G. I can't get both...
6
by: Mike Conklin | last post by:
This one really has me going. Probably something silly. I'm using dcount for a report to determine the number of different types of tests proctored in a semester. My report is based on a...
3
by: BerkshireGuy | last post by:
I am having difficulty with using the Dcount function. I am trying to return the number of records from qryIndividualFeedbackDetail where the TimelyManner field is set to 4. So, in the new...
6
by: Rajat Katyal | last post by:
Hi: In postgres documentation its written that if we execute query as PERFORM query inside our stored procedure; then the special variable FOUND is set totrue if the query produced at least one...
7
by: MLH | last post by:
?dcount("","qryOwnrsDueITSwMissingAddr") when run in the immediate window return a number greater than the number of rows that display when the saved query is run - opening in the database window?...
6
by: InnoCreate | last post by:
Hi everyone. I've recently written a classic asp website which uses an MS Access datasource. I know this is less than an ideal data source as it has limited functionality. I have a search form on...
3
by: ringer | last post by:
I am trying to add a functionality into a db I use for my checkbook that will help me plan for and save money for future large expenses. Into a table called tblFutureTransactions I want to enter...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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,...
0
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...
0
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...

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.