ALTER TABLE:添加一个带有默认值和复选框的新布尔列

时间:2011-03-09 12:54:10

标签: ms-access ddl alter-table

将Boolean数据类型列添加到表中的正确Access DDL查询是什么?

到目前为止,我已经看过以下示例......

ALTER TABLE MyTable ADD MyNewColumName BIT

但是,自

以来,它们似乎没有100%正确
  1. Access不会将复选框控件应用于新添加的列,并且
  2. 该列的允许值似乎为0-1

2 个答案:

答案 0 :(得分:5)

在访问中,是/否数据类型是一个逻辑字段,可以显示是/否,真/假或开/关。当您查看VBA代码时,true和false常量等效于-1和0.

如果您使用此字段填充复选框,它将正常运行。

您可以将alter语句更改为使用“YESNO”:

ALTER TABLE mytable ADD mynewcolumn YESNO

这应该会在访问表列中为您提供所需的复选框。

答案 1 :(得分:5)

DAO示例。

''Requires reference to Microsoft DAO 3.6 Object Library
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim db As Database
Dim strSQL As String


Set db = CurrentDb

''Create a table ...
strSQL = "Create Table tblLTD (TheYesNoCheck YesNo, TheYesNoCombo YesNo)"
db.Execute strSQL

''It is now in the table collection, so ...
Set tdf = db.TableDefs("tblLTD")

''Change the way the YesNo fields display.
''A Checkbox
Set fld = tdf.Fields("TheYesNoCheck")
Set prp = fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)
fld.Properties.Append prp

''A combobox
Set fld = tdf.Fields("TheYesNoCombo")
Set prp = fld.CreateProperty("DisplayControl", dbInteger, acComboBox)
fld.Properties.Append prp
''We will need a format
Set prp = fld.CreateProperty("Format", dbText, "Yes/No")
fld.Properties.Append prp

来自:http://wiki.lessthandot.com/index.php/Add_a_Display_Control_(Checkbox,_Combobox)_to_a_YesNo_Field