MS Access VBA – List Table Fields

'---------------------------------------------------------------------------------------
' Procedure : listTableFields
' Author    : CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Return a listing of all the fields (column names) of a give table
' Copyright : The following code may be used as you please, but may not be resold, as
'             long as the header (Author, Website & Copyright) remains with the code.
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' strTblName - Name of the table to list the fields of.
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2007-June-01            Initial Release
'---------------------------------------------------------------------------------------
Function listTableFields(strTblName As String) As String
On Error GoTo listTableFields_Error
    Dim db As DAO.Database
    Dim tdfld As DAO.TableDef
    Dim fld As Field
 
    Set db = CurrentDb()
    Set tdfld = db.TableDefs(strTblName)
    For Each fld In tdfld.Fields    'loop through all the fields of the tables
        Debug.Print fld.Name
    Next
 
    Set tdfld = Nothing
    Set db = Nothing
If Err.Number = 0 Then Exit Function
 
listTableFields_Error:
    MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf & "Error Number: " & _
    Err.Number & vbCrLf & "Error Source: listTableFields" & vbCrLf & _
    "Error Description: " & Err.Description, vbCritical, "An Error has Occured!"
    Exit Function
End Function

Leave a Reply









Spam protection by WP Captcha-Free