Back To Top


MS Access VBA Create Field For versions 2000, 2002, 2003, 2007

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

 

The VBA Create Field Sample is available for download here in a zipped MS Access Database.

The Actual VBA Code for Creating a Field, you should be able to copy/paste right into a module:



Function
CreateField( _
     
ByVal strTableName As String , _
     
ByVal strFieldName As String ) _
     
As Boolean

  
'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
   'Creates a Text field, other data types listed
   '
   ' strTableName : Name of table in which to create the field
   '
   ' strFieldName : Name of the new field to add to table
   ' Returns True on success, false otherwise
   '
   'USAGE: CreateField "TABLENAME", "FIELDNAME" 

  
On Error GoTo errhandler

  
Dim Db As DAO.Database
  
Dim fld As DAO.Field
  
Dim tdf As DAO.TableDef 

  
Set Db = Application.CurrentDb 
  
Set tdf = Db.TableDefs(strTableName) 

  
' First create a field with data type = Text
  
Set fld = tdf.CreateField(strFieldName, dbText)

  
'A few Alternate datatypes: for DAO - Note: The listed Complex data types require  
         '
Access 2007 or higher
   'Long = dbLong or dbComplexLong
   'Single = dbSingle or dbComplexSingle
   'Double = dbDouble or dbComplexDouble
   'Integer = dbInteger
   'Decimal = dbDecimal or dbComplexDecimal
   'Text = dbText or dbComplexText
   'Memo = dbMemo
   'Currency = dbCurrency
   'Yes/No = dbBoolean
   'Date = dbDate 

   ' Appending the field
  
With tdf.Fields
      .Append fld
      .Refresh
  
End With
  
CreateField = True

ExitHere:
  
Set fld = Nothing
   Set
tdf = Nothing
   Set
Db = Nothing

  
MsgBox "Create Field Complete"
  
Exit Function

errhandler:
  
CreateField = False 

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

   Resume
ExitHere

End Function



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 Rename a Field in VBA.
Go here To Check if Table exists in VBA.
Go here To Check if Field exists in VBA.
Follow the Set Autonumber Field with VBA Link to Set a Field to Autonumber.

To Create a field manually go here.

 

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

    

 

Custom Search