Solved

Queries that return values from Access using DAO 3.6

Posted on 2002-06-05
9
182 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now