Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Queries that return values from Access using DAO 3.6

Posted on 2002-06-05
9
Medium Priority
?
195 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 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

916 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