'Created on workbook_open and destroyed on workbook_close
Public Sub SQLCreateDatabaseConnection()
'Create Connection
Set oConn = CreateObject("ADODB.Connection")
oConn.Mode = 3
oConn.CursorLocation = adUseClient
End Sub
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.15.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=NO;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
Public Sub SQLWriteDatabase(SQLQuery As String)
Sleep 100
DoEvents
'Open Record Set by executing SQL
oConn.Execute SQLQuery
End Sub