กระทู้เก่าบอร์ด อ.สุภาพ ไชยา
320 1
URL.หัวข้อ /
URL
Finding Primary Key(s) in a table
ถามไว้ที่ http://www.utteraccess.com/forums/showflat.php?Board=access_2000&Number=273822
ใช้ DAO
Function DAOFindPrimaryKey(strTable As String)
Dim dbs As Database, tdf As TableDef, fld As Field
Dim idx As Index
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(strTable)
For Each idx In tdf.Indexes
If idx.Primary Then
For Each fld In idx.Fields
Debug.Print fld.Name
Next fld
tdf.Indexes.Refresh
End If
Next idx
Set dbs = Nothing
End Function
ใช้ ADO
Sub ADOFindPrimaryKey(strTable As String)
Dim cat As Object
Dim tbl As Object
Dim idx As Object
Dim col As Object
Set idx = CreateObject("ADOX.Index")
Set cat = CreateObject("ADOX.Catalog")
cat.ActiveConnection = CurrentProject.Connection
Set tbl = CreateObject("ADOX.Table")
Set tbl = cat.Tables(strTable)
For Each idx In tbl.Indexes
With idx
For Each col In .Columns
If .PrimaryKey = True Then
Debug.Print col.Name
End If
Next col
End With
Next idx
Set cat = Nothing
End Sub
ใช้ DAO
Function DAOFindPrimaryKey(strTable As String)
Dim dbs As Database, tdf As TableDef, fld As Field
Dim idx As Index
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(strTable)
For Each idx In tdf.Indexes
If idx.Primary Then
For Each fld In idx.Fields
Debug.Print fld.Name
Next fld
tdf.Indexes.Refresh
End If
Next idx
Set dbs = Nothing
End Function
ใช้ ADO
Sub ADOFindPrimaryKey(strTable As String)
Dim cat As Object
Dim tbl As Object
Dim idx As Object
Dim col As Object
Set idx = CreateObject("ADOX.Index")
Set cat = CreateObject("ADOX.Catalog")
cat.ActiveConnection = CurrentProject.Connection
Set tbl = CreateObject("ADOX.Table")
Set tbl = cat.Tables(strTable)
For Each idx In tbl.Indexes
With idx
For Each col In .Columns
If .PrimaryKey = True Then
Debug.Print col.Name
End If
Next col
End With
Next idx
Set cat = Nothing
End Sub
1 Reply in this Topic. Dispaly 1 pages and you are on page number 1
1 @R06784
Time: 0.1377s