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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test Table for 0 Records

Status
Not open for further replies.

gnosis13

Programmer
Jul 13, 2001
263
US
I am looking for a simple way to test a table to see if it contains records. I have a user that I support who has some VBA code he has written to update records in a table and he sometimes gets debugged because the table can be empty. The code needs to be relatively reusable and simple. The current solution uses error trapping that is confusing (goto).

Suggestions?

A+, N+, MCP
 
Function IsTableEmpty(strTableName As String) As Boolean

Dim rst As DAO.Recordset

Set rst = CurrentDb.OpenRecordset(strTableName)

IsTableEmpty = rst.BOF And rst.EOF

rst.Close

End Function
 
Hi,

You can use ADO :

Set rs = New ADODB.Recordset
rs.Open "Table_Name", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
MsgBox (rs.RecordCount)
rs.close

Make sure you've referenced the ADO Library
 
You could use the DCount function on the table's primary key field:

DCount("PrimaryKeyField","TableName")

-Gary
 
Thanks, I'm really liking that function...

A+, N+, MCP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top