Get list of column names of specific table in VBA

VBA code shown below creates function that returns names of all columns of specific table.

Code

Public Sub list_columns_in_table(table_name As String)

  Dim rst As New ADODB.Recordset
  rst.Open "select * from  " + table_name + " where 1=0", _
        CurrentProject.Connection 

  For i = 0 To rst.Fields.Count - 1
    Debug.Print rst.Fields(i).Name
  Next i

End Sub

Instruction

Execute funciton with following statement and provide your table name as parameter:

call  list_columns_in_table ("hr_employees")

Notes

The code builds query that returns no rows (where 1=0 condition), but it returns column names that are parsed in for...next loop.

Rows

  • One row represents a column
  • Scope of rows: - all columns in specified table

Sample results

You can see all columns of hr employees table: