Function GetFields(ByVal TableName As String) 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 all Field Names that exist in Table TableName.
' Accepts
' TableName: Name of table in which the fields are located
' Returns All Field Names, Null otherwise
'USAGE: GetFields "TABLENAME"
On Error GoTo errhandler
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim strField As String
Set db = CurrentDb
Set tdf = db.TableDefs(TableName)
' Loop through all the fields (columns)
For Each fld In tdf.Fields
strField = strField & fld.Name & ", " ' Grab the field name
Next
'Return the Field Names
GetFields = strField
'If no errors
ExitHere:
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
'Notify the user the process is complete.
' MsgBox "Record Print Complete"
Exit Function
errhandler:
'There is an error return as null
GetFields = "Error " & Err.Number & vbCrLf & Err.Description
With Err
' MsgBox "Error " & .Number & vbCrLf & .Description, _
' vbOKOnly Or vbCritical, "GetFields"
End With
Resume ExitHere
End Function
|