How do you set the required property when adding a column in Access using ADOX?
I have the following script tha开发者_JAVA技巧t adds columns to and table in an Access database. I don't know how to set the "required" property of that column to "NO", the default is yes. Here is my script:
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
You need to use the Attributes Property (ADOX)
e.g
With col
...
.Attributes = adColNullable
...
End With
精彩评论