Solved

Run a SQL select statement in VBA

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

733 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