1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| Public Function Open_MyRecordset_FromExcelSQL(strSQL$, ByRef rstReturn As ADODB.Recordset, WBPath$ = "ThisWorkBookCompletePath") As Boolean
On Error GoTo Err
Dim strDriver$
If TypeName(ConnEXCEL) = "Nothing" Then
If Right(WBPath, 3) = "xls" Then
strDriver = "DRIVER={Microsoft Excel Driver (*.5)}"
Else
strDriver = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)}"
End If
Set ConnEXCEL = New ADODB.Connection
With ConnEXCEL
.Provider = "MSDASQL"
.ConnectionString = strDriver & ";DBQ=" & WBPath & "; ReadOnly=True;"
.Open
End With
End If
rstReturn.Open strSQL, ConnEXCEL
Open_MyRecordset_FromExcelSQL = True
Fin:
Exit Function
Err:
MsgBox Err.Description
GoTo Fin
Resume
End Function |
Partager