Back To Top


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

Go here To Delete All Tables in VBA.
Go here To Delete a Table in VBA.
Go here To Delete Records in VBA.

 

The VBA Delete Field Sample is available for download here in a zipped MS Access Database. The below picture (click to enlarge) shows the form.

MS Access VBA Delete Field Button 

The Actual VBA Code to Delete a Field using criteria for the Function, you should be able to copy/paste right into a module:



Function
DeleteField(tblName As String , strField As String ) As Boolean

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

  
On Error GoTo errhandler
  
' 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 True on Success, False otherwise.
   ' Accepts
   ' tblName: Name of Table that the Field is located
   ' strField: Name of  Field to delete 
   ' Deletes Field strField in Table tblName.
   ' USAGE: DeleteField "tblName", "strField"

  
Set Db = CurrentDb()
  
  
'Set tblName to your table mine is 1A
  
Set tdf = Db.TableDefs(tblName)
   
   'Execute the delete 
   tdf.Fields.Delete (strField)
   DeleteField =
True
  
MsgBox "Delete Field " & strField & " Complete." 

   Db.Close

ExitHere:

  
Set Db = Nothing
   Set
tdf = Nothing

   Exit Function

errhandler:
'There is an error return as False

  
DeleteField = False 
   With
Err 

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

   Resume
ExitHere
End Function



For the above VBA Delete Function in a usable Form with Delete Button, download the Sample database in ZIP format. The sample includes 1 tables in which to test on.

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 Delete Records in the Query Builder.
Go here To Check if Table exists in VBA.
Go here To Check if Field exists in VBA.

 

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

    

 

Custom Search