Solved

Count the fields in a table

Posted on 2010-11-30
4
440 Views
Last Modified: 2013-11-27
I am trying to document an existing application, and I want to determine the number of fields in each table.  Is there a way to do this with the TableDefs collection?
0
Comment
Question by:AaronGreene1906
[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
  • 3
4 Comments
 
LVL 10

Expert Comment

by:VTKegan
ID: 34240508
This will print to the immediate window the number of records in each table excluding System Tables.
Sub PrintTableCounts()

    Dim Tbl As Object
    Dim rst As DAO.Recordset
    For Each Tbl In CurrentDb.TableDefs
        If Not Tbl.Name Like "*Sys*" Then
            Set rst = CurrentDb.OpenRecordset("Select * From " & Tbl.Name)
            If Not rst.EOF Then
                rst.MoveLast
                Debug.Print Tbl.Name & ":" & rst.RecordCount & "Records"
            Else
                Debug.Print Tbl.Name & ": 0 Records"
            End If
        End If
    Next
    Set Tbl = Nothing
    Set rst = Nothing
End Sub

Open in new window

0
 
LVL 10

Expert Comment

by:VTKegan
ID: 34240812
After re-reading the question is looks like you are looking for fields, not records.  My mistake.

Use this code below
Sub PrintTableCounts()

    Dim Tbl As Object
    Dim Counter As Integer
    For Each Tbl In CurrentDb.TableDefs
        Counter = 0
        If Not Tbl.Name Like "*Sys*" Then
            For Each Field In Tbl.Fields
                Counter = Counter + 1
            Next
            Debug.Print Tbl.Name & ": " & Counter & " Fields"
        End If
    Next
    Set Tbl = Nothing

End Sub

Open in new window

0
 
LVL 10

Accepted Solution

by:
VTKegan earned 500 total points
ID: 34240829
And after putting some thought into it.  I didn't need the For loop to count.  It is already a property of Fields.

This is a simpler version of the code.
Sub PrintTableCounts()

    Dim Tbl As Object
    For Each Tbl In CurrentDb.TableDefs
        If Not Tbl.Name Like "*Sys*" Then
            Debug.Print Tbl.Name & ": " & Tbl.Fields.Count & " Fields"
        End If
    Next
    Set Tbl = Nothing

End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:nathaniel
ID: 34242464
here is a function if you want to know the number/count of fields in a given table:

Function fieldCount(vTable)
    dim db as Database
    dim tdf as TableDef
    set db  = CurrentDb
    set tdf = db.TableDefs(vTable)
    fieldCount = tdf.Fields.Count
End Function

hope that helps
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print previewing the record showing on the form 4 51
Compress Newid value ms sql Mssql 4 47
Converting Access 2016 from 32-bit to 64-bit 8 61
SUBFORM on ACCESS 2013 8 35
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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 …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

738 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