【问题标题】:ALTER TABLE: Add a new boolean column with default value and checkboxALTER TABLE:添加一个具有默认值和复选框的新布尔列
【发布时间】:2011-07-11 21:23:36
【问题描述】:

向表中添加布尔数据类型列的正确 Access DDL 查询是什么?

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

ALTER TABLE MyTable ADD MyNewColumName BIT

但它们似乎不是 100% 正确的,因为

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

【问题讨论】:

  • 这个问题不是关于设置字段的数据类型,而是关于设置默认显示控件(你的标题有误导性)。正如@Remou 下面所说,这不是 DDL 可以设置的。

标签: ms-access ddl alter-table


【解决方案1】:

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

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

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

ALTER TABLE mytable ADD mynewcolumn YESNO

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

【讨论】:

  • 不会的,恐怕只有DAO才能得到复选框。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多