Creating table in Access database

E

Eric Carr

I have been trying to create a table in an Access database via a VB.NET
program and I am running into a problem defining an autoincrement field. I
am getting an error saying "Property 'Item' is 'ReadOnly'" on the line that
SHOULD be turning this property on for the ID field. Does any one have any
suggestions on how to do this in .NET?

The code that I am using is:
Dim cat As Catalog = New Catalog
Dim tblNew As ADOX.Table = New ADOX.Table
Dim colNew As ADOX.Column = New ADOX.Column

cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=Reservations.mdb;" & _
"Jet OLEDB:Engine Type=5")

' Create a new Table object.
With tblNew
.Name = "tblCabin"

' Create fields and append them to the columns collection of the new
Table object.

With .Columns
.Append("ID", DataTypeEnum.adInteger)
.Item("ID").ParentCatalog = cat
.Item("ID").Properties("AutoIncrement") = True '
This is the line that generates the error
.Append("Name", DataTypeEnum.adVarWChar, 50)
.Append("NameShort", DataTypeEnum.adVarWChar, 10)
End With
End With

' Add the new Table to the Tables collection of the database.
cat.Tables.Append(tblNew)
 
C

Cor Ligthert

Hi Eric,

You are using ADO that can be used in VB.net (however what not), but it not
Ado.Net.
Most of us are not using Ado anymore (I use it to create an access database,
because that is impossible with AdoNet)

However your problem can, so have a look for Adonet.

Here is a link to the resource kit to have some samples for that and more.
(There is also a sample in it using ADO when you want to keep that)

http://msdn.microsoft.com/vbasic/vbrkit/default.aspx

And if you have problems installing it

http://msdn.microsoft.com/vbasic/vbrkit/faq/#installvdir

I hope this helps a little bit?

Cor
 
P

Paul Clement

¤ I have been trying to create a table in an Access database via a VB.NET
¤ program and I am running into a problem defining an autoincrement field. I
¤ am getting an error saying "Property 'Item' is 'ReadOnly'" on the line that
¤ SHOULD be turning this property on for the ID field. Does any one have any
¤ suggestions on how to do this in .NET?
¤
¤ The code that I am using is:
¤ Dim cat As Catalog = New Catalog
¤ Dim tblNew As ADOX.Table = New ADOX.Table
¤ Dim colNew As ADOX.Column = New ADOX.Column
¤
¤ cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & _
¤ "Data Source=Reservations.mdb;" & _
¤ "Jet OLEDB:Engine Type=5")
¤
¤ ' Create a new Table object.
¤ With tblNew
¤ .Name = "tblCabin"
¤
¤ ' Create fields and append them to the columns collection of the new
¤ Table object.
¤
¤ With .Columns
¤ .Append("ID", DataTypeEnum.adInteger)
¤ .Item("ID").ParentCatalog = cat
¤ .Item("ID").Properties("AutoIncrement") = True '
¤ This is the line that generates the error
¤ .Append("Name", DataTypeEnum.adVarWChar, 50)
¤ .Append("NameShort", DataTypeEnum.adVarWChar, 10)
¤ End With
¤ End With
¤
¤ ' Add the new Table to the Tables collection of the database.
¤ cat.Tables.Append(tblNew)
¤

You should include the Value property:

Item("ID").Properties("AutoIncrement").Value = True

In addition, some of these properties may need to be set *before* appending the column to the
Columns collection.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top