M
MRM256
Guest
I have the following function.
Public Function dt_AccessTblRelationships(ByVal strCnn As String, _
ByVal strTbl As String) _
As DataTable
'Purpose: Find all relationships for the database table entered
'Parameters: strCnn As String - Properly built MS Access
' connection string
' strTbl As String - Database table name whose
' relationships we are seeking.
'Returns: A DataTable od Relationships
Dim schemaTable As DataTable
Dim restrictions() As String
Dim dr As DataRow
Call dt_Remove_Rel()
dt_Rel.Clear()
'Creating New columns for dt_Rel
dt_Rel.Columns.Add("PK_Tbl_Name", _
Type.GetType("System.String"))
dt_Rel.Columns.Add("PK_Col_Name", _
Type.GetType("System.String"))
dt_Rel.Columns.Add("FK_Tbl_Name", _
Type.GetType("System.String"))
dt_Rel.Columns.Add("FK_Col_Name", _
Type.GetType("System.String"))
Using cnn As New OleDbConnection(strCnn)
Try
With cnn
.Open()
restrictions = {Nothing, Nothing, Nothing, _
Nothing, Nothing, strTbl}
schemaTable = .GetOleDbSchemaTable( _
OleDbSchemaGuid.Foreign_Keys, _
restrictions)
.Close()
'Call dt_Read(schemaTable)
'Loop through the schemaTable
For RowCount = 0 To schemaTable.Rows.Count - 1
If InStr(UCase(schemaTable.Rows(RowCount)! _
PK_TABLE_NAME.ToString), "MSYS") Then
Continue For
Else
dr = dt_Rel.NewRow
dr.Item("PK_Tbl_Name") = _
schemaTable.Rows(RowCount)! _
PK_TABLE_NAME.ToString()
dr.Item("PK_Col_Name") = _
schemaTable.Rows(RowCount)! _
PK_COLUMN_NAME.ToString()
dr.Item("FK_Tbl_Name") = _
schemaTable.Rows(RowCount)! _
FK_TABLE_NAME.ToString()
dr.Item("FK_Col_Name") = _
schemaTable.Rows(RowCount)! _
FK_COLUMN_NAME.ToString()
dt_Rel.Rows.Add(dr)
End If
Next RowCount
'Debug Purposes
'Call dt_Read(dt_Rel)
End With
Catch ex As Exception
'Log error
Dim el As New Log.ErrorLogger
el.WriteToErrorLog(ex.Message, ex.StackTrace, "Error")
Return Nothing
End Try
End Using
Return dt_Rel
End Function
This functions returns all the relationships for the entered table, like so:

In this example the table name sent to the function is tblCustomers. There are two tables that have foreign keys to the customers table. tblCountries with primary key CountryID and tblStates with primary key StateID. I know this has something to do with the restrictions parameter, but I haven't found any references on how to adjust this parameter.
restrictions = {Nothing, Nothing, Nothing, _
Nothing, strColumn, strTbl}
Does anyone know where this reference is located?<sub></sub><sup></sup><strike></strike>
Thanks,
MRM256
Continue reading...
Public Function dt_AccessTblRelationships(ByVal strCnn As String, _
ByVal strTbl As String) _
As DataTable
'Purpose: Find all relationships for the database table entered
'Parameters: strCnn As String - Properly built MS Access
' connection string
' strTbl As String - Database table name whose
' relationships we are seeking.
'Returns: A DataTable od Relationships
Dim schemaTable As DataTable
Dim restrictions() As String
Dim dr As DataRow
Call dt_Remove_Rel()
dt_Rel.Clear()
'Creating New columns for dt_Rel
dt_Rel.Columns.Add("PK_Tbl_Name", _
Type.GetType("System.String"))
dt_Rel.Columns.Add("PK_Col_Name", _
Type.GetType("System.String"))
dt_Rel.Columns.Add("FK_Tbl_Name", _
Type.GetType("System.String"))
dt_Rel.Columns.Add("FK_Col_Name", _
Type.GetType("System.String"))
Using cnn As New OleDbConnection(strCnn)
Try
With cnn
.Open()
restrictions = {Nothing, Nothing, Nothing, _
Nothing, Nothing, strTbl}
schemaTable = .GetOleDbSchemaTable( _
OleDbSchemaGuid.Foreign_Keys, _
restrictions)
.Close()
'Call dt_Read(schemaTable)
'Loop through the schemaTable
For RowCount = 0 To schemaTable.Rows.Count - 1
If InStr(UCase(schemaTable.Rows(RowCount)! _
PK_TABLE_NAME.ToString), "MSYS") Then
Continue For
Else
dr = dt_Rel.NewRow
dr.Item("PK_Tbl_Name") = _
schemaTable.Rows(RowCount)! _
PK_TABLE_NAME.ToString()
dr.Item("PK_Col_Name") = _
schemaTable.Rows(RowCount)! _
PK_COLUMN_NAME.ToString()
dr.Item("FK_Tbl_Name") = _
schemaTable.Rows(RowCount)! _
FK_TABLE_NAME.ToString()
dr.Item("FK_Col_Name") = _
schemaTable.Rows(RowCount)! _
FK_COLUMN_NAME.ToString()
dt_Rel.Rows.Add(dr)
End If
Next RowCount
'Debug Purposes
'Call dt_Read(dt_Rel)
End With
Catch ex As Exception
'Log error
Dim el As New Log.ErrorLogger
el.WriteToErrorLog(ex.Message, ex.StackTrace, "Error")
Return Nothing
End Try
End Using
Return dt_Rel
End Function
This functions returns all the relationships for the entered table, like so:

In this example the table name sent to the function is tblCustomers. There are two tables that have foreign keys to the customers table. tblCountries with primary key CountryID and tblStates with primary key StateID. I know this has something to do with the restrictions parameter, but I haven't found any references on how to adjust this parameter.
restrictions = {Nothing, Nothing, Nothing, _
Nothing, strColumn, strTbl}
Does anyone know where this reference is located?<sub></sub><sup></sup><strike></strike>
Thanks,
MRM256
Continue reading...