Access autonumber creation with ADO
I copied the CreateAutoNumberField routine from MSDN and pasted it into a new project as shown. I've included "Microsoft ActiveX Data Objects Recordset 2.7 Library" msador15.dll and "Microsoft ADO Ext. 2.7 for DLL and Security" msadox.dll.
The line in red show the error "Property 'Item' is 'ReadOnly'".
Can anyone help?
VB Code:
Imports ADOX
Imports ADOX.DataTypeEnum
Imports ADOX.KeyTypeEnum
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Sub CreateAutoNumberField(ByVal strDBPath As String)
Dim catDB As ADOX.Catalog
Dim tbl As ADOX.Table
catDB = New ADOX.Catalog()
' Open the catalog.
catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strDBPath
tbl = New ADOX.Table()
With tbl
.Name = "Contacts"
.ParentCatalog = catDB
' Create fields and append them to the
' Columns collection of the new Table object.
With .Columns
.Append("ContactId", adInteger)
' Make the ContactId field auto-incrementing.
[COLOR=red] .Item("ContactId").Properties("AutoIncrement") = True[/COLOR]
.Append("CustomerID", adVarWChar)
.Append("FirstName", adVarWChar)
.Append("LastName", adVarWChar)
.Append("Phone", adVarWChar, 20)
.Append("Notes", adLongVarWChar)
End With
End With
' Add the new Table to the Tables collection of the database.
catDB.Tables.Append(tbl)
catDB = Nothing
End Sub
End Class