Quote:
>Greetings All:
>Using Access for Windows 95, my goal is to use Visual Basic to add an
>autonumber field to a table.
>Access "auto-numbering" documentation details adding the field using the
>table design window. The "TypeProperty" topic does not discuss
>auto-numbering fields. Are there no constants similar to "dbText" for
>creating auto-numbering fields?
>God Bless,
>Pierre
Pierre
There are two different ways of creating AutoNumber fields in an existing table
- using SQL or using DAO. Below are methods of both:
Sub sAddAuto1()
Dim db As Database
Dim strSQL As String
Set db = DBEngine(0)(0)
strSQL = "ALTER TABLE [tblDate] ADD COLUMN FieldPK COUNTER;"
db.Execute strSQL
Set db = Nothing
End Sub
Sub sCreate2()
Dim db As Database
Dim tdf As TableDef
Dim fld As Field
Set db = DBEngine(0)(0)
Set tdf = db.TableDefs("tblDate")
Set fld = tdf.CreateField("FieldPK", dbLong)
fld.Attributes = dbAutoIncrField
tdf.Fields.Append fld
db.TableDefs.Refresh
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
End Sub
Whilst the SQL method appears easier, remember that quite a lot of table/field
properties cannot be set using this method, and you would need to use DAO
instead.
Jon
Access tips & tricks - http://www.applecore99.com
Microsoft Access webring -
http://nav.webring.yahoo.com/hub?ring=microsoftaccess