Home > front end >  How to create an MS Access table with a checkbox field in VBA
How to create an MS Access table with a checkbox field in VBA

Time:04-29

I want to create a MS Access table dynamically with a checkbox field in VBA. Setting the field property to checkbox works with an existing table:

Set dbs = CurrentDb()
Set tdf = dbs.TableDefs("myTable")
Set fld = tdf.CreateField("myField", dbBoolean)
tdf.Fields.Append fld
fld.Properties.Append fld.CreateProperty("DisplayControl", dbInteger, CInt(acCheckBox))

However, when the table is created dynamically, it does not work.

Set dbs = CurrentDb()
Set tdf = dbs.CreateTableDef("myTable")
Set fld = tdf.CreateField("myField", dbBoolean)
tdf.Fields.Append fld
fld.Properties.Append fld.CreateProperty("DisplayControl", dbInteger, CInt(acCheckBox))
dbs.TableDefs.Append tdf

Does anybody know what is wrong with this code? Or if it is possible at all to set the checkbox property when the table is not yet created.

CodePudding user response:

This works for me:

Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field

Set dbs = CurrentDb()
Set tdf = dbs.CreateTableDef("myTable")

Set fld = tdf.CreateField("myField1", dbBoolean)
tdf.Fields.Append fld
dbs.TableDefs.Append tdf
fld.Properties.Append fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)

Set fld = tdf.CreateField("myField2", dbBoolean)
tdf.Fields.Append fld
fld.Properties.Append fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)
  • Related