在使用ADOX在Access中添加列时,如何设置所需的属性?

时间:2011-09-28 15:20:44

标签: ms-access vba

我有以下脚本在Access数据库中添加列和表。我不知道如何将该列的“required”属性设置为“NO”,默认为yes。这是我的剧本:

Option Compare Database

Function AddColumns()
'Purpose:   Show how to add fields to a table, and delete them using ADOX.
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim col As New ADOX.Column

Set cnn = CreateObject("ADODB.Connection")

cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
  "Data Source=\\network\drive\TestDB.accdb; Jet OLEDB:Database Password=testing; "

'Initialize
cat.ActiveConnection = cnn
Set tbl = cat.Tables("Test_Table")

'Add a new column
With col
    .Name = "Test_Column"
    .Type = adVarWChar   'Decimal type.
    '.Precision = 28     '28 digits.
    '.NumericScale = 8   '8 decimal places.

End With
tbl.Columns.Append col
MsgBox col.Name & " successfully added"
Set col = Nothing
'Debug.Print "Column added."


'Clean up
Set col = Nothing
Set tbl = Nothing
Set cat = Nothing

End Function

1 个答案:

答案 0 :(得分:2)

您需要使用Attributes Property (ADOX)

例如

With col
   ...
   .Attributes = adColNullable
   ...
End With