bradyboyy88
Well-known Member
- Joined
- Feb 25, 2015
- Messages
- 562
I have a function I put together which allows you to connect to an ado database. I am having trouble figuring out how to adapt this to allow queries across multiple databases considering the connection string only includes Data Source parameter with one database directory to link to. If this is possble what would a sample sql query look like to select and intersect two tables in two seperate databases?
Code:
Public Sub SQLOpenDatabaseConnection(StrDBPath As String, EngineType As Integer)
'Define Connection String by inputting StrDBPath into a larger string (Works for Excel DB)
'Define Connection String by inputting StrDBPath into a larger string
'Access Support for engine type
If EngineType = 0 Then
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & StrDBPath & ";" & _
"Jet OLEDB:Engine Type=5;" & _
"Persist Security Info=False;Mode=Share Exclusive;"
'Excel Support for engine type
ElseIf EngineType = 1 Then
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & StrDBPath & "';" & _
"Extended Properties=""Excel 12.0;HDR=YES;ReadOnly=0;"";"
End If
RetryConnection:
Sleep 100
DoEvents
On Error GoTo ErrorHandler
'Connect to the database
20 oConn.Open sConn
On Error GoTo 0
Exit Sub
ErrorHandler:
'triggered by connection error. Most likely locking type
'MsgBox "looks like we had a connection error. The error number is " & Err.Number & " and the description is " & Err.Description & " on line " & Erl
Err.Clear
Resume RetryConnection
End Sub
'Return a query as a recordset
Public Function SQLQueryDatabaseRecordset(SQLQuery As String) As ADODB.Recordset
'Create RecordSet
Set oRs = CreateObject("ADODB.Recordset")
oRs.LockType = adLockBatchOptimistic
Sleep 100
DoEvents
'Open Record Set by executing SQL
oRs.Open SQLQuery, oConn
'Disconnect the recordset
Set oRs.ActiveConnection = Nothing
'Return recordset
Set SQLQueryDatabaseRecordset = oRs
Set oRs = Nothing
End Function