Creating a new field and making it required

  • Thread starter Thread starter Miguel Velez
  • Start date Start date
M

Miguel Velez

I want to create a new field in using VBA code and make it
a required field. I've tried using the examples in the
help screens but I can't get it to work; I can create the
field OK but I don't seem to be able to write any valid
code that would make it a required field.

Can anyone show me how I would do this?

Miguel
 
This example creates a table with a required text field and an autonumber
field:

Sub CreateTableDAO()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field

Set db = CurrentDb()
Set tdf = db.CreateTableDef("MyNewTable")

With tdf
Set fld = .CreateField("MyID", dbLong)
fld.Attributes = dbAutoIncrField + dbFixedField
.Fields.Append fld

Set fld = .CreateField("MyRequiredField", dbText, 60)
fld.Required = True
.Fields.Append fld
End With

db.TableDefs.Append tdf
Application.RefreshDatabaseWindow 'Show the changes

Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
End Sub
 
I want to create a new field in using VBA code and make it
a required field.

ALTER TABLE MyTable
ADD COLUMN NewField INTEGER NOT NULL DEFAULT -1


The default clause only works with jet 4, so you need to run it on an ADO
connection, rather than a DAO database. ie This works:

CurrentProject.Connection.Execute strSQL

but this doesn't

CurrentDB().Execute strSQL, dbFailOnError


chiz chiz chiz

All the best


Tim F
 
Back
Top