Hi all,
I've figured out the syntax for using the COMAPI with Visual Basic.Net. But, I am having trouble actually inserting data into the detail (AR0042). When I call ".Insert", the program gives an "Unspecified error" (COMException) and crashes. Here is the code I'm using (I'm getting all the information from a data grid):
'Using COMAPI
Dim Session As New AccpacSession
Session.Init("", "AS", "AS1000", "52A")
Session.Open("MYUserName", "MYPassword", "TEST DBase", System.DateTime.Today, 0, "")
Dim link As AccpacCOMAPI.AccpacDBLink
link = Session.OpenDBLink(tagDBLinkTypeEnum.DBLINK_COMPANY, tagDBLinkFlagsEnum.DBLINK_FLG_READWRITE)
Dim ARInvoiceBatch As AccpacCOMAPI.AccpacView
Dim ARInvoiceBatchFields As AccpacCOMAPI.AccpacViewFields
link.OpenView("AR0041", ARInvoiceBatch)
ARInvoiceBatchFields = ARInvoiceBatch.Fields
Dim ARReceiptAdjustment As AccpacCOMAPI.AccpacView
Dim ARReceiptAdjustmentFields As AccpacCOMAPI.AccpacViewFields
link.OpenView("AR0042", ARReceiptAdjustment)
ARReceiptAdjustmentFields = ARReceiptAdjustment.Fields
Dim ARMiscellaneous As AccpacCOMAPI.AccpacView
Dim ARMiscellaneousFields As AccpacCOMAPI.AccpacViewFields
link.OpenView("AR0043", ARMiscellaneous)
ARMiscellaneousFields = ARMiscellaneous.Fields
Dim ARAppliedReceiptAdjustment As AccpacCOMAPI.AccpacView
Dim ARAppliedReceiptAdjustmentFields As AccpacCOMAPI.AccpacViewFields
link.OpenView("AR0044", ARAppliedReceiptAdjustment)
ARAppliedReceiptAdjustmentFields = ARAppliedReceiptAdjustment.Fields
Dim ARAdjustmentGL As AccpacCOMAPI.AccpacView
Dim ARAdjustmentGLFields As AccpacCOMAPI.AccpacViewFields
link.OpenView("AR0045", ARAdjustmentGL)
ARAdjustmentGLFields = ARAdjustmentGL.Fields
Dim openDocument As AccpacCOMAPI.AccpacView
link.OpenView("AR0061", openDocument)
Dim headerArray(2) As AccpacCOMAPI.AccpacView
headerArray(0) = ARInvoiceBatch
headerArray(1) = ARMiscellaneous
headerArray(2) = ARAppliedReceiptAdjustment
Dim appliedReceiptAdjustmentArray(1) As AccpacCOMAPI.AccpacView
appliedReceiptAdjustmentArray(0) = ARReceiptAdjustment
appliedReceiptAdjustmentArray(1) = ARAdjustmentGL
Dim openDocumentArray(4) As AccpacCOMAPI.AccpacView
openDocumentArray(0) = ARInvoiceBatch
openDocumentArray(1) = ARReceiptAdjustment
openDocumentArray(2) = ARMiscellaneous
openDocumentArray(3) = ARAppliedReceiptAdjustment
openDocumentArray(4) = ARAdjustmentGL
ARInvoiceBatch.Compose(ARReceiptAdjustment)
ARReceiptAdjustment.Compose(headerArray)
ARMiscellaneous.Compose(ARReceiptAdjustment)
ARAppliedReceiptAdjustment.Compose(appliedReceiptAdjustmentArray)
ARAdjustmentGL.Compose(ARAppliedReceiptAdjustment)
openDocument.Compose(openDocumentArray)
Dim newBatchNum As Integer = 3850
Dim foundBatch As Boolean = False
While Not foundBatch
newBatchNum = newBatchNum + 1
ARInvoiceBatch.Browse("CNTBTCH =" & newBatchNum, True)
foundBatch = Not ARInvoiceBatch.Fetch()
End While
ARInvoiceBatch.Fields.FieldByName("BATCHDESC").Value = "Post Dated Cheques for: " & ChequesDataGrid(0, 5)
ARInvoiceBatch.Fields.FieldByName("DATEBTCH").Value = System.DateTime.Today
'Set Batch Type = Imported
ARInvoiceBatch.Fields.FieldByName("BATCHTYPE").Value = 2
'Set Source Application (PDCS)
ARInvoiceBatch.Fields.FieldByName("SRCEAPPL").Value = "PDCS"
Dim index As Integer = 0
ARReceiptAdjustment.Fields.FieldByName("CODEPYMTYP").Value = "CA"
ARReceiptAdjustment.Fields.FieldByName("CNTBTCH").Value = newBatchNum
ARReceiptAdjustment.Fields.FieldByName("CNTITEM").Value = display_set.Tables(0).Rows.Count
For index = 0 To display_set.Tables(0).Rows.Count - 1
ARReceiptAdjustment.Fields.FieldByName("CODEPYMTYP").Value = "CA"
ARReceiptAdjustment.Fields.FieldByName("CNTBTCH").Value = newBatchNum
ARReceiptAdjustment.Fields.FieldByName("CNTITEM").Value = index + 1
ARReceiptAdjustment.Fields.FieldByName("IDCUST").Value = ChequesDataGrid(index, 2)
ARReceiptAdjustment.Fields.FieldByName("DATERMIT").Value = ChequesDataGrid(index, 5)
'not needed, these values are filled in automatically
'ARReceiptAdjustment.Fields.FieldByName("AMTRMITTC").Value = ChequesDataGrid(index, 6)
'ARReceiptAdjustment.Fields.FieldByName("SWRATETC").Value = 0
ARReceiptAdjustment.Fields.FieldByName("CODEPAYM").Value = "CHEQUE"
ARReceiptAdjustment.Fields.FieldByName("CODECURN").Value = "CAD"
ARReceiptAdjustment.Fields.FieldByName("RMITTYPE").Value = 3
ARReceiptAdjustment.Fields.FieldByName("CNTPAYMETR").Value = 1
ARReceiptAdjustment.Fields.FieldByName("AMTRMIT").Value = ChequesDataGrid(index, 6)
ARReceiptAdjustment.Fields.FieldByName("AMTPAYMTC").Value = ChequesDataGrid(index, 6)
ARReceiptAdjustment.Fields.FieldByName("DOCTYPE").Value = 1
ARReceiptAdjustment.Fields.FieldByName("CNTLSTLINE").Value = 1
ARReceiptAdjustment.Fields.FieldByName("FISCYR").Value = Regex.Match(ChequesDataGrid(index, 5), "\d+/\d+/(\d+)").Groups(1).ToString
Dim month As String = Regex.Match(ChequesDataGrid(index, 5), "(\d+)/\d+/\d+").Groups(1).ToString
If Len(month) = 1 Then
month = "0" & month
End If
ARReceiptAdjustment.Fields.FieldByName("IDRMIT").Value = getUniqueID(ChequesDataGrid(index, 0).ToString, index + 1)
ARReceiptAdjustment.Fields.FieldByName("FISCPER").Value = month
ARReceiptAdjustment.Fields.FieldByName("AMTRMITHC").Value = ChequesDataGrid(index, 6)
ARAppliedReceiptAdjustment.Fields.FieldByName("CODEPAYM").Value = "CA"
ARAppliedReceiptAdjustment.Fields.FieldByName("CNTBTCH").Value = newBatchNum
ARAppliedReceiptAdjustment.Fields.FieldByName("CNTITEM").Value = index + 1
ARAppliedReceiptAdjustment.Fields.FieldByName("CNTLINE").Value = 20
ARAppliedReceiptAdjustment.Fields.FieldByName("IDCUST").Value = ChequesDataGrid(index, 2)
ARAppliedReceiptAdjustment.Fields.FieldByName("IDINVC").Value = getInvoiceID(ChequesDataGrid(index, 5).ToString, index + 1)
ARAppliedReceiptAdjustment.Fields.FieldByName("TRXTYPE").Value = 2
ARAppliedReceiptAdjustment.Fields.FieldByName("PYMTRESL").Value = "10"
ARAppliedReceiptAdjustment.Fields.FieldByName("AMTPAYM").Value = ChequesDataGrid(index, 6)
ARAppliedReceiptAdjustment.Fields.FieldByName("CNTLASTSEQ").Value = 1
ARAppliedReceiptAdjustment.Fields.FieldByName("IDPPD").Value = getInvoiceID(ChequesDataGrid(index, 5).ToString, index + 1)
ARAppliedReceiptAdjustment.Fields.FieldByName("CDAPPLYTO").Value = 1
'get trouble here
ARAppliedReceiptAdjustment.Insert()
ARReceiptAdjustment.Insert()
Next
display_message(New Message("Data imported"))
Session.Close()
I've figured out the syntax for using the COMAPI with Visual Basic.Net. But, I am having trouble actually inserting data into the detail (AR0042). When I call ".Insert", the program gives an "Unspecified error" (COMException) and crashes. Here is the code I'm using (I'm getting all the information from a data grid):
'Using COMAPI
Dim Session As New AccpacSession
Session.Init("", "AS", "AS1000", "52A")
Session.Open("MYUserName", "MYPassword", "TEST DBase", System.DateTime.Today, 0, "")
Dim link As AccpacCOMAPI.AccpacDBLink
link = Session.OpenDBLink(tagDBLinkTypeEnum.DBLINK_COMPANY, tagDBLinkFlagsEnum.DBLINK_FLG_READWRITE)
Dim ARInvoiceBatch As AccpacCOMAPI.AccpacView
Dim ARInvoiceBatchFields As AccpacCOMAPI.AccpacViewFields
link.OpenView("AR0041", ARInvoiceBatch)
ARInvoiceBatchFields = ARInvoiceBatch.Fields
Dim ARReceiptAdjustment As AccpacCOMAPI.AccpacView
Dim ARReceiptAdjustmentFields As AccpacCOMAPI.AccpacViewFields
link.OpenView("AR0042", ARReceiptAdjustment)
ARReceiptAdjustmentFields = ARReceiptAdjustment.Fields
Dim ARMiscellaneous As AccpacCOMAPI.AccpacView
Dim ARMiscellaneousFields As AccpacCOMAPI.AccpacViewFields
link.OpenView("AR0043", ARMiscellaneous)
ARMiscellaneousFields = ARMiscellaneous.Fields
Dim ARAppliedReceiptAdjustment As AccpacCOMAPI.AccpacView
Dim ARAppliedReceiptAdjustmentFields As AccpacCOMAPI.AccpacViewFields
link.OpenView("AR0044", ARAppliedReceiptAdjustment)
ARAppliedReceiptAdjustmentFields = ARAppliedReceiptAdjustment.Fields
Dim ARAdjustmentGL As AccpacCOMAPI.AccpacView
Dim ARAdjustmentGLFields As AccpacCOMAPI.AccpacViewFields
link.OpenView("AR0045", ARAdjustmentGL)
ARAdjustmentGLFields = ARAdjustmentGL.Fields
Dim openDocument As AccpacCOMAPI.AccpacView
link.OpenView("AR0061", openDocument)
Dim headerArray(2) As AccpacCOMAPI.AccpacView
headerArray(0) = ARInvoiceBatch
headerArray(1) = ARMiscellaneous
headerArray(2) = ARAppliedReceiptAdjustment
Dim appliedReceiptAdjustmentArray(1) As AccpacCOMAPI.AccpacView
appliedReceiptAdjustmentArray(0) = ARReceiptAdjustment
appliedReceiptAdjustmentArray(1) = ARAdjustmentGL
Dim openDocumentArray(4) As AccpacCOMAPI.AccpacView
openDocumentArray(0) = ARInvoiceBatch
openDocumentArray(1) = ARReceiptAdjustment
openDocumentArray(2) = ARMiscellaneous
openDocumentArray(3) = ARAppliedReceiptAdjustment
openDocumentArray(4) = ARAdjustmentGL
ARInvoiceBatch.Compose(ARReceiptAdjustment)
ARReceiptAdjustment.Compose(headerArray)
ARMiscellaneous.Compose(ARReceiptAdjustment)
ARAppliedReceiptAdjustment.Compose(appliedReceiptAdjustmentArray)
ARAdjustmentGL.Compose(ARAppliedReceiptAdjustment)
openDocument.Compose(openDocumentArray)
Dim newBatchNum As Integer = 3850
Dim foundBatch As Boolean = False
While Not foundBatch
newBatchNum = newBatchNum + 1
ARInvoiceBatch.Browse("CNTBTCH =" & newBatchNum, True)
foundBatch = Not ARInvoiceBatch.Fetch()
End While
ARInvoiceBatch.Fields.FieldByName("BATCHDESC").Value = "Post Dated Cheques for: " & ChequesDataGrid(0, 5)
ARInvoiceBatch.Fields.FieldByName("DATEBTCH").Value = System.DateTime.Today
'Set Batch Type = Imported
ARInvoiceBatch.Fields.FieldByName("BATCHTYPE").Value = 2
'Set Source Application (PDCS)
ARInvoiceBatch.Fields.FieldByName("SRCEAPPL").Value = "PDCS"
Dim index As Integer = 0
ARReceiptAdjustment.Fields.FieldByName("CODEPYMTYP").Value = "CA"
ARReceiptAdjustment.Fields.FieldByName("CNTBTCH").Value = newBatchNum
ARReceiptAdjustment.Fields.FieldByName("CNTITEM").Value = display_set.Tables(0).Rows.Count
For index = 0 To display_set.Tables(0).Rows.Count - 1
ARReceiptAdjustment.Fields.FieldByName("CODEPYMTYP").Value = "CA"
ARReceiptAdjustment.Fields.FieldByName("CNTBTCH").Value = newBatchNum
ARReceiptAdjustment.Fields.FieldByName("CNTITEM").Value = index + 1
ARReceiptAdjustment.Fields.FieldByName("IDCUST").Value = ChequesDataGrid(index, 2)
ARReceiptAdjustment.Fields.FieldByName("DATERMIT").Value = ChequesDataGrid(index, 5)
'not needed, these values are filled in automatically
'ARReceiptAdjustment.Fields.FieldByName("AMTRMITTC").Value = ChequesDataGrid(index, 6)
'ARReceiptAdjustment.Fields.FieldByName("SWRATETC").Value = 0
ARReceiptAdjustment.Fields.FieldByName("CODEPAYM").Value = "CHEQUE"
ARReceiptAdjustment.Fields.FieldByName("CODECURN").Value = "CAD"
ARReceiptAdjustment.Fields.FieldByName("RMITTYPE").Value = 3
ARReceiptAdjustment.Fields.FieldByName("CNTPAYMETR").Value = 1
ARReceiptAdjustment.Fields.FieldByName("AMTRMIT").Value = ChequesDataGrid(index, 6)
ARReceiptAdjustment.Fields.FieldByName("AMTPAYMTC").Value = ChequesDataGrid(index, 6)
ARReceiptAdjustment.Fields.FieldByName("DOCTYPE").Value = 1
ARReceiptAdjustment.Fields.FieldByName("CNTLSTLINE").Value = 1
ARReceiptAdjustment.Fields.FieldByName("FISCYR").Value = Regex.Match(ChequesDataGrid(index, 5), "\d+/\d+/(\d+)").Groups(1).ToString
Dim month As String = Regex.Match(ChequesDataGrid(index, 5), "(\d+)/\d+/\d+").Groups(1).ToString
If Len(month) = 1 Then
month = "0" & month
End If
ARReceiptAdjustment.Fields.FieldByName("IDRMIT").Value = getUniqueID(ChequesDataGrid(index, 0).ToString, index + 1)
ARReceiptAdjustment.Fields.FieldByName("FISCPER").Value = month
ARReceiptAdjustment.Fields.FieldByName("AMTRMITHC").Value = ChequesDataGrid(index, 6)
ARAppliedReceiptAdjustment.Fields.FieldByName("CODEPAYM").Value = "CA"
ARAppliedReceiptAdjustment.Fields.FieldByName("CNTBTCH").Value = newBatchNum
ARAppliedReceiptAdjustment.Fields.FieldByName("CNTITEM").Value = index + 1
ARAppliedReceiptAdjustment.Fields.FieldByName("CNTLINE").Value = 20
ARAppliedReceiptAdjustment.Fields.FieldByName("IDCUST").Value = ChequesDataGrid(index, 2)
ARAppliedReceiptAdjustment.Fields.FieldByName("IDINVC").Value = getInvoiceID(ChequesDataGrid(index, 5).ToString, index + 1)
ARAppliedReceiptAdjustment.Fields.FieldByName("TRXTYPE").Value = 2
ARAppliedReceiptAdjustment.Fields.FieldByName("PYMTRESL").Value = "10"
ARAppliedReceiptAdjustment.Fields.FieldByName("AMTPAYM").Value = ChequesDataGrid(index, 6)
ARAppliedReceiptAdjustment.Fields.FieldByName("CNTLASTSEQ").Value = 1
ARAppliedReceiptAdjustment.Fields.FieldByName("IDPPD").Value = getInvoiceID(ChequesDataGrid(index, 5).ToString, index + 1)
ARAppliedReceiptAdjustment.Fields.FieldByName("CDAPPLYTO").Value = 1
'get trouble here
ARAppliedReceiptAdjustment.Insert()
ARReceiptAdjustment.Insert()
Next
display_message(New Message("Data imported"))
Session.Close()