how to assign primary key to more than one field

G

gaffar

Hello Sir,
How to assign more than one primary key to table by this below following
code. i am able assign to a single field only. how to assign primary key to
more than one field. the primary key assigning code line is symboled kept
with in the *************** in the below code.

'Creating database

Dim cat As Catalog = New Catalog

cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source= " & Application.StartupPath & "\databases" & "\" & fname1 &
";" & "Jet OLEDB:Engine Type=5")

cat = Nothing

MsgBox("Database succesfully created")

End If

'creating tables in the database

'

' This code adds a single-field Primary key

'

Dim Cn As ADODB.Connection

Dim Cat2 As ADOX.Catalog

Dim objTable1, objtable2 As ADOX.Table

Cn = New ADODB.Connection

Cat2 = New ADOX.Catalog

objTable1 = New ADOX.Table

objtable2 = New ADOX.Table

'Open the connection

Try

Cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source= " & Application.StartupPath & "\databases" & "\" & fname1 &
";" & "Jet OLEDB:Engine Type=5")

'"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\sample\gaffar.mdb"

Catch ex As Exception

MsgBox(ex.Message)

End Try

'Open the Catalog

Cat2.ActiveConnection = Cn

'Create the table

objTable1.Name = "Test_Table"

objtable2.Name = "Test_Table2"

'Create and Append a new field to the "Test_Table" Columns Collection

objTable1.Columns.Append("company", DataTypeEnum.adVarWChar)

objTable1.Columns.Append("contact", DataTypeEnum.adVarWChar)


'Create and Append a new key. Note that we are merely passing

'the "PimaryKey_Field" column as the source of the primary key. This

'new Key will be Appended to the Keys Collection of "Test_Table"

'dsclient.Tables(0).PrimaryKey = New DataColumn

'{dsClient.Tables(0).Columns("FirstName"),

'dsClient.Tables(0).Columns("LastName")}



****************************************************************************
*****************

objTable1.Keys.Append("PrimaryKey", KeyTypeEnum.adKeyPrimary, "company")

****************************************************************************
*****************



'Append the newly created table to the Tables Collection

Try

Cat2.Tables.Append(objTable1)

Catch ex As Exception

MsgBox(ex.Message)

End Try

Cat2.Tables.Append(objtable2)

' clean up objects

' objKey = Nothing

objTable1 = Nothing

objtable2 = Nothing

Cat2 = Nothing
 

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