Solved

Queries that return values from Access using DAO 3.6

Posted on 2002-06-05
9
183 Views
Last Modified: 2010-05-02
Hello, I am in VB and using the DAO 3.6 and I have a database that I want to run queries that return things other than recordsets. For instance, I have this:
 Dim i As Integer
 i = DB.Execute("SELECT COUNT (pID) FROM students;")
I want i to be the number of records in student. Is there a way that I can do this so that I don't have to open a recorset and then use rs.Count? Thanks a lot.
~evlich
0
Comment
Question by:Evlich
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 50 total points
ID: 7057819
that kind to use of DB.EXECUTE will ALWAYS return a recordset.  :

dim rsResult as Recordset

set rsResult = DB.Execute("SELECT COUNT(pID) as Result FROM students")

I = rsResult("Result")

That is the only way you can use THAT SQL with db.execute.

That is how DAO operates with a Select Query.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7058831
Use this method instead:

Dim i As Integer
i = DB.Execute("SELECT COUNT (pID) FROM students;").Fields(0).Value

This uses the implicit fields collection of the returned recordset and returns the value of the first (in this case only) field.
0
 
LVL 1

Author Comment

by:Evlich
ID: 7063294
I though DB.Execute does not return a value.
~evlich
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7065332
It doesn't return a value but a recordset, using .Fields(0).Value at the end will not save this recordset to an object but return the value of the first field in the recordset by holding the recordset in memory but never actually committing it to an object variable. It doesn't change the behaviour of the execute method but extends it as you would normally.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 7065971
thate does in one line, hat my code did in two, bit the oe line will be harder to debug, and less flexible.  The choice is up to you.

0
 
LVL 1

Expert Comment

by:barendb
ID: 7070053
'select count(pID) as fieldname from tablename' will return a recordset with one field called fieldname from the database.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 7072554
barendb---that is EXACTLY what both TimCoffee and I have already stated.
0
 
LVL 1

Expert Comment

by:barendb
ID: 7072618
Oops, sorry, was a bit to quick for myself there.  Didn't read properly.
0
 
LVL 1

Author Comment

by:Evlich
ID: 7115580
Thanks a lot, sorry that it took so long. I have been strapped for time hand have not been able to test it.
~evlich
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now