writeprivate
New member
- Local time
- Today, 02:01
- Joined
- Jun 20, 2023
- Messages
- 5
Hi - I am new here. I am a reasonably experienced Access developer and am close to the MVP stage of developing an investment tracking application with double-entry bookkeeping. The last module/functionality for this version will be handling bank statements. I have not worked with Excel to Access to Excel before so would really appreciate some guidance.
The desired workflow is as follows:
1. The user has multiple bank accounts with different banks - each having a different bank statement layout
2. The user will create an Excel spreadsheet for each bank type. Then periodically, the user will download the CSV/XLSX from the bank and either OPTION A) manually append records to the single spreadsheet or OPTION B) simply save the new spreadsheet with latest records using a standard file naming format and within a standard folder. ** I am working with the OPTION A at the moment - see why below **
3. Using a button to run code (having selected bank type, folder location etc.), the desired VBA code will append all Excel records ("imported" field marked as false) - to the MS Access table "tblBankStatements". During import, the MS Access table boolean field "Imported" will be set to TRUE. At the same time, the Excel Imported field will also be set to true (or -1) for those corresponding records.
I have tried working with the in-built functions of Access for importting, appending, linking to Excel but frankly these approaches are not robust and reproducable and i think that code is the better way to go so that i can keep things neater and scalable.
ChatGPT has given me this skeleton of code. Your thoughts on this would be appreciated - is this the right road?
Option Compare Database
Option Explicit
Sub ImportExcelFile()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim xlApp As Object
Dim xlWorkbook As Object
Dim xlWorksheet As Object
Dim strFile As String
Dim strFolder As String
Dim strSQL As String
' Set the folder path where your Excel files are located
strFolder = "C:\YourFolderPath"
' Initialize Excel objects
Set xlApp = CreateObject("Excel.Application")
' Disable Excel alerts and visibility for faster processing
xlApp.DisplayAlerts = False
xlApp.Visible = False
' Set the database and recordset objects
Set db = CurrentDb()
Set rst = db.OpenRecordset("YourTableName")
' Loop through each file in the folder
strFile = Dir(strFolder & "\*.xlsx") ' Change the file extension if necessary
Do While strFile <> ""
' Check if the file has already been imported
strSQL = "SELECT Count(*) FROM YourTableName WHERE ImportedFlag=True AND FileName='" & strFile & "'"
If DCount("*", "YourTableName", "ImportedFlag=True AND FileName='" & strFile & "'") = 0 Then
' Open the Excel file
Set xlWorkbook = xlApp.Workbooks.Open(strFolder & "\" & strFile)
' Specify the relevant worksheet in the Excel file
Set xlWorksheet = xlWorkbook.Sheets("YourWorksheetName")
' Loop through the rows in the worksheet
Dim i As Integer
For i = 2 To xlWorksheet.UsedRange.Rows.Count ' Assuming the data starts from row 2
' Extract the relevant information from the Excel file
Dim transactionDate As Date
Dim transactionAmount As Double
' ... extract other fields as necessary
' Insert the data into the Access table
rst.AddNew
rst!TransactionDate = transactionDate
rst!TransactionAmount = transactionAmount
' ... set other fields as necessary
rst!FileName = strFile ' Store the file name for reference
rst!ImportedFlag = True ' Mark the record as imported
rst.Update
' Mark the corresponding "Imported" field as True in the Excel sheet
xlWorksheet.Cells(i, "D").Value = True ' Assuming the "Imported" field is in column D
Next i
' Close the Excel file without saving changes
xlWorkbook.Close False
End If
' Move to the next file in the folder
strFile = Dir
Loop
' Clean up objects
rst.Close
Set rst = Nothing
Set db = Nothing
' Quit Excel and release the objects
xlApp.Quit
Set xlWorksheet = Nothing
Set xlWorkbook = Nothing
Set xlApp = Nothing
MsgBox "Import completed successfully."
End Sub
NOTE: Why using OPTION A. The source downloaded bank statements need manual data manipulation anyway because the download has marketing text and other information in header and footer rows so not a simple sheet with first row as column headings. In the future when i figure out Excel code to remove and reformat the source data, I may move to OPTION B. Right now I want to understand the broader picture of MS Access VBA talking to Excel and then I will have a skeleton to learn from.
The desired workflow is as follows:
1. The user has multiple bank accounts with different banks - each having a different bank statement layout
2. The user will create an Excel spreadsheet for each bank type. Then periodically, the user will download the CSV/XLSX from the bank and either OPTION A) manually append records to the single spreadsheet or OPTION B) simply save the new spreadsheet with latest records using a standard file naming format and within a standard folder. ** I am working with the OPTION A at the moment - see why below **
3. Using a button to run code (having selected bank type, folder location etc.), the desired VBA code will append all Excel records ("imported" field marked as false) - to the MS Access table "tblBankStatements". During import, the MS Access table boolean field "Imported" will be set to TRUE. At the same time, the Excel Imported field will also be set to true (or -1) for those corresponding records.
I have tried working with the in-built functions of Access for importting, appending, linking to Excel but frankly these approaches are not robust and reproducable and i think that code is the better way to go so that i can keep things neater and scalable.
ChatGPT has given me this skeleton of code. Your thoughts on this would be appreciated - is this the right road?
Option Compare Database
Option Explicit
Sub ImportExcelFile()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim xlApp As Object
Dim xlWorkbook As Object
Dim xlWorksheet As Object
Dim strFile As String
Dim strFolder As String
Dim strSQL As String
' Set the folder path where your Excel files are located
strFolder = "C:\YourFolderPath"
' Initialize Excel objects
Set xlApp = CreateObject("Excel.Application")
' Disable Excel alerts and visibility for faster processing
xlApp.DisplayAlerts = False
xlApp.Visible = False
' Set the database and recordset objects
Set db = CurrentDb()
Set rst = db.OpenRecordset("YourTableName")
' Loop through each file in the folder
strFile = Dir(strFolder & "\*.xlsx") ' Change the file extension if necessary
Do While strFile <> ""
' Check if the file has already been imported
strSQL = "SELECT Count(*) FROM YourTableName WHERE ImportedFlag=True AND FileName='" & strFile & "'"
If DCount("*", "YourTableName", "ImportedFlag=True AND FileName='" & strFile & "'") = 0 Then
' Open the Excel file
Set xlWorkbook = xlApp.Workbooks.Open(strFolder & "\" & strFile)
' Specify the relevant worksheet in the Excel file
Set xlWorksheet = xlWorkbook.Sheets("YourWorksheetName")
' Loop through the rows in the worksheet
Dim i As Integer
For i = 2 To xlWorksheet.UsedRange.Rows.Count ' Assuming the data starts from row 2
' Extract the relevant information from the Excel file
Dim transactionDate As Date
Dim transactionAmount As Double
' ... extract other fields as necessary
' Insert the data into the Access table
rst.AddNew
rst!TransactionDate = transactionDate
rst!TransactionAmount = transactionAmount
' ... set other fields as necessary
rst!FileName = strFile ' Store the file name for reference
rst!ImportedFlag = True ' Mark the record as imported
rst.Update
' Mark the corresponding "Imported" field as True in the Excel sheet
xlWorksheet.Cells(i, "D").Value = True ' Assuming the "Imported" field is in column D
Next i
' Close the Excel file without saving changes
xlWorkbook.Close False
End If
' Move to the next file in the folder
strFile = Dir
Loop
' Clean up objects
rst.Close
Set rst = Nothing
Set db = Nothing
' Quit Excel and release the objects
xlApp.Quit
Set xlWorksheet = Nothing
Set xlWorkbook = Nothing
Set xlApp = Nothing
MsgBox "Import completed successfully."
End Sub
NOTE: Why using OPTION A. The source downloaded bank statements need manual data manipulation anyway because the download has marketing text and other information in header and footer rows so not a simple sheet with first row as column headings. In the future when i figure out Excel code to remove and reformat the source data, I may move to OPTION B. Right now I want to understand the broader picture of MS Access VBA talking to Excel and then I will have a skeleton to learn from.