-- Soumis par Dev Ashish ---
Création d'un champ Autonumber à partir du code.
Il y a deux méthode pour créer un champ de type autonumber à partir du code. La première est d'exécuter un énoncé SQL "Create Table" et la seconde utilise VBA pour ajouter l'attribut dbAutoIncrField à la propriété Attributes du champ désiré.
Pour utiliser la méthode SQL, consulter l'article suivant de la base de connaissance:
Article ID Q116145
ACC: Create and Drop Tables and Relationships Using SQL DDL
Pour la méthode VBA et DAO, utiliser cette fonction:
' ********* Code Start *********** Function fCreateAutoNumberField( _ ByVal strTableName As String, _ ByVal strFieldName As String) _ As Boolean ' ' Creates an Autonumber field with name=strFieldName ' in table strTableName. ' Accepts ' strTableName: Name of table in which to create the field ' strFieldName: Name of the new field ' Returns True on success, false otherwise ' On Error GoTo ErrHandler Dim db As DAO.Database Dim fld As DAO.Field Dim tdf As DAO.TableDef Set db = Application.CurrentDb Set tdf = db.TableDefs(strTableName) ' First create a field with datatype = Long Integer Set fld = tdf.CreateField(strFieldName, dbLong) With fld ' Appending dbAutoIncrField to Attributes ' tells Jet that it's an Autonumber field .Attributes = .Attributes Or dbAutoIncrField End With With tdf.Fields .Append fld .Refresh End With fCreateAutoNumberField = True ExitHere: Set fld = Nothing Set tdf = Nothing Set db = Nothing Exit Function ErrHandler: fCreateAutoNumberField = False With Err MsgBox "Error " & .Number & vbCrLf & .Description, _ vbOKOnly Or vbCritical, "CreateAutonumberField" End With Resume ExitHere End Function ' ********* Code End ***********