Back To Top

MS Access DAO VBA Check if Field Exists 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 Check if Field Exsists in VBA using ADO.
Follow the Delete a Table with VBA Link to delete a Table in VBA.
Go here To Check if Query Exsists in VBA.

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

The following code snippet will use VBA to check if a field exists within a given table.


Function
ifFieldExists(FieldName As String , TableName As String ) As Boolean
  
Dim
rs As Recordset, Db As Database ' DAO Vars

   '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
   'Checks if Field exists.
   'USAGE: ifFieldExists "FIELDNAME", "TABLENAME"

  
On Error GoTo NoField 'If there is no Field capture the error.

  
Set Db = CurrentDb()

  
'If Field is there open it
  
Set rs = Db.OpenRecordset( "Select " & FieldName & " from " & TableName & ";" )
   ifFieldExists =
True
  
rs.Close

ExitHere:

  
Set rs = Nothing
  
Db.Close
  
Set Db = Nothing

  
'Notify the user the process is complete.
  
MsgBox "Field Check Complete"
  
Exit Function

NoField:

  
'If Field is not present set function to false

  
ifFieldExists = False

   With
Err
      MsgBox
"Error " & .Number & vbCrLf & .Description, _
            vbOKOnly
Or vbCritical, "ifFieldExists"
  
End With
   Resume
ExitHere
  
Exit Function
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 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