
Setting the field attribute
Pehaps because you may only have 1 autonumber field in a table. Your loop
intimates that there is more than 1 field. The code is probably being
rejected by the Jet engine.
I prefer using SQL in a DDL query:
CREATE TABLE tblContacts
([ContactID] counter,
[LastName] text,
[FirstName] text,
[ContactDate] date,
[Notes] memo,
CONSTRAINT [ContactID] PRIMARY KEY ([ContactID]));
In code, try something like:
Sub AddIncrementField()
Dim db As DATABASE
Dim tdf As TableDef
Dim fld As Field
Dim idx As index
' Return reference to current database.
Set db = CurrentDb
Set tdf = db.TableDefs![MyTable]
Set fld = tdf.CreateField("n", dbLong)
fld.Attributes = dbAutoIncrField
On Error Resume Next
tdf.Fields.Append fld
Set idx = tdf.CreateIndex("PrimaryKey")
idx.Primary = True
Set fld = idx.CreateField("n")
idx.Fields.Append fld ' Append field and refresh collection.
tdf.Indexes.Append idx
tdf.Fields.Refresh
Set db = Nothing
End Sub
-----
Arvin Meyer
Quote:
>Hi Norm,
>really thanks for replying.. thought nobody was reading my scream for help.
>My problem is not to make a table and then make a field in it , but setting
>the attribute of a field.
>When i make a field in a table i can choose if it is a
>"nummeric/text/memo/etc". but not "autonum" i discovered that it is the
>attribute "17" that makes it "autonum".
>I included the code so you can see what i am doing.
>Everything works (perhaps not the example, i ripped it out of my code and
>rewrote it to make it more readable)
>The code makes a Table in another DB, "New Table"
>Then it creates a field in the table, "New AutoNumField"
>In the For ... Next loop it "looks" for the table "New Table" and then it
>should set the attribute of all fields to 17
>BUT IT DOES NOT...
>The strange thing is that i can change the name of all the fields, i can
>READ the attribute, but cannot CHANGE the attribute... please help.
>Dim dbsNew As database, tdf As TableDef, fld,fld2 As Field
>Set dbsNew = OpenDatabase("c:\newdb.mdb")
>Set tdf = dbsNieuw.CreateTableDef("New Table")
>Set fld = tdf.CreateField("New AutoNumField" , 4, 10)
>tdf.Fields.Append fld
>tdf.Fields.Refresh
>dbsNieuw.TableDefs.Append tdf
>dbsNieuw.TableDefs.Refresh
>Set tdf = nothing
>For Each tdf In dbsNew.TableDefs
> If tdf.Attributes = 0 And tdf.Name = "New Table" Then
> For Each fld In tdf.Fields
> fld.Attributes = 17
> Next fld
> End If
>Next tdf
>I can't make it work
>Rinke