Check for existence of a field before other code ?

  • Thread starter Thread starter Isis
  • Start date Start date
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
 
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
 
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
 

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

Back
Top