Back To Top


MS Access VBA Get All Fields for versions 2000, 2002, 2003, 2007

Go here To Check if Table exists in VBA.
Go here To Update a Field in VBA.
Go here To Get Number of Fields in VBA
Go here To Get All Tables in VBA.

The VBA Get All Fields Sample is available for download here in a zipped MS Access Database. The below picture (click to enlarge) shows the Single form and module.

MS Access VBA Get All Fields within a Table. 

The following code snippet will use VBA to Return all Fields within a given table.




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 & vbCrLf ' 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 = vbNullString

  
With Err
     
MsgBox "Error " & .Number & vbCrLf & .Description, _
            vbOKOnly
Or vbCritical, "GetFields"
  
End With

   Resume
ExitHere

End Function

 


For the above VBA Get all Fields Function in an usable Form with Button, download the Sample database in ZIP format. The sample includes 100 records and 35 Fields.

For further help or suggestions go here: MS Access tutorials, Tables, Queries and later: Forms

You may post this tutorial on your website or in a forum. If you do please maintain a Link to Eraseve AP.



For further MS Access tutorials go here: MS Access tutorials

Go here To Check if Table exists in VBA.
Follow the Set Autonumber Field with VBA Link to Set a Field to Autonumber.

 

Did you find what you were looking for?
What would you suggest?
mail@eraserve.com
Your Name:
Your Message:
Your Email:

    

 

Custom Search