如何在Make Table查询中添加Yes / No列

时间:2018-08-09 18:27:00

标签: sql ms-access-2010

我有一个当前的Make Table查询。我想向该查询添加一列,以在新表中使用复选框创建是/否字段。我还希望默认选中所有复选框(或“是”值)。我不确定如何添加到SQL语句中以正确创建该方案。这是我当前的代码,其中没有新的“可报告”列:

SELECT 
    tChart1.[Original Except No] AS [Reportable Except No], 
    tChart1.[Original Document Number] AS [Reportable Document Number],
    tChart1.[Original Travler] AS [Reportable Travler], 
    tChart1.[Original Exception Amount] AS [Reportable Exception Amount],
    tChart1.[Original Destination] AS [Reportable Destination], 
    tChart1.[Original Exception Nights] AS [Reportable Exception Nights],
    tChart1.Explanation AS [Reportable Explanation], 
    tChart1.Reason AS [Reportable Reason], 
    tChart1.[Original Comments] AS [Reportable Comments], 
    tChart1.[Original Campus] AS [Reportable Campus], 
    tChart1.[Original Date] AS [Reportable Date], 
    tChart1.[Original Document Number] & "+" & [Original Travler] & "=" & [Original Exception amount] & "+" & [original destination] AS ID 
INTO tChart1_Reportable
FROM tChart1;

1 个答案:

答案 0 :(得分:0)

SELECT ... INTO查询根本无法执行您想要的操作。

您可以使用DDL添加列并设置其DEFAULT

CurrentProject.Connection.Execute "ALTER TABLE tChart1_Reportable ADD COLUMN Reportable YESNO DEFAULT -1;"

然后执行UPDATE将True存储在现有行中:

CurrentProject.Connection.Execute "UPDATE tChart1_Reportable SET Reportable = True;"

但是,如果您希望该字段显示为复选框,则这些步骤可能不够。您可以在“设计视图”中打开表格,选择字段,然后选择“查找”选项卡,然后从“显示控件”下拉列表中选择“复选框”。

您可以改为使用VBA代码...

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim prp As DAO.Property

Set db = CurrentDb
Set tdf = db.TableDefs("tChart1_Reportable")
Set fld = tdf.Fields("Reportable")
For Each prp In fld.Properties
    If prp.Name = "DisplayControl" Then
        Exit For
    End If
Next
If prp Is Nothing Then
    Set prp = fld.CreateProperty("DisplayControl", 3, 106)
    fld.Properties.Append prp
Else
    fld.Properties("DisplayControl").Value = 106
End If