Function ConnectOracleTable(strTblName As String, _
sServer As String, sUID As String, sPWD As String) As Boolean
On Error GoTo Connect_Err
Dim strConn As String
Dim db As Database
Dim tbl As TableDef
Set db = CurrentDb()
strConn = "ODBC;"
strConn = strConn & "DRIVER={Microsoft ODBC Driver for Oracle};"
strConn = strConn & "Server=" & sServer & ";"
strConn = strConn & "UID=" & sUID & ";"
strConn = strConn & "PWD=" & sPWD & ";"
If (DoesTblExist(strTblName) = False) Then
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, strTblName, _
strConn)
db.TableDefs.Append tbl
Else
Set tbl = db.TableDefs(strTblName)
tbl.Connect = strConn
tbl.RefreshLink
End If
ConnectOracleTable = True
Connect_Exit:
Set tbl = Nothing
Set db = Nothing
Exit Function
Connect_Err:
ConnectOracleTable = False
MsgBox Err & " - " & Error & vbCrLf & "Table attach failed."
Resume Connect_Exit
End Function
'***************************************************************
'The DoesTblExist function validates the existence of a TableDef
'object in the current database. The result determines if an
'object should be appended or its Connect property refreshed.
'***************************************************************
Function DoesTblExist(strTblName As String) As Boolean
On Error Resume Next
Dim db As Database, tbl As TableDef
Set db = CurrentDb
Set tbl = db.TableDefs(strTblName)
If Err.Number = 3265 Then ' Item not found.
DoesTblExist = False
Exit Function
End If
DoesTblExist = True
Set tbl = Nothing
Set db = Nothing
End Function
|