Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access ALTER TABLE:添加带有默认值和复选框的新布尔列_Ms Access_Ddl_Alter Table - Fatal编程技术网

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

Ms access ALTER TABLE:添加带有默认值和复选框的新布尔列,ms-access,ddl,alter-table,Ms Access,Ddl,Alter Table,向表中添加布尔数据类型列的正确访问DDL查询是什么 到目前为止,我已经看到了如下示例 ALTER TABLE MyTable ADD MyNewColumName BIT 但他们似乎并不是100%正确的 Access不会将复选框控件应用于新添加的列,并且 该列的允许值似乎是0和-1 在access中,是/否数据类型是一个逻辑字段,可以显示是/否、真/假或开/关。查看VBA代码时,true和false常量等效于-1和0 如果使用此字段填充复选框,它将正常工作 您可以将alter语句更改为使用“Y

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

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

ALTER TABLE MyTable ADD MyNewColumName BIT
但他们似乎并不是100%正确的

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

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

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

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

    这将在access table列中为您提供所需的复选框。

    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
    

    From:

    恐怕不会,复选框只能通过DAO获得。这个问题不是关于设置字段的数据类型,而是关于设置默认的显示控件(您的标题有误导性)。正如@Remou在下面所说的,这不是DDL可以设置的。
    ''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