Check for existence of a field before other code ?

I

Isis

I need to check for the existence of a field name in a Table before some
processing code - can this be done ?

Thanks
 
K

Klatuu

Public Function FindFieldName(strTblName As String, strFldName As String) As
Boolean
Dim tdfs As TableDefs
Dim tdf As TableDef
Dim flds As Fields
Dim fld As Field

On Error GoTo FindFieldName_Error

FindFieldName = False
Set tdfs = CurrentDb.TableDefs
Set tdf = tdfs(strTblName)
Set flds = tdf.Fields
For Each fld In flds
If fld.Name = strFldName Then
FindFieldName = True
Exit For
End If
Next fld

FindFieldName_Exit:

On Error Resume Next
Exit Function

FindFieldName_Error:

If Err.Number = 3265 Then
MsgBox "Table " & strTblName & " Not Found"
Else
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure FindFieldName of Module modUtilities"
End If
GoTo FindFieldName_Exit
End Function
 
I

Isis

Public Function FindFieldName(strTblName As String, strFldName As
String) As Boolean
Dim tdfs As TableDefs
Dim tdf As TableDef
Dim flds As Fields
Dim fld As Field

On Error GoTo FindFieldName_Error

FindFieldName = False
Set tdfs = CurrentDb.TableDefs
Set tdf = tdfs(strTblName)
Set flds = tdf.Fields
For Each fld In flds
If fld.Name = strFldName Then
FindFieldName = True
Exit For
End If
Next fld

FindFieldName_Exit:

On Error Resume Next
Exit Function

FindFieldName_Error:

If Err.Number = 3265 Then
MsgBox "Table " & strTblName & " Not Found"
Else
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure FindFieldName of Module modUtilities"
End If
GoTo FindFieldName_Exit
End Function


Thanks a great deal for this code ! I think I will find many uses for it.

Regards
 
Top