Hi guy,
would you help me, how to create table (ACCESS) with primary key in visual Basic 6?

Set catNewDB = CreateObject("ADOX.Catalog")
catNewDB.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDatabasetocreate & _
";Jet OLEDB:Engine Type=5;"
' Engine Type=5 = Access 2000 Database
' Engine Type=4 = Access 97 Database
Set catNewDB = Nothing
Set catDB = CreateObject("ADOX.Catalog")
' Open the catalog
catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDatabasetocreate
' Create new Table
Set tblNew = CreateObject("ADOX.Table")
tblNew.Name = "DataPeserta"
Set col = CreateObject("ADOX.Column")

With col
.ParentCatalog = catDB
.Type = adVarWChar
.Name = "NOPOL"
.Properties("Description") = "I am the Description " & _
"for the column"
End With
tblNew.Columns.Append col

' Now add the rest of the columns
With tblNew
' Create fields and append them to the
' Columns collection of the new Table object.
With .Columns
.Append "NOMBER", adVarWChar
.Append "NAME", adVarWChar
.Append "ISSUE_ATE", adDate
.Append "BIRTH_DATE", adDate
.Append "GAJI", adSingle
End With

would you help me, how to create table (ACCESS) with primary key in visual Basic 6?

Well, your code is not completed since you never add all created columns to catalog(catDB) and also missing "End With" statement.
To create PK you need to append a new key :
I add two lines code below (with comments):

' Now add the rest of the columns
With tblNew
' Create fields and append them to the
' Columns collection of the new Table object.
With .Columns
    .Append "NOMBER", adVarWChar
    .Append "NAME", adVarWChar
    .Append "ISSUE_ATE", adDate
    .Append "BIRTH_DATE", adDate
    .Append "GAJI", adSingle
End With

'Create and Append a new key. Note that we are merely passing
'the "NOPOL" column as the source of the primary key. This
'new Key will be Appended to the Keys Collection of "Data Peserta"
.Keys.Append "PrimaryKey", adKeyPrimary, "NOPOL" ' Add this line to append PK

End With

'Append the newly created table to the Tables Collection
catDB.Tables.Append tblNew ' Since you didn't append table column to catDB

Edited 4 Years Ago by Jx_Man: n/a

This article has been dead for over six months. Start a new discussion instead.