Skip to content

Instantly share code, notes, and snippets.

@pudelosha
Last active April 1, 2022 08:33
Show Gist options
  • Save pudelosha/0704ac594b8dd87007ce4d403eaa2b6c to your computer and use it in GitHub Desktop.
Save pudelosha/0704ac594b8dd87007ce4d403eaa2b6c to your computer and use it in GitHub Desktop.
VBA - class to import the data from another MS Excel workbook (ADODB method)
Option Explicit
Private strUser As String
Private strProvider As String
Private strPath As String
Private strProperties As String
Private strSQL As String
Private strPassword As String
Private con As ADODB.Connection
Property Let ConnProvider(strCPR As String)
strProvider = strCPR
End Property
Property Let ConnPath(strCPA As String)
strPath = strCPA
End Property
Property Let ConnProperties(strCPP As String)
strProperties = strCPP
End Property
Property Let SQLString(strSQLQuery As String)
strSQL = strSQLQuery
End Property
Property Let SQLPassword(strSQLPassword As String)
strPassword = strSQLPassword
End Property
Public Property Get RecordsetADODB(strSQL As String) As ADODB.Recordset
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open strSQL, con, adOpenStatic, adLockReadOnly
Set RecordsetADODB = rst
End Property
Public Property Get ConnStatus() As Integer
ConnStatus = con.State
End Property
Public Property Get RecordsetArray(strSQL As String) As Variant
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open strSQL, con, adOpenStatic, adLockReadOnly
RecordsetArray = rst.GetRows
End Property
Public Property Get ListSheets() As Variant
Dim i As Integer
Dim rst As ADODB.Recordset
Dim lngColumnCount As Long
Dim strColumnName As String
Dim varItems As Variant
Set rst = con.OpenSchema(adSchemaTables)
lngColumnCount = rst.RecordCount
ReDim varItems(0)
For i = 1 To lngColumnCount
ReDim Preserve varItems(1 To UBound(varItems) + 1)
varItems(UBound(varItems)) = rst.Fields("TABLE_NAME").Value
rst.MoveNext
Next i
ListSheets = varItems
End Property
Public Property Get ListFields(strTableName As String) As Variant
Dim i As Integer
Dim rst As ADODB.Recordset
Dim intColumnCount As Integer
Dim varItems As Variant
Set rst = con.OpenSchema(adSchemaColumns, Array(Empty, Empty, strTableName, Empty))
intColumnCount = rst.RecordCount
ReDim varItems(0)
For i = 1 To intColumnCount
ReDim Preserve varItems(1 To UBound(varItems) + 1)
varItems(UBound(varItems)) = rst.Fields("COLUMN_NAME").Value
rst.MoveNext
Next i
ListFields = varItems
End Property
Sub OpenConnection()
Dim strConnStr As String
If strProvider = "" Or strPath = "" Or strProperties = "" Then
MsgBox "Connection parameters were not provided."
Exit Sub
Else
strConnStr = "Data Source=" & strPath & "; Extended Properties='" & strProperties & "'"
With con
.Provider = strProvider
.ConnectionString = strConnStr
.CursorLocation = adUseClient
.Open
End With
End If
End Sub
Sub CloseConnection()
con.Close
End Sub
Private Sub Class_Initialize()
Set con = New ADODB.Connection
strUser = Environ("username")
End Sub
Private Sub Class_Terminate()
Set con = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment