Back To Top


MS Access VBA Count All Tables for versions 2000, 2002, 2003, 2007

Go here To Check if Table exists in VBA.
Go here To Get all Tables.
Go here To Get all Fields in a Table with VBA

The VBA Count Number of Tables Sample is available for download here in a zipped MS Access Database. The below picture (click to enlarge) shows the form.

MS Access VBA Get All Tables within a database. 

The following code snippet will use VBA to Return the Number of Tables within a given Access database.




Function
GetNumTables() As Long
  
Dim
Db As DAO.Database ' DAO Vars
  
Dim i As Integer , numTables As Integer , nCount As Integer
   Dim
Tname As String

  
'References: Microsoft Access 11.0 Object Library, Microsoft DAO 3.6 Object Library
   'Set references by Clicking Tools and Then References in the Code View window
   ' Returns Number of Tables in the Current Database. excluding MSys tables
   ' Accepts Nothing
   ' To include the MSys tables remove the nCount code
   'USAGE: GetNumTables

  
On Error GoTo errhandler
  
Set Db = CurrentDb()

  
' Loop through all the Tables
  
numTables = CurrentDb.TableDefs.Count
   nCount =
0

  
For i = 0 To numTables - 1

     
'Get Table name
     
Tname = LCase(CurrentDb.TableDefs(i).Name)

     
'Check for mssys and ~ prefixed tables and count them
     
If InStr( 1 , Tname, "msys" ) <> 0 Or _
            InStr(
1 , Tname, "~" ) <> 0 Then

        
'Count tables to exclude from our Total
        
nCount = nCount + 1

     
End If
   Next

  
'Retrieve the number of tables
  
GetNumTables = numTables

  
'Subtract the excluded MSys and ~ Tables
  
GetNumTables = GetNumTables - nCount

  
'If no errors
ExitHere:

  
Set Db = Nothing

  
'Notify the user the process is complete.
  
MsgBox "Table Count Complete"
  
Exit Function


errhandler:
'Capture any Errors

  
With Err
      MsgBox
"Error " & .Number & vbCrLf & .Description, _
            vbOKOnly
Or vbCritical, "GetNumTables"
  
End With
End Function



For the above VBA Count all Tables Function in a usable Form with Button, download the Sample database in ZIP format. The sample includes 3 tables to count.

For further help or suggestions go here: MS Access tutorials, Tables, Queries and later: Forms

You may post this tutorial on your website or in a forum. If you do please maintain a Link to Eraseve AP.



For further MS Access tutorials go here: MS Access tutorials

Go here To Check if Table exists in VBA.

Go here To Get all Fields in a Table with VBA

 

Did you find what you were looking for?
What would you suggest?
mail@eraserve.com
Your Name:
Your Message:
Your Email:

    

 

Custom Search