Last active
January 27, 2017 15:48
-
-
Save pudelosha/1e1ea38113d29388cde6ca0cbebccc9f to your computer and use it in GitHub Desktop.
DAO - class to query MS Excel internally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Private db As DAO.Database | |
Private rst As DAO.Recordset | |
Private strName As String | |
Private blnOptions As Boolean | |
Private blnReadOnly As Boolean | |
Private strConnect As String | |
' | |
' required libraries | |
' Microsoft Office xx.x Access database engine Objects | |
' | |
Property Let DbName(name As String) | |
strName = name | |
End Property | |
Property Let DbOptions(options As Boolean) | |
blnOptions = options | |
End Property | |
Property Let DbReadOnly(readonly As Boolean) | |
blnReadOnly = readonly | |
End Property | |
Property Let DbConnect(connect As String) | |
strConnect = connect | |
End Property | |
Private Sub Class_Initialize() | |
strName = ThisWorkbook.FullName | |
End Sub | |
Private Sub Class_Terminate() | |
Set db = Nothing | |
Set rst = Nothing | |
End Sub | |
Sub DbOpen() | |
If strName = "" Then | |
MsgBox "Database parameters were not provided." | |
Exit Sub | |
Else | |
On Error Resume Next | |
Set db = OpenDatabase(strName, blnOptions, blnReadOnly, strConnect) | |
If Err.Number <> 0 Then | |
MsgBox "Database was not opened!", vbCritical, "DB Error" | |
Exit Sub | |
End If | |
On Error GoTo 0 | |
End If | |
End Sub | |
Public Property Get DbRecordset(strSQL As String) As DAO.Recordset | |
Set rst = db.OpenRecordset(strSQL) | |
Set DbRecordset = rst | |
End Property | |
Public Property Get ListTables() As Variant | |
Dim varItems As Variant | |
Dim tdf As DAO.TableDef | |
ReDim varItems(0 To 0) | |
For Each tdf In db.TableDefs | |
If Not (tdf.name Like "MSys*" Or tdf.name Like "~*") Then | |
ReDim Preserve varItems(1 To UBound(varItems) + 1) | |
varItems(UBound(varItems)) = tdf.name | |
End If | |
Next | |
ListTables = varItems | |
End Property |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment