Excel - Power Query - Power Pivot - VBA
Share
Explore
VB Excel

icon picker
SQL & VBA

Function LoadCn(Optional sSource As String) As ADODB.Connection
Dim cn As New ADODB.Connection
If sSource = "" Then
sSource = ThisWorkbook.Path & "\" & ThisWorkbook.Name
End If
cn.Open "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & sSource
Set LoadCn = cn
End Function

Function LoadRst(sSQL As String, Optional sSource As String, Optional sMsgError As String) As ADODB.Recordset
Dim cn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Set cn = LoadCn(sSource)
On Error Resume Next
rst.Open sSQL, cn, adOpenStatic, adLockOptimistic
If Err <> 0 Then
sMsgError = Err.Description
If IsDebugMode Then
MsgBox Err.Description, vbExclamation
Stop
End If
Else
Set LoadRst = rst
End If
End Function
Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.