Solved

Run a SQL select statement in VBA

Posted on 2004-08-24
5
3,041 Views
Last Modified: 2012-08-13
Hi
How would I, in DBA code, run a SQL select statement and then display the number of records it returns?

The statement is:

"SELECT Contact_Number FROM Contacts WHERE Contacts.Tag1=Yes"

I know I can make this as a Query, but there are nine to make and I would rather I did it in code.

Thanks

Corin
0
Comment
Question by:corin_pearce
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
walterecook earned 125 total points
ID: 11881295
set a reference to MS DAO 3.6 object library

dim rs as dao.recordset
dim myCount
set rs = currentdb.openrecordset("SELECT Contact_Number FROM Contacts WHERE Contacts.Tag1=Yes")
rs.movelast
myCount = rs.recordcount
set rs = nothing

That's one way.  I didn't test this, but it looks alright.

Hope it helps
Walt

0
 
LVL 12

Expert Comment

by:fulscher
ID: 11881304
How about

    Dim SQL As String
    Dim RS As Recordset
    SQL = "SELECT Contact_Number FROM Contacts WHERE Contacts.Tag1=Yes"
   
    Set RS = CurrentDb.OpenRecordset(SQL)
    RS.MoveLast
   
    MsgBox RS.RecordCount

Hope this helps
Jan
0
 
LVL 12

Expert Comment

by:fulscher
ID: 11881313
Sorry Walt - didn't notice your post. Jan
0
 
LVL 17

Expert Comment

by:walterecook
ID: 11881326
Happens all the time!  No worries, fulscher

Walt
0
 

Author Comment

by:corin_pearce
ID: 11881385
Didn't know it was that simple!
Thanks very much, was playing around withQueryDef etc for ages...
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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

867 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

19 Experts available now in Live!

Get 1:1 Help Now