I have been tasked with converting a slew of VB6 apps to C#, and am having trouble creating an AP invoice batch. I get an "HRESULT E_FAIL" error when the program reaches "NewAPInvoicebatch.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_NOINSERT)" on line 52.
I copied the code from a recorded macro and trimmed some fat, but can't seem to find anything I left out. Any ideas? Thanks in advance.
using System;
using System.Windows.Forms;
using AccpacCOMAPI;
namespace AccpacWorkspace
{
public class CreateInvoice
{
public CreateInvoice()
{
try
{
string[] myArray = new string[10];
// Set Accpac session object
AccpacCOMAPI.AccpacSession AccpacSession = new AccpacCOMAPI.AccpacSession();
// Initialize and open Accpac session
AccpacSession.Init("", "CS", "CS0001", "56A");
AccpacSession.Open("ADMIN", "ADMIN", "SAMDAT", DateTime.Today, 0, "");
// Set Accpac database object
AccpacCOMAPI.AccpacDBLink AccpacCompanyDB =
AccpacSession.OpenDBLink(AccpacCOMAPI.tagDBLinkTypeEnum.DBLINK_COMPANY, AccpacCOMAPI.tagDBLinkFlagsEnum.DBLINK_FLG_READWRITE);
// Set Accpac views
AccpacCOMAPI.AccpacView NewAPInvoicebatch;
AccpacCOMAPI.AccpacView NewAPInvoiceheader;
AccpacCOMAPI.AccpacView NewAPInvoicedetail1;
AccpacCOMAPI.AccpacView NewAPInvoicedetail2;
AccpacCOMAPI.AccpacView NewAPInvoicedetail3;
AccpacCOMAPI.AccpacView NewAPInvoicedetail4;
// Open Accpac views
AccpacCompanyDB.OpenView("AP0020", out NewAPInvoicebatch);
AccpacCompanyDB.OpenView("AP0021", out NewAPInvoiceheader);
AccpacCompanyDB.OpenView("AP0022", out NewAPInvoicedetail1);
AccpacCompanyDB.OpenView("AP0023", out NewAPInvoicedetail2);
AccpacCompanyDB.OpenView("AP0402", out NewAPInvoicedetail3);
AccpacCompanyDB.OpenView("AP0401", out NewAPInvoicedetail4);
// Create the batch
NewAPInvoicebatch.Compose(new object[] { NewAPInvoiceheader });
// Create an invoice batch
NewAPInvoiceheader.Compose(new object[] { NewAPInvoicebatch, NewAPInvoicedetail1, NewAPInvoicedetail2, NewAPInvoicedetail3 });
NewAPInvoicedetail1.Compose(new object[] { NewAPInvoiceheader, NewAPInvoicebatch, NewAPInvoicedetail4 });
NewAPInvoicedetail2.Compose(new object[] { NewAPInvoiceheader });
NewAPInvoicedetail3.Compose(new object[] { NewAPInvoiceheader });
NewAPInvoicedetail4.Compose(new object[] { NewAPInvoicedetail1 });
NewAPInvoicebatch.Browse("((BTCHSTTS = 1) OR (BTCHSTTS = 7))", true);
NewAPInvoicebatch.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_NOINSERT);
NewAPInvoicebatch.Fields.get_FieldByName("PROCESSCMD").PutWithoutVerification("1"); // Process Command Code
NewAPInvoicebatch.Process();
NewAPInvoicebatch.Read();
NewAPInvoiceheader.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_INSERT);
NewAPInvoicedetail1.Cancel();
NewAPInvoicebatch.Fields.get_FieldByName("BTCHDESC").PutWithoutVerification("Test Batch 2"); // Description
NewAPInvoicebatch.Update();
NewAPInvoiceheader.Fields.get_FieldByName("IDVEND").set_Value("1450"); // Vendor Number
NewAPInvoiceheader.Fields.get_FieldByName("PROCESSCMD").PutWithoutVerification("7"); // Process Command Code
NewAPInvoiceheader.Fields.get_FieldByName("INVCDESC").PutWithoutVerification("Test Entry 2"); // Invoice Description
NewAPInvoiceheader.Process();
NewAPInvoiceheader.Fields.get_FieldByName("PROCESSCMD").PutWithoutVerification("4"); // Process Command Code
NewAPInvoiceheader.Process();
NewAPInvoicedetail1.RecordClear();
NewAPInvoicedetail1.RecordCreate(0);
NewAPInvoicedetail1.Fields.get_FieldByName("PROCESSCMD").PutWithoutVerification("0"); // Process Command Code
NewAPInvoicedetail1.Process();
NewAPInvoicedetail1.Fields.get_FieldByName("IDDIST").set_Value("INV"); // Distribution Code
NewAPInvoicedetail1.Fields.get_FieldByName("AMTDIST").set_Value("100.000"); // Distributed Amount
NewAPInvoicedetail1.Insert();
NewAPInvoicedetail1.Fields.get_FieldByName("CNTLINE").PutWithoutVerification("-1"); // Line Number
NewAPInvoicedetail1.Read();
NewAPInvoiceheader.Insert();
NewAPInvoiceheader.Fields.get_FieldByName("IDINVC").set_Value("5435"); // Document Number
NewAPInvoiceheader.Insert();
NewAPInvoicebatch.Read();
NewAPInvoiceheader.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_INSERT);
NewAPInvoicedetail1.Cancel();
NewAPInvoiceheader.Fields.get_FieldByName("CNTITEM").set_Value("2"); // Entry Number
NewAPInvoiceheader.Browse("", false);
NewAPInvoiceheader.Fetch();
NewAPInvoicedetail1.Fields.get_FieldByName("CNTLINE").PutWithoutVerification("20"); // Line Number
NewAPInvoicedetail1.Read();
NewAPInvoiceheader.Fields.get_FieldByName("AMTGROSTOT").set_Value("108.250"); // Document Total Including Tax
NewAPInvoiceheader.Update();
// Close and destroy the Accpac session
AccpacSession.Close();
AccpacSession = null;
Application.Run(new Form1());
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}
I copied the code from a recorded macro and trimmed some fat, but can't seem to find anything I left out. Any ideas? Thanks in advance.
using System;
using System.Windows.Forms;
using AccpacCOMAPI;
namespace AccpacWorkspace
{
public class CreateInvoice
{
public CreateInvoice()
{
try
{
string[] myArray = new string[10];
// Set Accpac session object
AccpacCOMAPI.AccpacSession AccpacSession = new AccpacCOMAPI.AccpacSession();
// Initialize and open Accpac session
AccpacSession.Init("", "CS", "CS0001", "56A");
AccpacSession.Open("ADMIN", "ADMIN", "SAMDAT", DateTime.Today, 0, "");
// Set Accpac database object
AccpacCOMAPI.AccpacDBLink AccpacCompanyDB =
AccpacSession.OpenDBLink(AccpacCOMAPI.tagDBLinkTypeEnum.DBLINK_COMPANY, AccpacCOMAPI.tagDBLinkFlagsEnum.DBLINK_FLG_READWRITE);
// Set Accpac views
AccpacCOMAPI.AccpacView NewAPInvoicebatch;
AccpacCOMAPI.AccpacView NewAPInvoiceheader;
AccpacCOMAPI.AccpacView NewAPInvoicedetail1;
AccpacCOMAPI.AccpacView NewAPInvoicedetail2;
AccpacCOMAPI.AccpacView NewAPInvoicedetail3;
AccpacCOMAPI.AccpacView NewAPInvoicedetail4;
// Open Accpac views
AccpacCompanyDB.OpenView("AP0020", out NewAPInvoicebatch);
AccpacCompanyDB.OpenView("AP0021", out NewAPInvoiceheader);
AccpacCompanyDB.OpenView("AP0022", out NewAPInvoicedetail1);
AccpacCompanyDB.OpenView("AP0023", out NewAPInvoicedetail2);
AccpacCompanyDB.OpenView("AP0402", out NewAPInvoicedetail3);
AccpacCompanyDB.OpenView("AP0401", out NewAPInvoicedetail4);
// Create the batch
NewAPInvoicebatch.Compose(new object[] { NewAPInvoiceheader });
// Create an invoice batch
NewAPInvoiceheader.Compose(new object[] { NewAPInvoicebatch, NewAPInvoicedetail1, NewAPInvoicedetail2, NewAPInvoicedetail3 });
NewAPInvoicedetail1.Compose(new object[] { NewAPInvoiceheader, NewAPInvoicebatch, NewAPInvoicedetail4 });
NewAPInvoicedetail2.Compose(new object[] { NewAPInvoiceheader });
NewAPInvoicedetail3.Compose(new object[] { NewAPInvoiceheader });
NewAPInvoicedetail4.Compose(new object[] { NewAPInvoicedetail1 });
NewAPInvoicebatch.Browse("((BTCHSTTS = 1) OR (BTCHSTTS = 7))", true);
NewAPInvoicebatch.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_NOINSERT);
NewAPInvoicebatch.Fields.get_FieldByName("PROCESSCMD").PutWithoutVerification("1"); // Process Command Code
NewAPInvoicebatch.Process();
NewAPInvoicebatch.Read();
NewAPInvoiceheader.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_INSERT);
NewAPInvoicedetail1.Cancel();
NewAPInvoicebatch.Fields.get_FieldByName("BTCHDESC").PutWithoutVerification("Test Batch 2"); // Description
NewAPInvoicebatch.Update();
NewAPInvoiceheader.Fields.get_FieldByName("IDVEND").set_Value("1450"); // Vendor Number
NewAPInvoiceheader.Fields.get_FieldByName("PROCESSCMD").PutWithoutVerification("7"); // Process Command Code
NewAPInvoiceheader.Fields.get_FieldByName("INVCDESC").PutWithoutVerification("Test Entry 2"); // Invoice Description
NewAPInvoiceheader.Process();
NewAPInvoiceheader.Fields.get_FieldByName("PROCESSCMD").PutWithoutVerification("4"); // Process Command Code
NewAPInvoiceheader.Process();
NewAPInvoicedetail1.RecordClear();
NewAPInvoicedetail1.RecordCreate(0);
NewAPInvoicedetail1.Fields.get_FieldByName("PROCESSCMD").PutWithoutVerification("0"); // Process Command Code
NewAPInvoicedetail1.Process();
NewAPInvoicedetail1.Fields.get_FieldByName("IDDIST").set_Value("INV"); // Distribution Code
NewAPInvoicedetail1.Fields.get_FieldByName("AMTDIST").set_Value("100.000"); // Distributed Amount
NewAPInvoicedetail1.Insert();
NewAPInvoicedetail1.Fields.get_FieldByName("CNTLINE").PutWithoutVerification("-1"); // Line Number
NewAPInvoicedetail1.Read();
NewAPInvoiceheader.Insert();
NewAPInvoiceheader.Fields.get_FieldByName("IDINVC").set_Value("5435"); // Document Number
NewAPInvoiceheader.Insert();
NewAPInvoicebatch.Read();
NewAPInvoiceheader.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_INSERT);
NewAPInvoicedetail1.Cancel();
NewAPInvoiceheader.Fields.get_FieldByName("CNTITEM").set_Value("2"); // Entry Number
NewAPInvoiceheader.Browse("", false);
NewAPInvoiceheader.Fetch();
NewAPInvoicedetail1.Fields.get_FieldByName("CNTLINE").PutWithoutVerification("20"); // Line Number
NewAPInvoicedetail1.Read();
NewAPInvoiceheader.Fields.get_FieldByName("AMTGROSTOT").set_Value("108.250"); // Document Total Including Tax
NewAPInvoiceheader.Update();
// Close and destroy the Accpac session
AccpacSession.Close();
AccpacSession = null;
Application.Run(new Form1());
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}