Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access table row count in VBA

Status
Not open for further replies.

johncb

Programmer
Nov 4, 1999
41
US
Hi all,
Advice please on how I can, in VBA code, return a row count into an integer variable.
e.g.
======================
Sub something
dim dbs as variant
dim intRows as integer
dim strSQL as string

set dbs = currentdb
strSQL = "SELECT Count(*) AS intRows FROM tblX WHERE tblX.name = 'smith';"
dbs.execute strSQL
end sub
=============
I need to be able to get the count into 'intRows' but this code does not work.

Any help gratefully received.

John


 
If you get a recordset rather then doing an execute query as shown below you can get the recordCount.
Code:
    Dim db As Database
    Dim qry As QueryDef
    Dim rst As Recordset
    Set db = CurrentDb
    Set qry = db.CreateQueryDef("", "SELECT * FROM tblX")
    Set rst = qry.OpenRecordset()
    if rst.eof = false then
         intRows = rst.RecordCount
    end if
    'make sure that you set the recordset to nothing or you will have a memory leak
    set rst = nothing


The hardest questions always have the easiest answers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top