Function CreateAutoNumberField(ByVal strTableName As String, ByVal strFieldName As String) As Boolean
On Error GoTo Err_CreateAutoNumberField
'ensure reference is made to DAO
Dim db As DAO.Database
Dim fld As DAO.Field
Dim tdef As DAO.TableDef
Set db = Application.CurrentDb
Set tdef = db.TableDefs(strTableName)
Set fld = tdef.CreateField(strFieldName, dbLong)
With fld
.Attributes = .Attributes Or dbAutoIncrField
End With
With tdef.Fields
.Append fld
.Refresh
End With
CreateAutoNumberField = True
Exit_CreateAutoNumberField:
Set fld = Nothing
Set tdef = Nothing
Set db = Nothing
Exit Function
Err_CreateAutoNumberField:
CreateAutoNumberField = False
With Err
MsgBox "Error " & .Number & vbCrLf & .Description, vbOKOnly Or vbCritical, "CreateAutonumberField"
End With
Resume Exit_CreateAutoNumberField
End Function