Applecore Pages on Microsoft Access

Checking if a Field in a Table is an AutoNumber

Because an AutoNumber field is only really a self-incrementing Long Integer field, you cannot just check the Field's Type to see if it is an AutoNumber. Instead, you must check the Attributes property of the field:

Function fIsFieldAutoNumber(strTableName As String, strFieldName As String) As Boolean
'   Function to check if a field is an AutoNumber field
'   Accepts:
'       strTableName - the name of the table in the database that contains the field
'       strFieldName - the name of the field to check
'   Returns:
'       True if the field is an AutoNumber field, False otherwise

    On Error GoTo E_Handle
    Dim db As Database
    Dim tdf As TableDef
    Dim fld As Field
    Set db = DBEngine(0)(0)
    Set tdf = db.TableDefs(strTableName)
    Set fld = tdf.Fields(strFieldName)
    If (fld.Attributes And dbAutoIncrField) Then fIsFieldAutoNumber = True
fExit:
    On Error Resume Next
    Exit Function
E_Handle:
    MsgBox Err.Description & vbCrLf & "fIsFieldAutoNumber", vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume fExit
End Function

Top

 


HOME | NEW | TABLES | QUERIES | FORMS | REPORTS | GENERAL | API | DOWNLOADS | TUTORIAL | RESOURCES
E-MAIL
Copyright & Disclaimer

 

Last modified at 06/06/2006 15:01:40