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

OrE*_*lse 5 ddl ms-access alter-table

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

到目前为止,我已经看过如下例子......

ALTER TABLE MyTable ADD MyNewColumName BIT
Run Code Online (Sandbox Code Playgroud)

但是从那以后它们似乎没有100%正确

  1. Access不会将复选框控件应用于新添加的列,并且
  2. 该列的允许值似乎是0-1

Pat*_*ick 5

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

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

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

ALTER TABLE mytable ADD mynewcolumn YESNO
Run Code Online (Sandbox Code Playgroud)

这应该在访问表列中提供所需的复选框.


Fio*_*ala 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
Run Code Online (Sandbox Code Playgroud)

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