Back To Top


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

Go here To Check if Field exists in VBA.
Go here To Change a Field's Data Type.
Go here To Update a Field in VBA.
Follow the Set Autonumber Field with VBA Link to Set a Field to Autonumber.

 

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

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


Function
RenameField( _
     
ByVal TableName As String , _
     
ByVal NewFieldName As String , OldFieldName 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
   'Changes a Fields Name

   ' TableName : Name of table
   ' OldFieldName : Old Name of the field
   ' NewFieldName : New Name of the field

   'Returns True on success, false otherwise

   'USAGE: RenameField "TABLENAME", "NEW FIELDNAME", "OLD FIELDNAME"


  
On Error GoTo errhandler

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



  
Set Db = CurrentDb()

  
Set tdf = Db.TableDefs(TableName)


  
' Rename the field
  
tdf.Fields(OldFieldName).Name = NewFieldName

   RenameField =
True


ExitHere:

  
Set tdf = Nothing
   Set
Db = Nothing

  
MsgBox "Field Renamed"
  
Exit Function


errhandler:
   RenameField =
False
   With Err
     
MsgBox "Error " & .Number & vbCrLf & .Description, _
            vbOKOnly
Or vbCritical, "RenameField "
  
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 Check if Field exists in VBA.
Go here To Create a Field 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