Hi Mark,
Here is the code -
Sub BtnPrepareBankEntries()
Dim sEntryDate AS String
Dim sBkStat AS String
Dim sBkStatBak AS String
Dim aFields(150,7) AS String
Dim sKey AS String
Dim aWhoRef(150,7) AS String
Dim iCount AS Integer
Dim iFileNo AS Integer
Dim iRec AS Integer
Dim iFld AS Integer
Dim iPtr AS Integer
Dim iBSB_AcNo AS Integer
Dim iEntryDate AS Integer
Dim iTypeCode AS Integer
Dim iAmount AS Integer
Dim iEntryType AS Integer
Dim iWho AS Integer
Dim iWhoRef AS Integer
Dim iKey AS Integer
iBSB_AcNo = 1
iEntryDate = 2
iTypeCode = 3
iAmount = 4
iEntryType = 5
iWho = 6
iWhoRef = 7
iKey = 0
sBkStat = "D:\Downloads\BBL.csv"
sBkStatBak = "D:\Downloads\BBL.bak"
if not FileExists(sBkStat) then
msgBox("File " + sBkStat + " not found. Check that you have
downloaded the month's entries from the Bank", MB_OK, "PRIOR ACTION NEEDED"
)
else
iCount = 0
iRec = 0
iFileNo = FreeFile()
' Read records from "D:\Downloads\BBL.csv into the array -
(aFields(150,7))
Open sBkStat for Input as #iFileNo
Do while NOT EOF(iFileNo)
iRec = iRec + 1
For iFld = 1 to 7
Input #iFileNo, aFields(iRec,iFld)
Next iFld
Loop
REM1 - The following lines transform the data to suit the Accounting System.
This is where you could delete duplicates or make data
adjustments.
For iPtr = iRec To 1 Step -1
aFields(iPtr,iEntryType) =
fCamelCase(fRemoveNumbers(aFields(iPtr,iEntryType),0))
aFields(iPtr,iWho) =
fCamelCase(fRemoveNumbers(aFields(iPtr,iWho),0))
aWhoRef(iPtr,iWhoRef) =
fCamelCase(fRemoveNumbers(aFields(iPtr,iWhoRef),2))
Select case aFields(iPtr,iEntryType)
Case "Debit Card Fee" :
aFields(iPtr,iKey) = aFields(iPtr,iEntryType)
Case "Deposit - Cheque(s)" :
aFields(iPtr,iKey) = aFields(iPtr,iEntryType)
Case "Direct Credit" :
aFields(iPtr,iKey) = aFields(iPtr,iWho)
if aFields(iPtr,iWho) = "Challenger Life" then
aFields(iPtr,iKey) = aFields(iPtr,iWho) + ": " +
aWhoRef(iPtr,iWhoRef)
End if
if aFields(iPtr,iWho) = "Ing Direct" then
aFields(iPtr,iKey) = aFields(iPtr,iWho) + " Credit"
End if
Case "Direct Debit" :
if aFields(iPtr,iWho) = "Ing Direct" then
aFields(iPtr,iKey) = aFields(iPtr,iWho) + " Debit"
else
aFields(iPtr,iKey) = aFields(iPtr,iWho) + ": " +
aWhoRef(iPtr,iWhoRef)
End if
Case "Interest" :
aFields(iPtr,iKey) = aFields(iPtr,iEntryType)
case "Pay Anyone" :
if aFields(iPtr,iAmount) > 0 then
aFields(iPtr,iKey) = aFields(iPtr,iWho)
else
aFields(iPtr,iKey) =
fCamelCase(fRemoveNumbers(aWhoRef(iPtr,iWhoRef),0))' Remove 2 digits
remaining on left of string
End if
case "Pension" :
aFields(iPtr,iKey) = aFields(iPtr,iWho) + ": " +
aWhoRef(iPtr,iWhoRef)
case "Retail Purchase" :
aFields(iPtr,iKey) = aFields(iPtr,iWho)
Case "Transaction Fees Charged" :
aFields(iPtr,iKey) = aFields(iPtr,iEntryType)
Case "Withdrawal - Atm" :
aFields(iPtr,iKey) = Left("WDraw-ATM: " +
aFields(iPtr,iWho), 30)
Case "Withdrawal - Eftpos" :
aFields(iPtr,iKey) = Left("WDraw-EFTPOS: " +
aFields(iPtr,iWho), 30)
case Else :
aFields(iPtr,iKey) = aFields(iPtr,iWho)
end Select
Next iPtr
REM2 - End of transformation section
' Add records to the BankStatments table in date order
For iPtr = iRec To 1 Step -1
sEntryDate = fReverseDate(aFields(iPtr,iEntryDate))
gsSQL = "INSERT INTO BankStatements
(EntryDate,Who,Amount,EntryType,WhoRef,""BSB-A/cNo"",TypeCode,Key) " _
+ "VALUES ('" + sEntryDate + "','" +
fProcessSingleQuotes(aFields(iPtr,iWho)) + "','" + aFields(iPtr,iAmount) +
"','" _
+ aFields(iPtr,iEntryType) + "','" +
aFields(iPtr,iWhoRef) + "','" + aFields(iPtr,iBSB_AcNo) + "','" _
+ aFields(iPtr,iTypeCode) + "','" +
fProcessSingleQuotes(aFields(iPtr,iKey)) + "')"
if goStmt9.executeUpdate(gsSQL) = 0 then '= row NOT
inserted
MsgBox("INSERT failed. Command was - '" + gsSQL + ";", 0,
"BtnPrepareBankEntries()")
Exit Sub
End if
iCount = iCount + 1
Next iPtr
Close #iFileNo
' Delete backup file
If FileExists(sBkStatBak) then
Kill(sBkStatBak)
else
msgBox("File " + sBkStatBak + " not found.")
End if
' Rename BBL.csv to BBL.bak
if FileExists(sBkStat) then
Name sBkStat As sBkStatBak
else
MsgBox("There was no '" + sBkStat + "' to make a new '" +
sBkStatBak + "' from.")
End if
MsgBox("Download Completed - " + CStr(iCount) + " entries
processed")
End if
End Sub
Hope you can read this - on a wide screen, the formatting is much better!
Perhaps you could copy and paste the code into a Writer document set to
landscape, to make it more readable.
You can probably ignore all the code between REM1 and REM2. This is just
formatting to suit my requirements.
I use several functions, all starting with "f", e.g. -
fReverseDate - Puts a date into yyyy-mm-dd format.
fProcessSingleQuotes - Single quotes need to be doubled up to avoid
problems.
etc.
If you would like the code for any of these, let me know.
Hopefully this may give you ideas you can experiment with.
Noel