?
Solved

Run a SQL select statement in VBA

Posted on 2004-08-24
5
Medium Priority
?
3,067 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
[X]
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
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
walterecook earned 500 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…
Suggested Courses

770 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