Solved

Run a SQL select statement in VBA

Posted on 2004-08-24
5
3,045 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

785 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