Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Queries that return values from Access using DAO 3.6

Posted on 2002-06-05
9
186 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON Response and request in VB6 application 11 564
VBA filters 2 64
Notepad++ how to remove delimiter : from beggning of the line? 3 110
Advice in Xamarin 21 95
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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 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…

790 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