Solved

Queries that return values from Access using DAO 3.6

Posted on 2002-06-05
9
187 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

733 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