Find out if a field exists in a table

  • Thread starter Thread starter Rod
  • Start date Start date
R

Rod

I want to find out if a table has a particular field in it.

Something like

If MyTable.Field("MyField").Exists Then Yippee

what's the neat way of doing this

cheers

Rod

--
 
Thanks for this

Rod


Rick Brandt said:
This function works in Access 97 using DAO.

Function DoesFieldExist(TableName As String, FieldName As String) As Boolean

On Error GoTo ErrHandler

Dim DB As Database
Dim Tbl As TableDef
Dim Fld As Field

Set DB = CurrentDb
Set Tbl = DB.TableDefs(TableName)

For Each Fld In Tbl.Fields
If Fld.Name = FieldName Then DoesFieldExist = True
Next Fld

Egress:
On Error Resume Next
Set Fld = Nothing
Set Tbl = Nothing
Set DB = Nothing
Exit Function

ErrHandler:
Select Case Err.Number
Case 3265
MsgBox "Invalid Table Name.", vbExclamation, "Error Message"
Case Else
MsgBox "Error # " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Select
Resume Egress
 
Back
Top