Public Function LinkOracleTable(sLocalName As String, sForeignName As String, Optional bShowErrors As Boolean = False) As Boolean
Dim sSQL As String
Dim oRS As ADODB.Recordset
Dim bAlreadyLinked As Boolean
Dim bCreateLink As Boolean
Dim bDeleteLink As Boolean
Dim msg As String
Dim sTemp As String
If sLocalName = "" Then sLocalName = sForeignName
sSQL = "SELECT Name, Type, Database, ForeignName from MSysObjects WHERE Name='" & sLocalName & "'; "
Set oRS = New ADODB.Recordset
oRS.Open sSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
If Not oRS.EOF Then
If oRS.Fields("Type") = 4 Then
bDeleteLink = True
bCreateLink = True
Else
' name exists but is not attached
If bShowErrors Then
MsgBox "Failed to link to table '" & sForeignName & "' as " & sLocalName & vbCrLf & "This is required to ensure the most up to date database table is linked", vbCritical, "Delete Link"
Else
Debug.Print "Failed to link to table '" & sForeignName & "' as " & sLocalName
End If
End If
Else
bCreateLink = True
End If
If bDeleteLink Then
' linked but to a different name
Err.Number = 0
If GetObjectType(sLocalName) = 4 Then DoCmd.DeleteObject acTable, sLocalName
If Err.Number <> 0 Then
If bShowErrors Then
MsgBox "Failed to delete existing table '" & sLocalName & "'" & vbCrLf & "This is required to ensure the most up to date database table is linked", vbCritical, "Delete Link"
Else
Debug.Print "Failed to delete existing table '" & sLocalName & "'"
End If
GoTo exit_linkoracletable
Else
Debug.Print "Deleted existing link '" & sLocalName & "'"
End If
End If
If bCreateLink Then
' now create link to table
'On Error Resume Next
sTemp = gsConnectString
DoCmd.TransferDatabase acLink, "ODBC Database", sTemp, acTable, sForeignName, sLocalName
If Not GetObjectType(sLocalName) = 4 Then
If bShowErrors Then
MsgBox "Failed to link to table '" & sForeignName & "' as '" & sLocalName & "'" & vbCrLf & "This is required to ensure the most up to date database table is linked", vbCritical, "Delete Link"
Else
Debug.Print "Failed to link to table '" & sForeignName & "' as '" & sLocalName & "'"
End If
GoTo exit_linkoracletable
Else
Debug.Print "Linked to table '" & sForeignName & "' as '" & sLocalName & "'"
End If
End If
LinkOracleTable = True
exit_linkoracletable:
Exit Function
err_linkoracletable:
GoTo exit_linkoracletable
End Function
Public Sub RelinkAllOracle()
Dim bRes As Boolean
bRes = True
DoCmd.SetWarnings False
bRes = LinkOracleTable("table1", "TEST.table1")
bRes = bRes And LinkOracleTable("table2", "TEST.table2")
bRes = bRes And LinkOracleTable("table3", "TEST.table3")
End Sub