Option Explicit
' ******************************************************************************
' This procedure retrieves ALL transactions (regardless of account) from the
' Access 2007 file (mData.accdb) and writes the data to the mData worksheet.
' This data is sorted by date (oldest to newest) in Access 2007 and is used with
' user queries which require all transactional data (not just account specific).
'
Sub GetINCTX()
' This macro requires reference to the Microsoft ActiveX Data Objects 2.x Library.
' *******************************************************************************
Dim DBFullName As String
Dim Cnct As String, Src As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
Dim Col As Integer
Dim LR As Long
' *******************************************************************************
' Turn off screen updating, select the appropriate sheet, and clear all cells.
Application.ScreenUpdating = False
Worksheets("Income").Activate
Cells.Clear
' *******************************************************************************
' The database path information is defined. Note the Access database MUST reside
' within the same directory as this workbook!
DBFullName = ThisWorkbook.Path & "\mdata.accdb"
' *******************************************************************************
' Open the connection to the data source.
Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.ACE.OLEDB.12.0;"
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Cnct
' *******************************************************************************
' Create the new RecordSet.
Set Recordset = New ADODB.Recordset
With Recordset
' Define the appropriate Filter(s) and notify the user of the selection criteria.
Src = "Select * from mData where Tag = 'GHI' "
Src = Src & "or Tag = 'DEF' "
Src = Src & "or Tag = 'LMN'"
.Open Source:=Src, ActiveConnection:=Connection
' Write the field names.
For Col = 0 To Recordset.Fields.Count - 1
Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name
Next
' Write the recordset.
Range("A1").Offset(1, 0).CopyFromRecordset Recordset
End With
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing
' *******************************************************************************
' Create and format the table from the Recordset.
LR = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$M" & LR), _
, xlYes).Name = "INC2tbl"
ActiveSheet.ListObjects("INC2tbl").ShowTotals = True
ActiveSheet.Columns.AutoFit
' *******************************************************************************
' Return to the Main Worksheet, turn screen updating back on and notify the user.
Sheets("Main").Activate
Range("A1").Select
Application.ScreenUpdating = True
' *******************************************************************************
End Sub