Hi there, I need to insert invoices from a staging table into ACCPAC but seem to be getting the error : "Error HRESULT E_FAIL has been returned from a call to a COM component" on the insert statement. I've coded my app similarly to way the the VBA macro was created. Please can someone help me and point out my error. Below is the code. Thanking you in advance...
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
'Create session to accpac...
session = New Session()
'Initialise session and login...
session.Init("", "XX", "XX0001", "55")
session.Open("USER1", "ACCPAC", "CLDDAT", System.DateTime.Now, 0)
InitializeDBLink()
OpenInvoice()
Dim temp As Boolean
temp = ARINVOICE1batch.Exists
ARINVOICE1batch.Init()
temp = ARINVOICE1header.Exists
ARINVOICE1header.Init()
temp = ARINVOICE1detail1.Exists
ARINVOICE1detail1.Init()
temp = ARINVOICE1detail2.Exists
ARINVOICE1detail2.Init()
temp = ARINVOICE1detail3.Exists
ARINVOICE1detail2.Init()
temp = ARINVOICE1detail4.Exists
ARINVOICE1detail2.Init()
Dim objconnection As New SqlConnection("server=mssql-fin.hosting.local;database=INT_TCC_INTEGRATION;user id=*****;password=****")
Dim SQL As String
SQL = "select * from AI_Invoices"
objconnection.Open()
Dim cmd As New SqlCommand(SQL, objconnection)
Dim ds As New DataSet
Dim SqlDataAdapter = New SqlDataAdapter(cmd).Fill(ds, "AI_Invoices")
For Each dr As DataRow In ds.Tables(0).Rows
ARINVOICE1batch.Browse("((BTCHSTTS = 1) OR (BTCHSTTS = 7))", 1)
temp = ARINVOICE1batch.Exists
ARINVOICE1batch.RecordCreate(1)
ARINVOICE1batchFields.FieldByName("PROCESSCMD").SetValue("1", False) ' Process Command
ARINVOICE1batch.Process()
ARINVOICE1batch.Read(0)
temp = ARINVOICE1header.Exists
ARINVOICE1header.RecordCreate(2)
ARINVOICE1detail1.Cancel()
ARINVOICE1batchFields.FieldByName("BTCHDESC").SetValue(dr("BatchDesc").ToString(), False) ' Description
ARINVOICE1batch.Update()
ARINVOICE1headerFields.FieldByName("INVCTYPE").SetValue(dr("HeaderInvType").ToString(), False) ' Invoice Type
ARINVOICE1headerFields.FieldByName("IDCUST").SetValue(dr("HeaderCustNumber").ToString(), False) ' Customer Number
ARINVOICE1headerFields.FieldByName("IDINVC").SetValue(dr("HeaderDocNumber").ToString(), False) ' Document Number
temp = ARINVOICE1detail1.Exists
ARINVOICE1detail1.RecordClear()
temp = ARINVOICE1detail1.Exists
ARINVOICE1detail1.RecordCreate(0)
ARINVOICE1detail1Fields.FieldByName("IDACCTREV").SetValue(dr("DetailRevAccount").ToString(), False) ' Revenue Account
'ARINVOICE1headerFields.FieldByName("AMTEXTN").SetValue(dr("DetailTaxStatus").ToString(), False) ' Extended Amount w/ TIP
ARINVOICE1detail1.Insert()
ARINVOICE1detail1Fields("CNTLINE").SetValue("-1", False) ' Line Number
ARINVOICE1detail1.Read(0)
temp = ARINVOICE1detail1.Exists
ARINVOICE1detail1.RecordCreate(0)
ARINVOICE1detail1Fields.FieldByName("IDACCTREV").SetValue(dr("DetailRevAccount").ToString(), False) ' Revenue Account
ARINVOICE1headerFields.FieldByName("AMTEXTN").SetValue(dr("DetailTaxStatus").ToString(), False) ' Extended Amount w/ TIP
temp = ARINVOICE1detail1.Exists
'ARINVOICE1detail1Fields("TAXSTTS1").SetValue = ("3", False) ' Tax Class 1
ARINVOICE1detail1.Insert()
ARINVOICE1detail1Fields("CNTLINE").SetValue("-2", False) ' Line Number
ARINVOICE1detail1.Read(0)
ARINVOICE1headerFields.FieldByName("SPECINST").SetValue(dr("HeaderInstructions").ToString(), False) ' Special Instructions
ARINVOICE1headerFields.FieldByName("ORDRNBR").SetValue(dr("HeaderOrderNumber").ToString(), False) ' Order Number
ARINVOICE1headerFields.FieldByName("CUSTPO").SetValue(dr("HeaderPO").ToString(), False) ' PO Number
'ARINVOICE1headerFields("INVCDESC").PutWithoutVerification("Summary Entry") ' Invoice Description
ARINVOICE1headerFields.FieldByName("IDSHIPNBR").SetValue(dr("HeaderShipNumber").ToString(), False) ' Shipment Number
ARINVOICE1header.Insert()
ARINVOICE1detail1.Read(0)
ARINVOICE1detail1.Read(0)
ARINVOICE1batch.Read(1)
temp = ARINVOICE1header.Exists
ARINVOICE1header.RecordCreate(2)
ARINVOICE1detail1.Cancel()
Next
Try
'Display a message that the record was added...
MessageBox.Show("Invoices Added", "Click OK to continue", MessageBoxButtons.OK, _
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Catch SqlExceptionErr As SqlException
MessageBox.Show("ACCPAR ERROR", "ACCPAC IE Customers", MessageBoxButtons.OK, _
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
End Try
DisposeConnection()
End Sub
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
'Create session to accpac...
session = New Session()
'Initialise session and login...
session.Init("", "XX", "XX0001", "55")
session.Open("USER1", "ACCPAC", "CLDDAT", System.DateTime.Now, 0)
InitializeDBLink()
OpenInvoice()
Dim temp As Boolean
temp = ARINVOICE1batch.Exists
ARINVOICE1batch.Init()
temp = ARINVOICE1header.Exists
ARINVOICE1header.Init()
temp = ARINVOICE1detail1.Exists
ARINVOICE1detail1.Init()
temp = ARINVOICE1detail2.Exists
ARINVOICE1detail2.Init()
temp = ARINVOICE1detail3.Exists
ARINVOICE1detail2.Init()
temp = ARINVOICE1detail4.Exists
ARINVOICE1detail2.Init()
Dim objconnection As New SqlConnection("server=mssql-fin.hosting.local;database=INT_TCC_INTEGRATION;user id=*****;password=****")
Dim SQL As String
SQL = "select * from AI_Invoices"
objconnection.Open()
Dim cmd As New SqlCommand(SQL, objconnection)
Dim ds As New DataSet
Dim SqlDataAdapter = New SqlDataAdapter(cmd).Fill(ds, "AI_Invoices")
For Each dr As DataRow In ds.Tables(0).Rows
ARINVOICE1batch.Browse("((BTCHSTTS = 1) OR (BTCHSTTS = 7))", 1)
temp = ARINVOICE1batch.Exists
ARINVOICE1batch.RecordCreate(1)
ARINVOICE1batchFields.FieldByName("PROCESSCMD").SetValue("1", False) ' Process Command
ARINVOICE1batch.Process()
ARINVOICE1batch.Read(0)
temp = ARINVOICE1header.Exists
ARINVOICE1header.RecordCreate(2)
ARINVOICE1detail1.Cancel()
ARINVOICE1batchFields.FieldByName("BTCHDESC").SetValue(dr("BatchDesc").ToString(), False) ' Description
ARINVOICE1batch.Update()
ARINVOICE1headerFields.FieldByName("INVCTYPE").SetValue(dr("HeaderInvType").ToString(), False) ' Invoice Type
ARINVOICE1headerFields.FieldByName("IDCUST").SetValue(dr("HeaderCustNumber").ToString(), False) ' Customer Number
ARINVOICE1headerFields.FieldByName("IDINVC").SetValue(dr("HeaderDocNumber").ToString(), False) ' Document Number
temp = ARINVOICE1detail1.Exists
ARINVOICE1detail1.RecordClear()
temp = ARINVOICE1detail1.Exists
ARINVOICE1detail1.RecordCreate(0)
ARINVOICE1detail1Fields.FieldByName("IDACCTREV").SetValue(dr("DetailRevAccount").ToString(), False) ' Revenue Account
'ARINVOICE1headerFields.FieldByName("AMTEXTN").SetValue(dr("DetailTaxStatus").ToString(), False) ' Extended Amount w/ TIP
ARINVOICE1detail1.Insert()
ARINVOICE1detail1Fields("CNTLINE").SetValue("-1", False) ' Line Number
ARINVOICE1detail1.Read(0)
temp = ARINVOICE1detail1.Exists
ARINVOICE1detail1.RecordCreate(0)
ARINVOICE1detail1Fields.FieldByName("IDACCTREV").SetValue(dr("DetailRevAccount").ToString(), False) ' Revenue Account
ARINVOICE1headerFields.FieldByName("AMTEXTN").SetValue(dr("DetailTaxStatus").ToString(), False) ' Extended Amount w/ TIP
temp = ARINVOICE1detail1.Exists
'ARINVOICE1detail1Fields("TAXSTTS1").SetValue = ("3", False) ' Tax Class 1
ARINVOICE1detail1.Insert()
ARINVOICE1detail1Fields("CNTLINE").SetValue("-2", False) ' Line Number
ARINVOICE1detail1.Read(0)
ARINVOICE1headerFields.FieldByName("SPECINST").SetValue(dr("HeaderInstructions").ToString(), False) ' Special Instructions
ARINVOICE1headerFields.FieldByName("ORDRNBR").SetValue(dr("HeaderOrderNumber").ToString(), False) ' Order Number
ARINVOICE1headerFields.FieldByName("CUSTPO").SetValue(dr("HeaderPO").ToString(), False) ' PO Number
'ARINVOICE1headerFields("INVCDESC").PutWithoutVerification("Summary Entry") ' Invoice Description
ARINVOICE1headerFields.FieldByName("IDSHIPNBR").SetValue(dr("HeaderShipNumber").ToString(), False) ' Shipment Number
ARINVOICE1header.Insert()
ARINVOICE1detail1.Read(0)
ARINVOICE1detail1.Read(0)
ARINVOICE1batch.Read(1)
temp = ARINVOICE1header.Exists
ARINVOICE1header.RecordCreate(2)
ARINVOICE1detail1.Cancel()
Next
Try
'Display a message that the record was added...
MessageBox.Show("Invoices Added", "Click OK to continue", MessageBoxButtons.OK, _
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Catch SqlExceptionErr As SqlException
MessageBox.Show("ACCPAR ERROR", "ACCPAC IE Customers", MessageBoxButtons.OK, _
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
End Try
DisposeConnection()
End Sub