Question : In Access 2007, need a VBA function to test external database table to see if it is empty

In an Access 2007 form's VBA, trying to test a table in an external database, to see if it is empty. Hoping to do so with a button.

This is as far as I've been able to get, and it's just not working...

Private Sub cmdTester_Click()
On Error GoTo Err_cmdTester_Click

Dim strQRY, varcount
strQRY = "SELECT * FROM tbl_External IN 'C:\datastores\myExternalDatabase.mdb';"

varcount = Nz(DCount("*", "[strQRY]")

If varcount = 0 Then
    MsgBox ("it's empty")
Else
     MsgBox (varcount)
End If
 
 
Exit_cmdTester_Click:
    Exit Sub

Err_cmdTester_Click:
    MsgBox Err.Description
    Resume Exit_cmdTester_Click
   
End Sub


I have a number of tables to check, and was eventually hoping to create a function

function test(external_table_name)

which I could call from within other procedures.

Any help would be greatly appreciated.

Answer : In Access 2007, need a VBA function to test external database table to see if it is empty

Private Sub cmdTester_Click()
On Error GoTo Err_cmdTester_Click

Dim strQRY, varcount, qd as Dao.querydef
strQRY = "SELECT * FROM tbl_External IN 'C:\datastores\myExternalDatabase.mdb';"

set qd=currentdb.createquerydef("myQ",strQRY)
varcount = DCount("*", "myQ")

If varcount = 0 Then
    MsgBox ("it's empty")
Else
     MsgBox (varcount)
End If
 
 
Exit_cmdTester_Click:
    Exit Sub

Err_cmdTester_Click:
    MsgBox Err.Description
    Resume Exit_cmdTester_Click
   
End Sub
Random Solutions  
 
programming4us programming4us