Sage Accpac 500 ERP (Version 5.3B)
I am trying to programmatically create Invoices from multiple PO Receipts (i.e. "Invoice Entry" icon from the "P/O Transactions").
As usual, my code is derived from a macro I recorded. I am looping through invoices for a vendor. On the first iteration of the loop it works fine. On the second iteration I get the following errors:
I know I must not be reinitializing the header view properly to get it ready for the next record, I just don't know how. The error occurs on line 7080 on the code below.
I am trying to programmatically create Invoices from multiple PO Receipts (i.e. "Invoice Entry" icon from the "P/O Transactions").
As usual, my code is derived from a macro I recorded. I am looping through invoices for a vendor. On the first iteration of the loop it works fine. On the second iteration I get the following errors:
AccPac said:1. Invalid input. Header sequence must be positive.; 2. Invoice creation process failed. Please enter the invoice again.
I know I must not be reinitializing the header view properly to get it ready for the next record, I just don't know how. The error occurs on line 7080 on the code below.
Code:
Private Sub ImportInvoices(GoodInvoices() As String, BadInvoices() As String)
900 On Error GoTo ACCPACErrorHandler
1000 Dim lngInvoiceCount As Long
1010 Dim lngInvoiceIdx As Long
1020 Dim strInvoiceNo As String
1021 Dim objPOReceipt As clsPOReceipt
1022 Dim bolDetailsCreated As Boolean
1023 Dim lngLineNo As Long
1024 Dim strLineNo As String
'************** Macro Code ******************
1030 Dim mDBLinkCmpRW As AccpacCOMAPI.AccpacDBLink
1040 Set mDBLinkCmpRW = OpenDBLink(DBLINK_COMPANY, DBLINK_FLG_READWRITE)
1050 Dim mDBLinkSysRW As AccpacCOMAPI.AccpacDBLink
1060 Set mDBLinkSysRW = OpenDBLink(DBLINK_SYSTEM, DBLINK_FLG_READWRITE)
1070 Dim temp As Boolean
1080 Dim POINV1header As AccpacCOMAPI.AccpacView
1090 Dim POINV1headerFields As AccpacCOMAPI.AccpacViewFields
1100 mDBLinkCmpRW.OpenView "PO0420", POINV1header
1110 Set POINV1headerFields = POINV1header.Fields
1120 Dim POINV1detail1 As AccpacCOMAPI.AccpacView
1130 Dim POINV1detail1Fields As AccpacCOMAPI.AccpacViewFields
1140 mDBLinkCmpRW.OpenView "PO0430", POINV1detail1
1150 Set POINV1detail1Fields = POINV1detail1.Fields
1160 Dim POINV1detail2 As AccpacCOMAPI.AccpacView
1170 Dim POINV1detail2Fields As AccpacCOMAPI.AccpacViewFields
1180 mDBLinkCmpRW.OpenView "PO0416", POINV1detail2
1190 Set POINV1detail2Fields = POINV1detail2.Fields
1200 Dim POINV1detail3 As AccpacCOMAPI.AccpacView
1210 Dim POINV1detail3Fields As AccpacCOMAPI.AccpacViewFields
1220 mDBLinkCmpRW.OpenView "PO0440", POINV1detail3
1230 Set POINV1detail3Fields = POINV1detail3.Fields
1240 Dim POINV1detail4 As AccpacCOMAPI.AccpacView
1250 Dim POINV1detail4Fields As AccpacCOMAPI.AccpacViewFields
1260 mDBLinkCmpRW.OpenView "PO0436", POINV1detail4
1270 Set POINV1detail4Fields = POINV1detail4.Fields
1280 Dim POINV1detail5 As AccpacCOMAPI.AccpacView
1290 Dim POINV1detail5Fields As AccpacCOMAPI.AccpacViewFields
1300 mDBLinkCmpRW.OpenView "PO0419", POINV1detail5
1310 Set POINV1detail5Fields = POINV1detail5.Fields
1320 Dim POINV1detail6 As AccpacCOMAPI.AccpacView
1330 Dim POINV1detail6Fields As AccpacCOMAPI.AccpacViewFields
1340 mDBLinkCmpRW.OpenView "PO0438", POINV1detail6
1350 Set POINV1detail6Fields = POINV1detail6.Fields
1360 Dim POINV1detail7 As AccpacCOMAPI.AccpacView
1370 Dim POINV1detail7Fields As AccpacCOMAPI.AccpacViewFields
1380 mDBLinkCmpRW.OpenView "PO0444", POINV1detail7
1390 Set POINV1detail7Fields = POINV1detail7.Fields
1400 Dim POINV1detail8 As AccpacCOMAPI.AccpacView
1410 Dim POINV1detail8Fields As AccpacCOMAPI.AccpacViewFields
1420 mDBLinkCmpRW.OpenView "PO0423", POINV1detail8
1430 Set POINV1detail8Fields = POINV1detail8.Fields
1440 Dim POINV1detail9 As AccpacCOMAPI.AccpacView
1450 Dim POINV1detail9Fields As AccpacCOMAPI.AccpacViewFields
1460 mDBLinkCmpRW.OpenView "PO0433", POINV1detail9
1470 Set POINV1detail9Fields = POINV1detail9.Fields
1480 Dim POINV1detail10 As AccpacCOMAPI.AccpacView
1490 Dim POINV1detail10Fields As AccpacCOMAPI.AccpacViewFields
1500 mDBLinkCmpRW.OpenView "PO0443", POINV1detail10
1510 Set POINV1detail10Fields = POINV1detail10.Fields
1520 Dim POINV1detail11 As AccpacCOMAPI.AccpacView
1530 Dim POINV1detail11Fields As AccpacCOMAPI.AccpacViewFields
1540 mDBLinkCmpRW.OpenView "PO0417", POINV1detail11
1550 Set POINV1detail11Fields = POINV1detail11.Fields
1560 Dim POINV1detail12 As AccpacCOMAPI.AccpacView
1570 Dim POINV1detail12Fields As AccpacCOMAPI.AccpacViewFields
1580 mDBLinkCmpRW.OpenView "PO0415", POINV1detail12
1590 Set POINV1detail12Fields = POINV1detail12.Fields
1600 Dim POINV1detail13 As AccpacCOMAPI.AccpacView
1610 Dim POINV1detail13Fields As AccpacCOMAPI.AccpacViewFields
1620 mDBLinkCmpRW.OpenView "PO0424", POINV1detail13
1630 Set POINV1detail13Fields = POINV1detail13.Fields
1640 POINV1header.Compose Array(POINV1detail2, POINV1detail1, POINV1detail3, POINV1detail4, POINV1detail5, POINV1detail6, POINV1detail7, POINV1detail8, POINV1detail12)
1650 POINV1detail1.Compose Array(POINV1header, POINV1detail5, Nothing, Nothing, POINV1detail9)
1660 POINV1detail2.Compose Array(POINV1header, POINV1detail1)
1670 POINV1detail3.Compose Array(POINV1header, POINV1detail5, Nothing, Nothing, POINV1detail6, POINV1detail7, POINV1detail10, POINV1detail12)
1680 POINV1detail4.Compose Array(POINV1header, POINV1detail5)
1690 POINV1detail5.Compose Array(POINV1header, POINV1detail2, POINV1detail1, POINV1detail4, POINV1detail3, POINV1detail6, POINV1detail7, POINV1detail12)
1700 POINV1detail6.Compose Array(POINV1header, POINV1detail5)
1710 POINV1detail7.Compose Array(POINV1header, POINV1detail5, Nothing, Nothing, POINV1detail3, POINV1detail6, POINV1detail1)
1720 POINV1detail8.Compose Array(POINV1header)
1730 POINV1detail9.Compose Array(POINV1detail1)
1740 POINV1detail10.Compose Array(POINV1detail3)
1750 POINV1detail11.Compose Array(Nothing, POINV1detail12, POINV1detail3)
1760 POINV1detail12.Compose Array(POINV1detail3, POINV1header, POINV1detail5, POINV1detail11)
1770 POINV1detail13.Compose Array(POINV1detail3, POINV1detail12)
'************** Macro Code ******************
1800 lngInvoiceCount = UBound(GoodInvoices) 'Array is 0-based but we didn't put values in the
'first element
1810 m_lngInvoicesCount = m_lngInvoicesCount + lngInvoiceCount
1850 For lngInvoiceIdx = 1 To lngInvoiceCount
1900 strInvoiceNo = GoodInvoices(lngInvoiceIdx)
'If on of the PO Receipts that should be in this invoice was not imported, we should
'not try to import the invoice
1950 If IsInArray(BadInvoices, strInvoiceNo, "BadInvoices()", m_Vendor.VendNo) = True Then
2000 WriteToLog Now, "FAIL", "Import Invoice # " & strInvoiceNo, _
"Did not import invoice because one or more associated PO Receipts could not be imported.", _
m_Vendor.VendNo, ""
2050 GoTo NextInvoice
2060 End If
'1. Create header for invoice
'2. Loop through all PO Receipts, add any whose Invoice Number matches (keep a running
' sum of the total)
'3. Put the sum total in the invoice header (if necessary)
2080 WriteToLog Now, "ATTEMPT", "Import Invoices", "Begin importing Invoice # " & strInvoiceNo, _
m_Vendor.VendNo, ""
'Invoice Header fields
3000 POINV1headerFields("INVHSEQ").PutWithoutVerification ("0") ' Invoice Sequence Key
3020 temp = POINV1header.Exists
3030 POINV1header.Init
3040 POINV1header.Order = 1
3050 temp = POINV1detail4.Exists
3060 POINV1detail4.Init
3070 POINV1headerFields("INVNUMBER").Value = strInvoiceNo ' Invoice Number
3080 POINV1headerFields("VDCODE").Value = m_Vendor.VendNo ' Vendor
3090 POINV1detail4Fields("INVPREV").PutWithoutVerification ("-999999999999999999") ' Payment Number
4000 POINV1detail4.Browse "", 1
4010 POINV1detail4.Fetch
4020 POINV1headerFields("MULTIRCP").Value = "1" ' Multiple Receipts
'Initialization of details (as per macro code)
4030 temp = POINV1detail6.Exists
4031 POINV1detail6.RecordClear
4032 bolDetailsCreated = False
4033 lngLineNo = 0
'Loop through POReceipts, add any that belong to this invoice
4040 For Each objPOReceipt In m_Vendor.POReceipts
4060 If objPOReceipt.InvoiceNo = strInvoiceNo Then
4065 WriteToLog Now, "ATTEMPT", "Import Invoices", "Assign PO Receipt # " & objPOReceipt.ReceiptNo & _
" to Invoice # " & strInvoiceNo, m_Vendor.VendNo, objPOReceipt.ReceiptNo
4080 POINV1detail6.Read
5000 temp = POINV1detail6.Exists
5020 POINV1detail6.RecordGenerate False
5040 POINV1detail6Fields("RCPNUMBER").Value = objPOReceipt.ReceiptNo ' Receipt Number
5060 POINV1detail6.Insert
5065 lngLineNo = lngLineNo + 1
5066 strLineNo = "-" & lngLineNo
5080 POINV1detail6Fields("INVRREV").PutWithoutVerification (strLineNo) ' Line Number
5090 WriteToLog Now, "SUCCEED", "Import Invoices", "Assigned PO Receipt # " & objPOReceipt.ReceiptNo & _
" to Invoice # " & strInvoiceNo, m_Vendor.VendNo, objPOReceipt.ReceiptNo
6000 bolDetailsCreated = True
6020 End If
7040 Next 'Next PO Receipt
7050 If bolDetailsCreated Then
7060 POINV1detail6.Read
7070 POINV1detail5Fields("FUNCTION").PutWithoutVerification ("12") ' Function
7080 POINV1detail5.Process
7090 End If
'Finish creating the invoice
8000 POINV1header.Order = 0
8010 POINV1header.Order = 1
8020 POINV1detail4Fields("INVPREV").PutWithoutVerification ("-999999999999999999") ' Payment Number
8030 POINV1detail4.Browse "", 1
8040 POINV1detail4.Fetch
8050 POINV1header.Insert
8060 POINV1detail4Fields("INVPREV").PutWithoutVerification ("-999999999999999999") ' Payment Number
8070 POINV1detail4.Browse "", 1
8080 POINV1detail4.Fetch
8090 POINV1header.Order = 0
9000 POINV1headerFields("INVHSEQ").PutWithoutVerification ("0") ' Invoice Sequence Key
9010 temp = POINV1header.Exists
9020 POINV1header.Init
9030 POINV1header.Order = 1
9040 temp = POINV1detail4.Exists
9050 POINV1detail4.Init
'* 21-Dec-2009 - Trying to get rid of "Header Sequence" error during next iteration of a
'* different invoice
9052 POINV1header.Order = 0
9053 POINV1headerFields("INVHSEQ").PutWithoutVerification ("0")
9054 temp = POINV1header.Exists
9055 POINV1header.Init
9056 POINV1header.Order = 1
9057 temp = POINV1detail4.Exists
9058 POINV1detail4.Init
'*
9060 WriteToLog Now, "SUCCEED", "Import Invoices", "Imported Invoice # " & strInvoiceNo, _
m_Vendor.VendNo, ""
9061 m_lngInvoicesAdded = m_lngInvoicesAdded + 1
9065 NextInvoice:
9070 Next ' Next Invoice
Exit Sub
ACCPACErrorHandler:
Dim strError As String
Dim lCount As Long
Dim lIndex As Long
Dim strPOReceiptNo As String
Dim lErrNo As Long
'Preserve error information
lErrNo = Err.Number
strError = Err.Description
If Not (objPOReceipt Is Nothing) Then
strPOReceiptNo = objPOReceipt.ReceiptNo
End If
If Errors Is Nothing Then
strError = "Error # " & lErrNo & ": " & strError
If Erl <> 0 Then
strError = strError & " LINE " & Erl
End If
If Not (objPOReceipt Is Nothing) Then
strPOReceiptNo = objPOReceipt.ReceiptNo
End If
WriteToLog Now, "FAIL", "Import Invoices", strError, m_Vendor.VendNo, _
"Invoice # " & strInvoiceNo & ", PO Receipt # " & strPOReceiptNo
Else
lCount = Errors.Count
If lCount = 0 Then
strError = "Error # " & lErrNo & ": " & strError
If Erl <> 0 Then
strError = strError & " LINE " & Erl
End If
WriteToLog Now, "FAIL", "Import Invoices", strError, m_Vendor.VendNo, _
"Invoice # " & strInvoiceNo & ", PO Receipt # " & strPOReceiptNo
Else
strError = "AccPac errors:"
lErrNo = 0
For lIndex = 0 To lCount - 1
'Don't include error if it is a warning about inventory number
'going below zero
If InStr(1, Errors(lIndex), "result in a negative quantity") = 0 Then
lErrNo = lErrNo + 1
strError = strError & " " & lErrNo & ". " & Errors.Item(lIndex) & ";"
End If
Next
If Erl <> 0 Then
strError = strError & " LINE " & Erl
End If
WriteToLog Now, "FAIL", "Import Invoices", strError, m_Vendor.VendNo, _
"Invoice # " & strInvoiceNo & ", PO Receipt # " & strPOReceiptNo
Errors.Clear
End If
End If
'Move on to the next invoice (there may be valid records after the one that caused the error)
Resume NextInvoice
End Sub