'How to create an MS Access table with a checkbox field in VBA

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.



Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1