I am trying to get an ACCPAC macro to work and it keeps giving me an error stating that there are no records. I have taken the CASHFLOW.avb macro and modified it so that I can only access the AR database. I would appreciate it someone could look at and see what the problem is with the macro. I have added two textboxes and a combobox to the form to allow the user to print certain documents based on the short name field. The macro works fine if I enter the short name directly into the macro as soon as I try to get the value from the textbox or combobox it won't find any records. I believe it is the way I am calling the field and would like help to see where my problem is. I will place the code here for the experts to look at and comment on. Thank you.
Code is as follows
Private Sub AgingPeriodsFrame_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'make sure that all the edit boxes get the exit message
CurrentPeriod_Exit Cancel
Period1_Exit Cancel
Period2_Exit Cancel
Period3_Exit Cancel
End Sub
Private Sub CloseButton_Click()
RunDateBox.value = ""
Hide
End Sub
Private Sub CurrentPeriod_Change()
Static goodCurrentPeriod
If CurrentPeriod.value = "" Then Exit Sub
'Perform range checking on the value, restoring the old value if out of range
If isInteger(CurrentPeriod.value) Then
If CurrentPeriod.value >= 0 Then
'Strip any leading zeros
CurrentPeriod.value = CInt(CurrentPeriod.value)
goodCurrentPeriod = CurrentPeriod.value
Else
CurrentPeriod.value = goodCurrentPeriod
End If
Else
CurrentPeriod.value = goodCurrentPeriod
End If
End Sub
Private Sub CurrentPeriod_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If the box is empty then default to a value of 0
If CurrentPeriod.value = "" Then CurrentPeriod.value = 0
End Sub
Private Sub CutoffPeriod_Change()
EndDate.Text = fiscalInfo(CutoffYear.ListIndex + 1)(CutoffPeriod.ListIndex + 1)
End Sub
Private Sub CutoffYear_Change()
EndDate.Text = fiscalInfo(CutoffYear.ListIndex + 1)(CutoffPeriod.ListIndex + 1)
End Sub
Private Sub OKButton_Click()
Dim cp As Integer, p1 As Integer, p2 As Integer, p3 As Integer
Dim rd As Date, ed As Date
If CurrentPeriod.value = "" Or Period1.value = "" Or Period2.value = "" Or _
Period3.value = "" Then
MsgBox "Aging Periods must be filled in.", vbOKOnly + vbExclamation, _
"CashFlow Macro: Error"
Exit Sub
End If
'Cast the values into integers for comparison
cp = CurrentPeriod.value
p1 = Period1.value
p2 = Period2.value
p3 = Period3.value
EndDate.Text = fiscalInfo(CutoffYear.ListIndex + 1)(CutoffPeriod.ListIndex + 1)
ed = EndDate.Text
If Not isDate(RunDateBox.value) Then
MsgBox "Invalid date entered.", vbOKOnly + vbExclamation, "Error"
Else
rd = RunDateBox.value
If rd > ed Then
MsgBox "The Age As Of date is later than the Cutoff date.", _
vbOKOnly + vbExclamation, "Error"
ElseIf Not (cp < p1 And p1 < p2 And p2 < p3) Then
MsgBox "Invalid input. Aging periods must be in ascending order.", _
vbOKOnly + vbExclamation, "CashFlow Macro: Error"
Else
Hide
End If
End If
End Sub
Private Sub Period1_Change()
Static goodPeriod1
If Period1.value = "" Then Exit Sub
'Perform range checking on the value, restoring the old value if out of range
If isInteger(Period1.value) Then
If Period1.value >= 0 Then
'Strip any leading zeros
Period1.value = CInt(Period1.value)
goodPeriod1 = Period1.value
Else
Period1.value = goodPeriod1
End If
Else
Period1.value = goodPeriod1
End If
End Sub
Private Sub Period1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If the box is empty then default to a value of 0
If Period1.value = "" Then Period1.value = 0
End Sub
Private Sub Period2_Change()
Static goodPeriod2
If Period2.value = "" Then Exit Sub
'Perform range checking on the value, restoring the old value if out of range
If isInteger(Period2.value) Then
If Period2.value >= 0 Then
'Strip any leading zeros
Period2.value = CInt(Period2.value)
goodPeriod2 = Period2.value
Else
Period2.value = goodPeriod2
End If
Else
Period2.value = goodPeriod2
End If
End Sub
Private Sub Period2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If the box is empty then default to a value of 0
If Period2.value = "" Then Period2.value = 0
End Sub
Private Sub Period3_Change()
Static goodPeriod3
If Period3.value = "" Then Exit Sub
'Perform range checking on the value, restoring the old value if out of range
If isInteger(Period3.value) Then
If Period3.value >= 0 Then
'Strip any leading zeros
Period3.value = CInt(Period3.value)
goodPeriod3 = Period3.value
Over.value = Period3.value 'update the value in the "over period 3" box
Else
Period3.value = goodPeriod3
End If
Else
Period3.value = goodPeriod3
End If
End Sub
Private Sub Period3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If the box is empty then default to a value of 0
If Period3.value = "" Then
Period3.value = 0
Over.value = 0 'update the value in the "over period 3" box
End If
End Sub
Private Sub txtIDFROM2_Change()
Dim IDFROM2 As String
IDFROM2 = txtIDFROM2.Text
End Sub
Private Sub txtIDTO2_Change()
Dim IDTO2 As String
IDTO2 = txtIDTO2.Text
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
RunDateBox.value = ""
End Sub
Main Module
'Open the view
Set viewARAGE = Session.OpenView("AR0055", "AR")
viewARAGE.Init
On Error GoTo ErrHandler
Success = True
'Setup the super view
With viewARAGE
.Fields("CMNDCODE").PutWithoutVerification 51
.Fields("RUNDATE").PutWithoutVerification runDate
.Fields("AGEINVDTSW").PutWithoutVerification 1 'Age by document date
.Fields("AGETCURSW").PutWithoutVerification 0 'Functional currency
.Fields("ZEROBALSW").PutWithoutVerification 0 'Exclude zero balances
.Fields("PASTDUESW").PutWithoutVerification 0 'Aged Cash Received
.Fields("AGEPERIOD1").PutWithoutVerification agePeriods(0)
.Fields("AGEPERIOD2").PutWithoutVerification agePeriods(1)
.Fields("AGEPERIOD3").PutWithoutVerification agePeriods(2)
.Fields("AGEPERIOD4").PutWithoutVerification agePeriods(3)
.Fields("DAYSOUTFRM").PutWithoutVerification 0
.Fields("DAYSOUTTO").PutWithoutVerification 9999
.Fields("SWDETAIL").PutWithoutVerification 0 'Summary
.Fields("SWOPTMETER").PutWithoutVerification 1 'Display meter
.Fields("SWMATCHING").PutWithoutVerification 0 'Do not include applied details
.Fields("SWACCTTYPE").PutWithoutVerification 0 'All customers
.Fields("SWCUTOFFBY").PutWithoutVerification 1 'Cutoff by Year/Period
.Fields("SWCUSTCRLM").PutWithoutVerification 0 ' =1 Customers over credit limit only =0 not over cr limit
.Fields("CUTOFFYEAR").PutWithoutVerification CutoffYear
.Fields("IDFROM2").PutWithoutVerification ("IDFROM2") ' Range 2 From for Customer Account Set
.Fields("IDTO2").PutWithoutVerification ("IDTO2") ' Range 2 To for Customer Account Set
.Fields("INDEX2").PutWithoutVerification ("27") ' Range 2 Type for Customer Account Set
Dim strTemp As String
strTemp = CStr(CutoffPeriod)
Select Case Len(strTemp)
Case 1
strTemp = "0" & strTemp
Case 0
strTemp = "01" 'This is just a precaution
End Select
.Fields("CUTOFFPERD").PutWithoutVerification strTemp
'Generate the CSV file
.Process
If Success Then
'Get the name of the CSV file
fname = Trim(.Fields("FILENAME"))
'Open the CSV file
ExcelApp.Workbooks.Open FileName:=fname, Format:=2
Set CSVBook = ExcelApp.ActiveWorkbook
'Copy the contents
agePeriodHeadings = Array("", "", "", "")
CopyReceivablesTable CSVBook.ActiveSheet, theBook.Worksheets(2), _
agePeriodHeadings
'Close the CSV file
CSVBook.Close False
'Delete the CSV file
.Fields("CMNDCODE").PutWithoutVerification 13 'Delete work file
.Process
'Clean up the view
Set viewARAGE = Nothing
'Create the pivot table
With theBook
CreateReceivablesPivotTable .Worksheets(2), .Worksheets(1), _
agePeriodHeadings, runDate, CutoffYear, CutoffPeriod, _
PrintTransIn
End With
Else
'Clean up the view
Set viewARAGE = Nothing
End If
End With
AgeReceivables = Success
Exit Function
'''''Error Handler
SysErrHandler:
InvalidInstallationMsg "AR"
ErrHandler: 'Display error messages
HandleError
Success = False
Resume Next
End Function
Private Sub CopyReceivablesTable(fromSheet, toSheet, agePeriodHeadings)
'Purpose: Copy the relevant columns from the temporary CSV file into a
' worksheet for creating the Aged Cash Received pivot table
'Outline: Loop through the defined columns, copying the data from the CSV file
' into a temporary worksheet, trimming the data and converting date values into
' native format. Any record that is not an invoice (record type 1) will have
' all its amounts zeroed. The data is then copied as values into the data sheet
' in preparation for creating the pivot table.
On Error GoTo ErrHandler
FocusExcel
'Create the temporary worksheet
Set tempBook = ExcelApp.Workbooks.Add
Set tempSheet = tempBook.ActiveSheet
With fromSheet.Rows(1)
numColumns = 23
ColumnArray = Array( _
Array("IDCUST", "Customer Number"), _
Array("NAMECUST", "Customer Name"), _
Array("IDNATACCT", "National Account"), _
Array("IDGRP", "Customer Group"), _
Array("IDINVC", "Invoice Number"), _
Array("NAMECTAC", "Contact Name"), _
Array("TEXTPHON1", "Phone Number"), _
Array("CODETERR", "Territory Code"), _
Array("IDACCTSET", "Account Set"), _
Array("IDBILLCYCL", "Billing Cycle"), _
Array("IDSVCCHRG", "Interest Profile"), _
Array("CODECURN", "Customer Currency"), _
Array("SWBALFWD", "Account Type"), _
Array("CODETERM", "Terms"), _
Array("AMTCRLIMT", "Credit Limit"), _
Array("DATEINVC", "Document Date"), _
Array("DATEDUE", "Due Date"), _
Array("AMTTOTLBKD", "Overdue "), _
Array("AMTDUEAGE1", "Current "), _
Array("AMTDUEAGE2", "Period 1 "), _
Array("AMTDUEAGE3", "Period 2 "), _
Array("AMTDUEAGE4", "Period 3 "), _
Array("AMTDUEAGE5", "Over Period 3 ")) 'note the trailing spaces
'Find where the RECTYPE column is located in the CSV file
rectypeFormula = "'" & CSVBook.Name & "'!" & _
fromSheet.cells(1, .Find("RECTYPE", , xlFormulas, xlWhole, _
xlByRows, xlNext, True).Column).Address(False, False)
'Get the extent of the CSV file
lastRow = fromSheet.cells(1, 1).End(xlDown).Row - 1 'Ignoring the EOF marker
'Loop through the defined columns
For col = 1 To numColumns
'Locate the column in the CSV file
theColumn = .Find(ColumnArray(col - 1)(0), , xlFormulas, xlWhole, _
xlByRows, xlNext, True).Column
With tempSheet
'Create a reference to the cell
tempFormula = "'" & CSVBook.Name & "'!" & _
fromSheet.cells(1, theColumn).Address(False, False)
Select Case ColumnArray(col - 1)(0)
Case "AMTCRLIMT", "TEXTPHON1" 'Number values
'Remain as is
.cells(1, col) = "=" & tempFormula
Case "DATEINVC", "DATEDUE" 'All date values
'Convert into native date format
.cells(1, col) = "=DATE(MID(" & tempFormula & ", 1, 4)," & _
"MID(" & tempFormula & ", 5, 2),MID(" & tempFormula & _
", 7, 2))"
Case "SWBALFWD" 'Account type
'Change 0's and 1's into a description
.cells(1, col) = _
"=IF(" & tempFormula & "=1,""Balance Forward"",""Open Item"")"
Case "AMTTOTLBKD", "AMTDUEAGE1", "AMTDUEAGE2", _
"AMTDUEAGE3", "AMTDUEAGE4", "AMTDUEAGE5"
'If the record is an invoice, keep the value;
'otherwise, zero it
.cells(1, col) = _
"=IF(" & rectypeFormula & "=1," & tempFormula & ",0)"
Case Else
'Trim string values
.cells(1, col) = "=TRIM(" & tempFormula & ")"
End Select
'Fill the column with the formula
Set tempRange = .Range(.cells(1, col), .cells(lastRow, col))
.cells(1, col).Copy tempRange
tempRange.Copy
End With
'Transfer the column as values into the data sheet
With toSheet.cells(1, col)
.PasteSpecial xlPasteValues, , True
.Formula = ColumnArray(col - 1)(1)
End With
Next col
'Clear end of file marker
toSheet.cells(lastRow + 1, 1).Clear
'Retrieve the names of the aging periods
For i = 6 To 8
fromDay = fromSheet.cells(2, .Find("FSTTITLE" & i, , xlFormulas, _
xlWhole, xlByRows, xlNext, True).Column)
toDay = fromSheet.cells(2, .Find("SNDTITLE" & i, , xlFormulas, _
xlWhole, xlByRows, xlNext, True).Column)
agePeriodHeadings(i - 6) = fromDay & " to " & toDay & " Days"
toSheet.cells(1, 14 + i) = agePeriodHeadings(i - 6) & " " 'note the trailing space
Next i
agePeriodHeadings(3) = "Over " & toDay & " Days"
toSheet.cells(1, 23) = agePeriodHeadings(3) & " " 'note the trailing space
End With
'Close the temporary worksheet
tempBook.Close SaveChanges:=False
Set tempSheet = Nothing
Set tempBook = Nothing
FocusExcel
'Format the data sheet
With toSheet
.Activate
With .Rows(1)
.Font.ColorIndex = 55
.Font.Bold = True
.Interior.ColorIndex = 36
End With
.Columns(7).NumberFormat = phoneFormat
.Columns("P:Q").NumberFormat = dateFormat
.Columns("R:W").NumberFormat = numFormat
.cells.EntireColumn.AutoFit
.cells(2, 1).Select
ExcelApp.ActiveWindow.FreezePanes = True
End With
Exit Sub
'''''Error Handler
ErrHandler: 'Display error messages
HandleError
Resume Next
End Sub
Private Sub CreateReceivablesPivotTable(dataSheet, rptSheet, agePeriodHeadings, _
runDate, CutoffYear, CutoffPeriod, PrintTransIn)
'Purpose: Create a pivot table from the Aged Cash Received Data.
On Error GoTo ErrHandler
With rptSheet
'Create the pivot table
dataSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
dataSheet.Range(dataSheet.cells(1, 1), _
dataSheet.cells(dataSheet.cells(1, 1).End(xlDown).Row, _
dataSheet.cells(1, 1).End(xlToRight).Column)), _
TableDestination:=.cells(18, 1), TableName:="ReceivablesPivotTable"
With .PivotTables("ReceivablesPivotTable")
With .PivotFields("Overdue ") 'note the trailing space
.Orientation = xlDataField
.Name = "Overdue"
.Function = xlSum
.NumberFormat = numFormat
.Position = 1
End With
With .PivotFields("Current ") 'note the trailing space
.Orientation = xlDataField
.Name = "Current"
.Function = xlSum
.NumberFormat = numFormat
.Position = 2
End With
For i = 0 To 3
With .PivotFields(agePeriodHeadings(i) & " ") 'note the trailing space
.Orientation = xlDataField
.Name = agePeriodHeadings(i)
.Function = xlSum
.NumberFormat = numFormat
.Position = i + 3
End With
Next i
With .PivotFields("Data")
.Name = "Aging"
.Orientation = xlColumnField
.Position = 1
End With
.AddFields PageFields:=Array("Account Type", "Billing Cycle", _
"Interest Profile", "Account Set", "Customer Group")
.PivotFields("Customer Name").Subtotals = NoSubtotalsArray
.PivotFields("Customer Number").Subtotals = NoSubtotalsArray
.PivotFields("Document Date").Subtotals = NoSubtotalsArray
.PivotFields("Due Date").Subtotals = NoSubtotalsArray
.PivotFields("Invoice Number").Subtotals = NoSubtotalsArray
On Error GoTo CErrHandler
Dim bTemp As Boolean
bTemp = False
.AddFields "Customer Number", , , True
.AddFields "Customer Name", , , True
If PrintTransIn <> 0 Then
.AddFields "Invoice Number", , , True
.AddFields "Document Date", , , True
.AddFields "Due Date", , , True
Else
.AddFields , , "Invoice Number", True
.PivotFields("Invoice Number").Position = 1
End If
'Insert the row total
.CalculatedFields.Add "ReceivablesAmt", _
"='Current '+'" & agePeriodHeadings(0) & " '+'" & _
agePeriodHeadings(1) & " '+'" & agePeriodHeadings(2) & " '+'" & _
agePeriodHeadings(3) & " '" 'note the trailing spaces
.PivotFields("ReceivablesAmt").Orientation = xlDataField
.PivotFields("Aging").PivotItems("Sum of ReceivablesAmt").Name = "Total Receivables"
'Insert the row total
.CalculatedFields.Add "InterestAmt", _
"=if('Overdue '*.152/365<1,0,'Overdue '*.152/365 " '" 'note the trailing spaces
.PivotFields("ReceivablesAmt").Orientation = xlDataField
.PivotFields("Aging").PivotItems("Som of InterestAmt").Name = "Interest Calculation"
If bTemp Then
OversizedDBMsg
bTemp = False
End If
'Format the pivot table
.PivotSelect "Aging[All]", xlLabelOnly
If bTemp Then
bTemp = False
Else
ExcelApp.Selection.Font.ColorIndex = 55
ExcelApp.Selection.Font.Bold = True
ExcelApp.Selection.Interior.ColorIndex = 36
ExcelApp.Selection.HorizontalAlignment = xlCenter
End If
.PivotSelect "'Customer Name'[All]", xlLabelOnly
If bTemp Then
bTemp = False
Else
ExcelApp.Selection.Interior.ColorIndex = 35
End If
.PivotSelect "'Customer Number'[All]", xlLabelOnly
If bTemp Then
bTemp = False
Else
ExcelApp.Selection.Interior.ColorIndex = 40
ExcelApp.Selection.HorizontalAlignment = xlRight
End If
.PivotSelect "'Column Grand Total'", xlDataAndLabel
If bTemp Then
bTemp = False
Else
ExcelApp.Selection.Interior.ColorIndex = 37
End If
.PivotSelect "'Customer Name'[All;Total]", xlDataAndLabel
If bTemp Then
bTemp = False
Else
ExcelApp.Selection.Interior.ColorIndex = 34
End If
On Error GoTo ErrHandler
'Format as either a Summary or a Detail view
Select Case PrintTransIn
' Case 0 'Summary
' .PivotFields("Document Date").Orientation = xlHidden
' .PivotFields("Due Date").Orientation = xlHidden
' With .PivotFields("Invoice Number")
' .Orientation = xlPageField
' .Position = 1
' End With
Case 1 'Detail by Document
rptSheet.PageSetup.PaperSize = xlPaperLegal
Case 2 'Detail by Document Date
.PivotFields("Document Date").Position = 3
rptSheet.PageSetup.PaperSize = xlPaperLegal
End Select
End With
'Insert sheet title and data information
.cells(1, 1).Select
.Range(.cells(1, 1), .cells(1, 1).SpecialCells(xlLastCell)).Columns.AutoFit
With .cells(1, 1)
.Formula = companyName
.Font.Bold = True
.Font.Name = "Tahoma"
.Font.Size = 14
End With
With .cells(2, 1)
.Formula = "Date: " & Format(Date, "Long Date") & " " & Time
.Characters(1, 5).Font.Bold = True
End With
With .cells(3, 1)
.Formula = "Aged Cash Received by Document Date"
.Font.Bold = True
End With
.cells(5, 1) = "Age Transaction As Of"
With .cells(5, 3)
.Formula = runDate
.HorizontalAlignment = xlLeft
End With
.cells(6, 1) = "Cutoff By Year/Period"
.cells(6, 3) = "Year " & CutoffYear & " Period " & CutoffPeriod
With .Range(.cells(5, 1), .cells(6, 2))
.Font.Bold = True
.Merge Across:=True
End With
.Range(.cells(5, 3), .cells(6, 5)).Merge Across:=True
End With
'Setup the page margins
SetupPage rptSheet
Exit Sub
'''''Error Handler
CErrHandler:
bTemp = True
Resume Next
ErrHandler: 'Display error messages
HandleError
Resume Next
End Sub
Private Sub FocusExcel()
'Purpose: Make Excel's window the foreground window
On Error Resume Next
AppActivate "Microsoft Excel"
End Sub
Private Sub LoadCompanyInfo()
'Purpose: Load the company informarion from the database.
'Get company name, or if none, use the COMPANYID
companyName = Trim(Company.Name)
If companyName = "" Then companyName = Trim(Company.OrgID)
numPeriods = Company.FiscalPeriods
funcCurrency = Trim(Company.HomeCurrency)
End Sub
Private Sub SetupPage(sheet)
'Purpose: Setup the page margins
With sheet.PageSetup
.LeftMargin = ExcelApp.InchesToPoints(0.6)
.RightMargin = ExcelApp.InchesToPoints(0.6)
.TopMargin = ExcelApp.InchesToPoints(0.6)
.BottomMargin = ExcelApp.InchesToPoints(0.6)
.HeaderMargin = ExcelApp.InchesToPoints(0.3)
.FooterMargin = ExcelApp.InchesToPoints(0.3)
.Orientation = xlLandscape
End With
End Sub
Private Sub DrawLine(theRange, theSide, thickness)
'Purpose: Draw a border of a specified thickness on the specified side of
' a particular range
With theRange.Borders(theSide)
.LineStyle = xlContinuous
.Weight = thickness
.ColorIndex = xlAutomatic
End With
End Sub
Private Sub ThinLine(theRange, theSide)
'Purpose: Draw a thin border on the specfied side of a particular range
DrawLine theRange, theSide, xlThin
End Sub
Private Sub ThinBox(theRange)
'Purpose: Draw a thin box around a particular range
ThinLine theRange, xlEdgeLeft
ThinLine theRange, xlEdgeRight
ThinLine theRange, xlEdgeTop
ThinLine theRange, xlEdgeBottom
End Sub
Public Function isInteger(value)
'Purpose: Check to see if value is a valid integer by attempting to cast
' it into a variable of type Integer
If value = "" Then
isInteger = True
Exit Function
End If
Dim i As Integer
On Error GoTo NotAnInteger
i = value
isInteger = True
Exit Function
NotAnInteger:
isInteger = False
End Function
Private Sub HandleError()
'Purpose: This is the default error handler
Dim Errors As xapiErrors
Dim Error As Variant
Set Errors = Session.Errors
If Errors.Count = 0 Then
MsgBox Err.Description
Else
For Each Error In Errors
MsgBox Error.Description
Next
Errors.Clear
End If
Set Errors = Nothing
End Sub
Private Sub InvalidInstallationMsg(ModuleID As String)
MsgBox "Improper installation detected!" & vbCrLf & _
"Please make sure " & ModuleID & _
" is properly installed and activated before running this macro again!" _
, vbCritical, "Accpac"
End
End Sub
Private Sub OversizedDBMsg()
Static b As Boolean
If Not b Then
MsgBox "The memory requirements for this database exceeds the memory limits on Excel PivotTables!" & _
vbCrLf & "Note that some details may not be shown!", vbExclamation, "Accpac"
b = True
End If
End Sub
Code is as follows
Private Sub AgingPeriodsFrame_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'make sure that all the edit boxes get the exit message
CurrentPeriod_Exit Cancel
Period1_Exit Cancel
Period2_Exit Cancel
Period3_Exit Cancel
End Sub
Private Sub CloseButton_Click()
RunDateBox.value = ""
Hide
End Sub
Private Sub CurrentPeriod_Change()
Static goodCurrentPeriod
If CurrentPeriod.value = "" Then Exit Sub
'Perform range checking on the value, restoring the old value if out of range
If isInteger(CurrentPeriod.value) Then
If CurrentPeriod.value >= 0 Then
'Strip any leading zeros
CurrentPeriod.value = CInt(CurrentPeriod.value)
goodCurrentPeriod = CurrentPeriod.value
Else
CurrentPeriod.value = goodCurrentPeriod
End If
Else
CurrentPeriod.value = goodCurrentPeriod
End If
End Sub
Private Sub CurrentPeriod_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If the box is empty then default to a value of 0
If CurrentPeriod.value = "" Then CurrentPeriod.value = 0
End Sub
Private Sub CutoffPeriod_Change()
EndDate.Text = fiscalInfo(CutoffYear.ListIndex + 1)(CutoffPeriod.ListIndex + 1)
End Sub
Private Sub CutoffYear_Change()
EndDate.Text = fiscalInfo(CutoffYear.ListIndex + 1)(CutoffPeriod.ListIndex + 1)
End Sub
Private Sub OKButton_Click()
Dim cp As Integer, p1 As Integer, p2 As Integer, p3 As Integer
Dim rd As Date, ed As Date
If CurrentPeriod.value = "" Or Period1.value = "" Or Period2.value = "" Or _
Period3.value = "" Then
MsgBox "Aging Periods must be filled in.", vbOKOnly + vbExclamation, _
"CashFlow Macro: Error"
Exit Sub
End If
'Cast the values into integers for comparison
cp = CurrentPeriod.value
p1 = Period1.value
p2 = Period2.value
p3 = Period3.value
EndDate.Text = fiscalInfo(CutoffYear.ListIndex + 1)(CutoffPeriod.ListIndex + 1)
ed = EndDate.Text
If Not isDate(RunDateBox.value) Then
MsgBox "Invalid date entered.", vbOKOnly + vbExclamation, "Error"
Else
rd = RunDateBox.value
If rd > ed Then
MsgBox "The Age As Of date is later than the Cutoff date.", _
vbOKOnly + vbExclamation, "Error"
ElseIf Not (cp < p1 And p1 < p2 And p2 < p3) Then
MsgBox "Invalid input. Aging periods must be in ascending order.", _
vbOKOnly + vbExclamation, "CashFlow Macro: Error"
Else
Hide
End If
End If
End Sub
Private Sub Period1_Change()
Static goodPeriod1
If Period1.value = "" Then Exit Sub
'Perform range checking on the value, restoring the old value if out of range
If isInteger(Period1.value) Then
If Period1.value >= 0 Then
'Strip any leading zeros
Period1.value = CInt(Period1.value)
goodPeriod1 = Period1.value
Else
Period1.value = goodPeriod1
End If
Else
Period1.value = goodPeriod1
End If
End Sub
Private Sub Period1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If the box is empty then default to a value of 0
If Period1.value = "" Then Period1.value = 0
End Sub
Private Sub Period2_Change()
Static goodPeriod2
If Period2.value = "" Then Exit Sub
'Perform range checking on the value, restoring the old value if out of range
If isInteger(Period2.value) Then
If Period2.value >= 0 Then
'Strip any leading zeros
Period2.value = CInt(Period2.value)
goodPeriod2 = Period2.value
Else
Period2.value = goodPeriod2
End If
Else
Period2.value = goodPeriod2
End If
End Sub
Private Sub Period2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If the box is empty then default to a value of 0
If Period2.value = "" Then Period2.value = 0
End Sub
Private Sub Period3_Change()
Static goodPeriod3
If Period3.value = "" Then Exit Sub
'Perform range checking on the value, restoring the old value if out of range
If isInteger(Period3.value) Then
If Period3.value >= 0 Then
'Strip any leading zeros
Period3.value = CInt(Period3.value)
goodPeriod3 = Period3.value
Over.value = Period3.value 'update the value in the "over period 3" box
Else
Period3.value = goodPeriod3
End If
Else
Period3.value = goodPeriod3
End If
End Sub
Private Sub Period3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If the box is empty then default to a value of 0
If Period3.value = "" Then
Period3.value = 0
Over.value = 0 'update the value in the "over period 3" box
End If
End Sub
Private Sub txtIDFROM2_Change()
Dim IDFROM2 As String
IDFROM2 = txtIDFROM2.Text
End Sub
Private Sub txtIDTO2_Change()
Dim IDTO2 As String
IDTO2 = txtIDTO2.Text
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
RunDateBox.value = ""
End Sub
Main Module
'Open the view
Set viewARAGE = Session.OpenView("AR0055", "AR")
viewARAGE.Init
On Error GoTo ErrHandler
Success = True
'Setup the super view
With viewARAGE
.Fields("CMNDCODE").PutWithoutVerification 51
.Fields("RUNDATE").PutWithoutVerification runDate
.Fields("AGEINVDTSW").PutWithoutVerification 1 'Age by document date
.Fields("AGETCURSW").PutWithoutVerification 0 'Functional currency
.Fields("ZEROBALSW").PutWithoutVerification 0 'Exclude zero balances
.Fields("PASTDUESW").PutWithoutVerification 0 'Aged Cash Received
.Fields("AGEPERIOD1").PutWithoutVerification agePeriods(0)
.Fields("AGEPERIOD2").PutWithoutVerification agePeriods(1)
.Fields("AGEPERIOD3").PutWithoutVerification agePeriods(2)
.Fields("AGEPERIOD4").PutWithoutVerification agePeriods(3)
.Fields("DAYSOUTFRM").PutWithoutVerification 0
.Fields("DAYSOUTTO").PutWithoutVerification 9999
.Fields("SWDETAIL").PutWithoutVerification 0 'Summary
.Fields("SWOPTMETER").PutWithoutVerification 1 'Display meter
.Fields("SWMATCHING").PutWithoutVerification 0 'Do not include applied details
.Fields("SWACCTTYPE").PutWithoutVerification 0 'All customers
.Fields("SWCUTOFFBY").PutWithoutVerification 1 'Cutoff by Year/Period
.Fields("SWCUSTCRLM").PutWithoutVerification 0 ' =1 Customers over credit limit only =0 not over cr limit
.Fields("CUTOFFYEAR").PutWithoutVerification CutoffYear
.Fields("IDFROM2").PutWithoutVerification ("IDFROM2") ' Range 2 From for Customer Account Set
.Fields("IDTO2").PutWithoutVerification ("IDTO2") ' Range 2 To for Customer Account Set
.Fields("INDEX2").PutWithoutVerification ("27") ' Range 2 Type for Customer Account Set
Dim strTemp As String
strTemp = CStr(CutoffPeriod)
Select Case Len(strTemp)
Case 1
strTemp = "0" & strTemp
Case 0
strTemp = "01" 'This is just a precaution
End Select
.Fields("CUTOFFPERD").PutWithoutVerification strTemp
'Generate the CSV file
.Process
If Success Then
'Get the name of the CSV file
fname = Trim(.Fields("FILENAME"))
'Open the CSV file
ExcelApp.Workbooks.Open FileName:=fname, Format:=2
Set CSVBook = ExcelApp.ActiveWorkbook
'Copy the contents
agePeriodHeadings = Array("", "", "", "")
CopyReceivablesTable CSVBook.ActiveSheet, theBook.Worksheets(2), _
agePeriodHeadings
'Close the CSV file
CSVBook.Close False
'Delete the CSV file
.Fields("CMNDCODE").PutWithoutVerification 13 'Delete work file
.Process
'Clean up the view
Set viewARAGE = Nothing
'Create the pivot table
With theBook
CreateReceivablesPivotTable .Worksheets(2), .Worksheets(1), _
agePeriodHeadings, runDate, CutoffYear, CutoffPeriod, _
PrintTransIn
End With
Else
'Clean up the view
Set viewARAGE = Nothing
End If
End With
AgeReceivables = Success
Exit Function
'''''Error Handler
SysErrHandler:
InvalidInstallationMsg "AR"
ErrHandler: 'Display error messages
HandleError
Success = False
Resume Next
End Function
Private Sub CopyReceivablesTable(fromSheet, toSheet, agePeriodHeadings)
'Purpose: Copy the relevant columns from the temporary CSV file into a
' worksheet for creating the Aged Cash Received pivot table
'Outline: Loop through the defined columns, copying the data from the CSV file
' into a temporary worksheet, trimming the data and converting date values into
' native format. Any record that is not an invoice (record type 1) will have
' all its amounts zeroed. The data is then copied as values into the data sheet
' in preparation for creating the pivot table.
On Error GoTo ErrHandler
FocusExcel
'Create the temporary worksheet
Set tempBook = ExcelApp.Workbooks.Add
Set tempSheet = tempBook.ActiveSheet
With fromSheet.Rows(1)
numColumns = 23
ColumnArray = Array( _
Array("IDCUST", "Customer Number"), _
Array("NAMECUST", "Customer Name"), _
Array("IDNATACCT", "National Account"), _
Array("IDGRP", "Customer Group"), _
Array("IDINVC", "Invoice Number"), _
Array("NAMECTAC", "Contact Name"), _
Array("TEXTPHON1", "Phone Number"), _
Array("CODETERR", "Territory Code"), _
Array("IDACCTSET", "Account Set"), _
Array("IDBILLCYCL", "Billing Cycle"), _
Array("IDSVCCHRG", "Interest Profile"), _
Array("CODECURN", "Customer Currency"), _
Array("SWBALFWD", "Account Type"), _
Array("CODETERM", "Terms"), _
Array("AMTCRLIMT", "Credit Limit"), _
Array("DATEINVC", "Document Date"), _
Array("DATEDUE", "Due Date"), _
Array("AMTTOTLBKD", "Overdue "), _
Array("AMTDUEAGE1", "Current "), _
Array("AMTDUEAGE2", "Period 1 "), _
Array("AMTDUEAGE3", "Period 2 "), _
Array("AMTDUEAGE4", "Period 3 "), _
Array("AMTDUEAGE5", "Over Period 3 ")) 'note the trailing spaces
'Find where the RECTYPE column is located in the CSV file
rectypeFormula = "'" & CSVBook.Name & "'!" & _
fromSheet.cells(1, .Find("RECTYPE", , xlFormulas, xlWhole, _
xlByRows, xlNext, True).Column).Address(False, False)
'Get the extent of the CSV file
lastRow = fromSheet.cells(1, 1).End(xlDown).Row - 1 'Ignoring the EOF marker
'Loop through the defined columns
For col = 1 To numColumns
'Locate the column in the CSV file
theColumn = .Find(ColumnArray(col - 1)(0), , xlFormulas, xlWhole, _
xlByRows, xlNext, True).Column
With tempSheet
'Create a reference to the cell
tempFormula = "'" & CSVBook.Name & "'!" & _
fromSheet.cells(1, theColumn).Address(False, False)
Select Case ColumnArray(col - 1)(0)
Case "AMTCRLIMT", "TEXTPHON1" 'Number values
'Remain as is
.cells(1, col) = "=" & tempFormula
Case "DATEINVC", "DATEDUE" 'All date values
'Convert into native date format
.cells(1, col) = "=DATE(MID(" & tempFormula & ", 1, 4)," & _
"MID(" & tempFormula & ", 5, 2),MID(" & tempFormula & _
", 7, 2))"
Case "SWBALFWD" 'Account type
'Change 0's and 1's into a description
.cells(1, col) = _
"=IF(" & tempFormula & "=1,""Balance Forward"",""Open Item"")"
Case "AMTTOTLBKD", "AMTDUEAGE1", "AMTDUEAGE2", _
"AMTDUEAGE3", "AMTDUEAGE4", "AMTDUEAGE5"
'If the record is an invoice, keep the value;
'otherwise, zero it
.cells(1, col) = _
"=IF(" & rectypeFormula & "=1," & tempFormula & ",0)"
Case Else
'Trim string values
.cells(1, col) = "=TRIM(" & tempFormula & ")"
End Select
'Fill the column with the formula
Set tempRange = .Range(.cells(1, col), .cells(lastRow, col))
.cells(1, col).Copy tempRange
tempRange.Copy
End With
'Transfer the column as values into the data sheet
With toSheet.cells(1, col)
.PasteSpecial xlPasteValues, , True
.Formula = ColumnArray(col - 1)(1)
End With
Next col
'Clear end of file marker
toSheet.cells(lastRow + 1, 1).Clear
'Retrieve the names of the aging periods
For i = 6 To 8
fromDay = fromSheet.cells(2, .Find("FSTTITLE" & i, , xlFormulas, _
xlWhole, xlByRows, xlNext, True).Column)
toDay = fromSheet.cells(2, .Find("SNDTITLE" & i, , xlFormulas, _
xlWhole, xlByRows, xlNext, True).Column)
agePeriodHeadings(i - 6) = fromDay & " to " & toDay & " Days"
toSheet.cells(1, 14 + i) = agePeriodHeadings(i - 6) & " " 'note the trailing space
Next i
agePeriodHeadings(3) = "Over " & toDay & " Days"
toSheet.cells(1, 23) = agePeriodHeadings(3) & " " 'note the trailing space
End With
'Close the temporary worksheet
tempBook.Close SaveChanges:=False
Set tempSheet = Nothing
Set tempBook = Nothing
FocusExcel
'Format the data sheet
With toSheet
.Activate
With .Rows(1)
.Font.ColorIndex = 55
.Font.Bold = True
.Interior.ColorIndex = 36
End With
.Columns(7).NumberFormat = phoneFormat
.Columns("P:Q").NumberFormat = dateFormat
.Columns("R:W").NumberFormat = numFormat
.cells.EntireColumn.AutoFit
.cells(2, 1).Select
ExcelApp.ActiveWindow.FreezePanes = True
End With
Exit Sub
'''''Error Handler
ErrHandler: 'Display error messages
HandleError
Resume Next
End Sub
Private Sub CreateReceivablesPivotTable(dataSheet, rptSheet, agePeriodHeadings, _
runDate, CutoffYear, CutoffPeriod, PrintTransIn)
'Purpose: Create a pivot table from the Aged Cash Received Data.
On Error GoTo ErrHandler
With rptSheet
'Create the pivot table
dataSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
dataSheet.Range(dataSheet.cells(1, 1), _
dataSheet.cells(dataSheet.cells(1, 1).End(xlDown).Row, _
dataSheet.cells(1, 1).End(xlToRight).Column)), _
TableDestination:=.cells(18, 1), TableName:="ReceivablesPivotTable"
With .PivotTables("ReceivablesPivotTable")
With .PivotFields("Overdue ") 'note the trailing space
.Orientation = xlDataField
.Name = "Overdue"
.Function = xlSum
.NumberFormat = numFormat
.Position = 1
End With
With .PivotFields("Current ") 'note the trailing space
.Orientation = xlDataField
.Name = "Current"
.Function = xlSum
.NumberFormat = numFormat
.Position = 2
End With
For i = 0 To 3
With .PivotFields(agePeriodHeadings(i) & " ") 'note the trailing space
.Orientation = xlDataField
.Name = agePeriodHeadings(i)
.Function = xlSum
.NumberFormat = numFormat
.Position = i + 3
End With
Next i
With .PivotFields("Data")
.Name = "Aging"
.Orientation = xlColumnField
.Position = 1
End With
.AddFields PageFields:=Array("Account Type", "Billing Cycle", _
"Interest Profile", "Account Set", "Customer Group")
.PivotFields("Customer Name").Subtotals = NoSubtotalsArray
.PivotFields("Customer Number").Subtotals = NoSubtotalsArray
.PivotFields("Document Date").Subtotals = NoSubtotalsArray
.PivotFields("Due Date").Subtotals = NoSubtotalsArray
.PivotFields("Invoice Number").Subtotals = NoSubtotalsArray
On Error GoTo CErrHandler
Dim bTemp As Boolean
bTemp = False
.AddFields "Customer Number", , , True
.AddFields "Customer Name", , , True
If PrintTransIn <> 0 Then
.AddFields "Invoice Number", , , True
.AddFields "Document Date", , , True
.AddFields "Due Date", , , True
Else
.AddFields , , "Invoice Number", True
.PivotFields("Invoice Number").Position = 1
End If
'Insert the row total
.CalculatedFields.Add "ReceivablesAmt", _
"='Current '+'" & agePeriodHeadings(0) & " '+'" & _
agePeriodHeadings(1) & " '+'" & agePeriodHeadings(2) & " '+'" & _
agePeriodHeadings(3) & " '" 'note the trailing spaces
.PivotFields("ReceivablesAmt").Orientation = xlDataField
.PivotFields("Aging").PivotItems("Sum of ReceivablesAmt").Name = "Total Receivables"
'Insert the row total
.CalculatedFields.Add "InterestAmt", _
"=if('Overdue '*.152/365<1,0,'Overdue '*.152/365 " '" 'note the trailing spaces
.PivotFields("ReceivablesAmt").Orientation = xlDataField
.PivotFields("Aging").PivotItems("Som of InterestAmt").Name = "Interest Calculation"
If bTemp Then
OversizedDBMsg
bTemp = False
End If
'Format the pivot table
.PivotSelect "Aging[All]", xlLabelOnly
If bTemp Then
bTemp = False
Else
ExcelApp.Selection.Font.ColorIndex = 55
ExcelApp.Selection.Font.Bold = True
ExcelApp.Selection.Interior.ColorIndex = 36
ExcelApp.Selection.HorizontalAlignment = xlCenter
End If
.PivotSelect "'Customer Name'[All]", xlLabelOnly
If bTemp Then
bTemp = False
Else
ExcelApp.Selection.Interior.ColorIndex = 35
End If
.PivotSelect "'Customer Number'[All]", xlLabelOnly
If bTemp Then
bTemp = False
Else
ExcelApp.Selection.Interior.ColorIndex = 40
ExcelApp.Selection.HorizontalAlignment = xlRight
End If
.PivotSelect "'Column Grand Total'", xlDataAndLabel
If bTemp Then
bTemp = False
Else
ExcelApp.Selection.Interior.ColorIndex = 37
End If
.PivotSelect "'Customer Name'[All;Total]", xlDataAndLabel
If bTemp Then
bTemp = False
Else
ExcelApp.Selection.Interior.ColorIndex = 34
End If
On Error GoTo ErrHandler
'Format as either a Summary or a Detail view
Select Case PrintTransIn
' Case 0 'Summary
' .PivotFields("Document Date").Orientation = xlHidden
' .PivotFields("Due Date").Orientation = xlHidden
' With .PivotFields("Invoice Number")
' .Orientation = xlPageField
' .Position = 1
' End With
Case 1 'Detail by Document
rptSheet.PageSetup.PaperSize = xlPaperLegal
Case 2 'Detail by Document Date
.PivotFields("Document Date").Position = 3
rptSheet.PageSetup.PaperSize = xlPaperLegal
End Select
End With
'Insert sheet title and data information
.cells(1, 1).Select
.Range(.cells(1, 1), .cells(1, 1).SpecialCells(xlLastCell)).Columns.AutoFit
With .cells(1, 1)
.Formula = companyName
.Font.Bold = True
.Font.Name = "Tahoma"
.Font.Size = 14
End With
With .cells(2, 1)
.Formula = "Date: " & Format(Date, "Long Date") & " " & Time
.Characters(1, 5).Font.Bold = True
End With
With .cells(3, 1)
.Formula = "Aged Cash Received by Document Date"
.Font.Bold = True
End With
.cells(5, 1) = "Age Transaction As Of"
With .cells(5, 3)
.Formula = runDate
.HorizontalAlignment = xlLeft
End With
.cells(6, 1) = "Cutoff By Year/Period"
.cells(6, 3) = "Year " & CutoffYear & " Period " & CutoffPeriod
With .Range(.cells(5, 1), .cells(6, 2))
.Font.Bold = True
.Merge Across:=True
End With
.Range(.cells(5, 3), .cells(6, 5)).Merge Across:=True
End With
'Setup the page margins
SetupPage rptSheet
Exit Sub
'''''Error Handler
CErrHandler:
bTemp = True
Resume Next
ErrHandler: 'Display error messages
HandleError
Resume Next
End Sub
Private Sub FocusExcel()
'Purpose: Make Excel's window the foreground window
On Error Resume Next
AppActivate "Microsoft Excel"
End Sub
Private Sub LoadCompanyInfo()
'Purpose: Load the company informarion from the database.
'Get company name, or if none, use the COMPANYID
companyName = Trim(Company.Name)
If companyName = "" Then companyName = Trim(Company.OrgID)
numPeriods = Company.FiscalPeriods
funcCurrency = Trim(Company.HomeCurrency)
End Sub
Private Sub SetupPage(sheet)
'Purpose: Setup the page margins
With sheet.PageSetup
.LeftMargin = ExcelApp.InchesToPoints(0.6)
.RightMargin = ExcelApp.InchesToPoints(0.6)
.TopMargin = ExcelApp.InchesToPoints(0.6)
.BottomMargin = ExcelApp.InchesToPoints(0.6)
.HeaderMargin = ExcelApp.InchesToPoints(0.3)
.FooterMargin = ExcelApp.InchesToPoints(0.3)
.Orientation = xlLandscape
End With
End Sub
Private Sub DrawLine(theRange, theSide, thickness)
'Purpose: Draw a border of a specified thickness on the specified side of
' a particular range
With theRange.Borders(theSide)
.LineStyle = xlContinuous
.Weight = thickness
.ColorIndex = xlAutomatic
End With
End Sub
Private Sub ThinLine(theRange, theSide)
'Purpose: Draw a thin border on the specfied side of a particular range
DrawLine theRange, theSide, xlThin
End Sub
Private Sub ThinBox(theRange)
'Purpose: Draw a thin box around a particular range
ThinLine theRange, xlEdgeLeft
ThinLine theRange, xlEdgeRight
ThinLine theRange, xlEdgeTop
ThinLine theRange, xlEdgeBottom
End Sub
Public Function isInteger(value)
'Purpose: Check to see if value is a valid integer by attempting to cast
' it into a variable of type Integer
If value = "" Then
isInteger = True
Exit Function
End If
Dim i As Integer
On Error GoTo NotAnInteger
i = value
isInteger = True
Exit Function
NotAnInteger:
isInteger = False
End Function
Private Sub HandleError()
'Purpose: This is the default error handler
Dim Errors As xapiErrors
Dim Error As Variant
Set Errors = Session.Errors
If Errors.Count = 0 Then
MsgBox Err.Description
Else
For Each Error In Errors
MsgBox Error.Description
Next
Errors.Clear
End If
Set Errors = Nothing
End Sub
Private Sub InvalidInstallationMsg(ModuleID As String)
MsgBox "Improper installation detected!" & vbCrLf & _
"Please make sure " & ModuleID & _
" is properly installed and activated before running this macro again!" _
, vbCritical, "Accpac"
End
End Sub
Private Sub OversizedDBMsg()
Static b As Boolean
If Not b Then
MsgBox "The memory requirements for this database exceeds the memory limits on Excel PivotTables!" & _
vbCrLf & "Note that some details may not be shown!", vbExclamation, "Accpac"
b = True
End If
End Sub