Find out if a field exists in a table

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

--
 
R

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top