Below are 2 procedures that enable you to enumerate a list of field names for a given table.
Approach 1 – using the DAO TableDefs Collection
'---------------------------------------------------------------------------------------
' Procedure : listTableFields
' Author : CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Return a listing of all the fields (column names) of a given 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 Occurred!"
Exit Function
End Function
Approach 2 – using the OpenRecordset Method
'---------------------------------------------------------------------------------------
' Procedure : listTblFlds
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Return a listing of all the fields (column names) of a given table
' Copyright : The following may be altered and reused as you wish so long as the
' copyright notice is left unchanged (including Author, Website and
' Copyright). It may not be sold/resold or reposted on other sites (links
' back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sTblName : Name of the table to list the fields of.
'
' Usage:
' ~~~~~~
' Call listTblFlds("YourTableName")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2016-08-26 Initial Release
'---------------------------------------------------------------------------------------
Function listTblFlds(sTblName As String) As String
On Error GoTo Error_Handler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sSQL As String
Dim fld As Field
Set db = CurrentDb()
sSQL = "SELECT *" & _
" FROM [" & sTblName & "]" & _
" WHERE (False);"
Set rs = db.OpenRecordset(sSQL, dbOpenSnapshot)
For Each fld In rs.Fields 'loop through all the fields of the tables
Debug.Print fld.Name
Next
Error_Handler_Exit:
On Error Resume Next
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: listTblFlds" & vbCrLf & _
"Error Description: " & Err.Description _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
How do you run the function. A couple of examples would be great. Thanks!
You will call the function, as you would any other VBA function. To try it out, simply use the following syntax in the VBE immediate window:
Call listTableFields(“YourTableName”)
Hello! How can I return the attribute name of a table in Vba?
I’m afraid I do not understand the question. Could you explain further or provide a concrete example?
Method one gets a type mismatch error on later version of Access you need to dim fld as DAO.Field
I can’t seem to replicate the issue.
I’d check which references you have set. As long as you have the ACE library (Microsoft Office XX.X Access database engine Object Library) it will work.
Good day — I received a MS Access Error 3265 Error “item not found in this collection” when trying to run your listTableFields process. Running under Access 2019.
Never Mind, Found that the table name I was testing with ended up with a typo in it. (oops…)