Originally Posted by
orcinus
When I try this, I get an "Invalid Operation" error that points to the:
.Fields("field").Attributes = .Fields("field").Attributes + dbAutoIncrField
part of the code. The field appears to be appended per the code just before that as when I check the table, it shows. However, it fails when trying to adjust the field to autonumber..
Any suggestions? Thanks..
I got it!!! I learned something new today! wooohoo!
apparently you can't assign the autonumber attribute to an existing field that's been appended to the table already, via code. So you have to use a variable to do the work and append it at the very last line. e.g. - change this:
Code:
Dim tbl As DAO.TableDef
Dim db As DAO.Database
Set db = CurrentDb
Set tbl = db.tabledefs("tablename")
with tbl
.Fields.Append .CreateField("field", dblong)
.Fields("field").Attributes = .Fields("field").Attributes + dbAutoIncrField
end with
Set tbl = Nothing
set db = nothing
to this:
Code:
Dim tbl As DAO.TableDef
Dim db As DAO.Database
dim fld as dao.field
Set db = CurrentDb
Set tbl = db.tabledefs("tablename")
with tbl
set fld = tbl.CreateField("field", dbLong)
fld.Attributes = fld.Attributes + dbAutoIncrField
.Fields.Append fld
end with
set fld = nothing
Set tbl = Nothing
set db = nothing
You should be fine with that.