Re: Adding a key to MS Access Table

From: harry (harry_at_nospam)
Date: 04/09/04


Date: Fri, 9 Apr 2004 12:21:41 +1000


Below is copy of an old post. This may help you get started.

Hi Frank,

I tried both your sample and the post by Josef on 02/13/2004, however as
weird as it appears, all solutions still return error: "Item is ReadOnly"

I searched net and all samples I found were same/similat to both your's and
MSDN

However, for your ref and anyone else pulling their hair out with this crazy
problem, I found (stumbled across) a solution...

.Item("Reference").ParentCatalog = cat
.Item("Reference").Properties("AutoIncrement").Value = True

'and if you like to set seed + increment values...

.Item("Reference").Properties.Item("Seed").Value = 1 'or whatever your
preference
.Item("Reference").Properties.Item("Increment").Value = 1 'or whatever your
preference

Regards
Harry

"Frank Hickman" <fhickman_nosp@m_noblesoft.com> wrote in message
news:PNGdnYYZcr6CVt_dRVn-uw@comcast.com...
> Try adding this line prior to setting the property...
>
> .Item("ContactId").ParentCatalog = catDB;
>
> My VB is a little rusty so you may have to tweak it :)
>
>
> Sub CreateAutoNumberField(strDBPath As String)
> Dim catDB As ADOX.Catalog
> Dim tbl As ADOX.Table
>
> Set catDB = New ADOX.Catalog
> ' Open the catalog.
> catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & strDBPath
>
> Set tbl = New ADOX.Table
> With tbl
> .Name = "Contacts"
> Set .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.
> .Item("ContactId").ParentCatalog = catDB;
> .Item("ContactId").Properties("AutoIncrement") = True
> .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
>
> Set catDB = Nothing
> End Sub
>
> This solution was posted by Josef Blösl on 02/13/2004 in a reply to my
reply
> about the same subject. My solution was somewhat different so if the
above
> does not work for you, you may want to try that instead. Which was to go
> ahead and append the table to the catalog and then set the column
property.
> But his solution should work as appending the table essentially does this
> too.
>
> HTH
> --
> ============
> Frank Hickman
> NobleSoft, Inc.
> ============
> Replace the _nosp@m_ with @ to reply.
>
>
> "harry" <harry@nospam> wrote in message
> news:uNA2FOz$DHA.1464@tk2msftngp13.phx.gbl...
> > Previously posted on microsoft.public.data.oledb
> >
> > Hi,
> >
> > I'm trying to create AutoIncrement column in a new Access database table
> > via ADOX.
> > I am using MSDN sample code however I still receive error: Property
> 'Item'
> > is 'ReadOnly'
> >
> > The line triggering the error is:
> > .Item("ContactId").Properties("AutoIncrement") = True
> >
> > I'm using MDAC 2.8 and VB.NET
> >
> > How do I create AutoIncrement column when creating new table using ADOX
?
> >
> > Thanks
> > Harry
> >
> > This is the Sample I tried...
> >
> >
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odeopg/html/deovrcreatingmodifyingaccesstables.asp
> >
> > Sub CreateAutoNumberField(strDBPath As String)
> > Dim catDB As ADOX.Catalog
> > Dim tbl As ADOX.Table
> >
> > Set catDB = New ADOX.Catalog
> > ' Open the catalog.
> > catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> > "Data Source=" & strDBPath
> >
> > Set tbl = New ADOX.Table
> > With tbl
> > .Name = "Contacts"
> > Set .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.
> > .Item("ContactId").Properties("AutoIncrement") = True
> > .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
> >
> > Set catDB = Nothing
> > End Sub
> >
> >
> >
> >
> >
> >
>
>

"Art" <arthlan@yahoo.com> wrote in message
news:E99CD7F0-BA9C-4171-BF0F-EA3D987C01EA@microsoft.com...
> Hi everyone,
>
> I was hoping someone might be able to help me with this. I'm just
starting to try to work with MS Access tables through VB.net. In Access I
can take an existing table and add a new field with the type AutoNumber. I
can then set this up as a key. Then if I go into the table I will see
sequential numbers have been inserted into that field for me. Can I do this
through VB.net?
>
> I'd appreciate any help or alternative suggestions.
>
> Thanks very much,
>
> Art



Relevant Pages

  • Re: How do I create an AutoIncrement column with ADOX ?
    ... Sub CreateAutoNumberField ... Dim catDB As ADOX.Catalog ... Dim tbl As ADOX.Table ... Set catDB = New ADOX.Catalog ...
    (microsoft.public.data.ado)
  • Re: How do I create an AutoIncrement column with ADOX ?
    ... > Sub CreateAutoNumberField ... > Dim tbl As ADOX.Table ... > ahead and append the table to the catalog and then set the column ... >> Dim catDB As ADOX.Catalog ...
    (microsoft.public.data.ado)
  • How do I create an AutoIncrement column with ADOX ?
    ... How do I create AutoIncrement column when creating new table using ADOX? ... Sub CreateAutoNumberField ... Dim catDB As ADOX.Catalog ... Dim tbl As ADOX.Table ...
    (microsoft.public.data.oledb)
  • Re: UnHide All the Database Objects
    ... > Sub Unhide_Tables ... >> Does anyone know code to unhide all the hidden objects in the database? ... >> Dim tbl As DAO.TableDef ...
    (microsoft.public.access.modulesdaovba)
  • Re: read sheetnames with ADO
    ... This ignores sheet names with embedded spaces, they get seen as type TABLE ... > Sub Demo() ... > Dim cnn As New ADODB.Connection ... > Dim tbl As ADOX.Table ...
    (microsoft.public.excel.programming)