PDA

View Full Version : how to use ado to get primary key information?


laserdisk
05-21-02, 08:54
I've tried the following function, which someone else wrote to use, but to no avail?

Dim cn As New ADODB.Connection
strUrl = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=VAPMiningBase;Data Source=server"
cn.Open strUrl

Public Function FindPK(TableName As String) As Variant
Dim cat As ADOX.Catalog, key As ADOX.key, tbl As ADOX.Table, col As ADOX.Column

Set cat = New ADOX.Catalog
cat.ActiveConnection = cn
Set tbl = cat.Tables(TableName)


For Each key In tbl.Keys
If key.Type = adKeyPrimary Then
FindPK = key.Name
'this is some other information that may be useful
Debug.Print " Key Name = " & key.Name
Debug.Print " Key Type = " & key.Type
Debug.Print "Num columns = " & key.Columns.Count

For Each col In key.Columns
Debug.Print "Column Name = " & col.Name
Next col

Exit Function
End If
FindPK = Null
Next key
End Function

when function execute key.Columns.Count,run error "3251" occured
"The operation requested by the application is not supported by the provider"
why,what means is it?
how can i get primary key?
who can help me,Thanks a lot

Melly
06-25-03, 21:50
I am having problems accessing my information off Access database from my Visual Basic program.

Can someone show me the coding - mine is not working!