Create an Autonumber field? (1 Viewer)

greaseman

Closer to seniority!
Local time
Today, 16:32
Joined
Jan 6, 2003
Messages
360
Please help.... I know how to create tables in queries and by use of code, but have encountered a mild headache when trying to create a table field that is of type Autonumber from code.

My code:

CREATE TABLE trefTargetGroups (Autonumber Long,VISIT_DATE Date,LT_SPONSOR_ID TEXT (10));

This makes the table with a field titled Autonumber, but it comes out as a Long Integer, when I want it to be an Autonumber field. I have tried several changes, but all have given sysntax errors. I'd also like to be able to name this Autonumber field programmatically.


Any ideas or suggestions? Thanks!!
 

ghudson

Registered User.
Local time
Today, 17:32
Joined
Jun 8, 2002
Messages
6,195
Reset AutoNumber

If the field's "Data Type" = AutoNumber then it is an AutoNumber field. Long Integer
is a field size option for an AutoNumber field. Those type of numbers created for
that field will increment by +1.

The following subs will allow you to delete all the records in a table, delete a specific field from a table, create an AutoNumber field in a table and move that field to the top [beginning] of the field list in the table.
Code:
[COLOR=Green]'Delete all records in your table[/COLOR]
Public Sub DeleteAllRecords()
    CurrentDb().Execute "DELETE * FROM YourTableNameHere"
End Sub

[COLOR=Green]'Delete the ID_Number [AutoNumber] field from the your table[/COLOR]
Public Sub DeleteTableField()
    DoCmd.RunSQL ("ALTER TABLE YourTableNameHere" & "DROP COLUMN ID_Number;")
End Sub

[COLOR=Green]'Move the new ID_Number [AutoNumber] field in your table to the beginning[/COLOR]
Public Sub MoveTableField()
    Call SetFieldPosition("YourTableNameHere", "ID_Number", 0)
End Sub

[COLOR=Green]'Create the new ID_Number [AutoNumber] field in your table[/COLOR]
Public Sub AddTableField()
    Call CreateAutoNumberField("YourTableNameHere", "YourFieldNameHere")
End Sub
    
Public Sub CreateAutoNumberField(ByVal strTableName As String, ByVal strFieldName As String) As Boolean
On Error GoTo Err_CreateAutoNumberField
'[COLOR=Red]Ensure you make a reference to the Microsoft DAO 3.X Object Library![/COLOR]
    
    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 Sub
    
Err_CreateAutoNumberField:
    CreateAutoNumberField = False
    With Err
        MsgBox "Error " & .Number & vbCrLf & .Description, vbOKOnly Or vbCritical, "CreateAutoNumberField"
    End With
    Resume Exit_CreateAutoNumberField
    
End Sub
HTH

Reset AutoNumber
 
Last edited:

greaseman

Closer to seniority!
Local time
Today, 16:32
Joined
Jan 6, 2003
Messages
360
Thanks for your reply!! I'll give it a go and see what cooks......
 

greaseman

Closer to seniority!
Local time
Today, 16:32
Joined
Jan 6, 2003
Messages
360
Wanted to let you know..... your suggestion worked nicely in our project! Thank you very much!!
 

Users who are viewing this thread

Top Bottom