1

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.

0

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

5 Comments

That is setting the checkbox property after the table is created, like in my first code example. My question was if setting this property is also possible before the table is created.
@esims - And the answer to that question is ... no.
That's how to do it - using a single Field object once.
So it requires looping your fields twice; the first time to create them, and a second time to change posible checkbox fields.
Be a bit creative. See extended answer, please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.