Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Accpac AP Invoice Batch

Status
Not open for further replies.

ksilha

Programmer
Jan 3, 2011
3
US
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());
}
}
}
}
 
Trim some more fat...
My best advice is get the basics running in VBA and then go to another language. Also add an error handler so that you get a meaningful error message.
 
I tried trimming more fat from both the VBA macro and my program. The VBA macro works, and I cannot determine what is missing from my program.

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 });
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);

// Close and destroy the Accpac session
AccpacSession.Close();
AccpacSession = null;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}

Compare to:

Sub MainSub()
'
' Sage Accpac Macro file: C:\Documents and Settings\silha\Desktop\test ap.AVB
' Recorded at: Mon Jan 03 16:33:32 2011
'

On Error GoTo ACCPACErrorHandler

' TODO: To increase efficiency, comment out any unused DB links.
Dim mDBLinkCmpRW As AccpacCOMAPI.AccpacDBLink
Set mDBLinkCmpRW = OpenDBLink(DBLINK_COMPANY, DBLINK_FLG_READWRITE)

Dim mDBLinkSysRW As AccpacCOMAPI.AccpacDBLink
Set mDBLinkSysRW = OpenDBLink(DBLINK_SYSTEM, DBLINK_FLG_READWRITE)

Dim temp As Boolean
Dim APINVOICE1batch As AccpacCOMAPI.AccpacView
Dim APINVOICE1batchFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0020", APINVOICE1batch
Set APINVOICE1batchFields = APINVOICE1batch.Fields

Dim APINVOICE1header As AccpacCOMAPI.AccpacView
Dim APINVOICE1headerFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0021", APINVOICE1header
Set APINVOICE1headerFields = APINVOICE1header.Fields

Dim APINVOICE1detail1 As AccpacCOMAPI.AccpacView
Dim APINVOICE1detail1Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0022", APINVOICE1detail1
Set APINVOICE1detail1Fields = APINVOICE1detail1.Fields

Dim APINVOICE1detail2 As AccpacCOMAPI.AccpacView
Dim APINVOICE1detail2Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0023", APINVOICE1detail2
Set APINVOICE1detail2Fields = APINVOICE1detail2.Fields

Dim APINVOICE1detail3 As AccpacCOMAPI.AccpacView
Dim APINVOICE1detail3Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0402", APINVOICE1detail3
Set APINVOICE1detail3Fields = APINVOICE1detail3.Fields

Dim APINVOICE1detail4 As AccpacCOMAPI.AccpacView
Dim APINVOICE1detail4Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0401", APINVOICE1detail4
Set APINVOICE1detail4Fields = APINVOICE1detail4.Fields

APINVOICE1batch.Compose Array(APINVOICE1header)

APINVOICE1header.Compose Array(APINVOICE1batch, APINVOICE1detail1, APINVOICE1detail2, APINVOICE1detail3)

APINVOICE1detail1.Compose Array(APINVOICE1header, APINVOICE1batch, APINVOICE1detail4)

APINVOICE1detail2.Compose Array(APINVOICE1header)

APINVOICE1detail3.Compose Array(APINVOICE1header)

APINVOICE1detail4.Compose Array(APINVOICE1detail1)


Dim APINVCPOST2 As AccpacCOMAPI.AccpacView
Dim APINVCPOST2Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0039", APINVCPOST2
Set APINVCPOST2Fields = APINVCPOST2.Fields


Dim APINVOICE3batch As AccpacCOMAPI.AccpacView
Dim APINVOICE3batchFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0020", APINVOICE3batch
Set APINVOICE3batchFields = APINVOICE3batch.Fields

Dim APINVOICE3header As AccpacCOMAPI.AccpacView
Dim APINVOICE3headerFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0021", APINVOICE3header
Set APINVOICE3headerFields = APINVOICE3header.Fields

Dim APINVOICE3detail1 As AccpacCOMAPI.AccpacView
Dim APINVOICE3detail1Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0022", APINVOICE3detail1
Set APINVOICE3detail1Fields = APINVOICE3detail1.Fields

Dim APINVOICE3detail2 As AccpacCOMAPI.AccpacView
Dim APINVOICE3detail2Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0023", APINVOICE3detail2
Set APINVOICE3detail2Fields = APINVOICE3detail2.Fields

Dim APINVOICE3detail3 As AccpacCOMAPI.AccpacView
Dim APINVOICE3detail3Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0402", APINVOICE3detail3
Set APINVOICE3detail3Fields = APINVOICE3detail3.Fields

Dim APINVOICE3detail4 As AccpacCOMAPI.AccpacView
Dim APINVOICE3detail4Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AP0401", APINVOICE3detail4
Set APINVOICE3detail4Fields = APINVOICE3detail4.Fields

APINVOICE3batch.Compose Array(APINVOICE3header)

APINVOICE3header.Compose Array(APINVOICE3batch, APINVOICE3detail1, APINVOICE3detail2, APINVOICE3detail3)

APINVOICE3detail1.Compose Array(APINVOICE3header, APINVOICE3batch, APINVOICE3detail4)

APINVOICE3detail2.Compose Array(APINVOICE3header)

APINVOICE3detail3.Compose Array(APINVOICE3header)

APINVOICE3detail4.Compose Array(APINVOICE3detail1)


APINVOICE3batch.Browse "((BTCHSTTS = 1) OR (BTCHSTTS = 7))", 1
APINVOICE3batch.RecordCreate 1

Exit Sub

ACCPACErrorHandler:
Dim lCount As Long
Dim lIndex As Long

If Errors Is Nothing Then
MsgBox Err.Description
Else
lCount = Errors.Count

If lCount = 0 Then
MsgBox Err.Description
Else
For lIndex = 0 To lCount - 1
MsgBox Errors.Item(lIndex)
Next
Errors.Clear
End If
Resume Next

End If
End Sub
 
Invoice batches are always inserted upon creation, so you probably shouldn't use VIEW_RECORD_CREATE_NOINSERT.
 
Thanks, I changed the line to "NewAPInvoicebatch.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_INSERT);" but I am still receiving the error.
 
I have done a similar task recently, my experience is that you might have to test if NewAPInvoicebatch.Exist == False, if yes, then RecordCreate(...), if not, then move forward to create header & detail...

Hope this is helpful.
 
A couple of things, First, in your catch(Exception e) try seeing if the AccpacSession.Errors object has anything in it. If there's something there, you can loop through the errors to see what is up with Accpac.

The second thing, There could be a duplicate document number or something that you're trying to insert, or a missing account number, or a poorly formatted account segment.

Third, I just checked my own code, and where you've got RECORD_CREATE_NOINSERT, I have RECORD_CREATE_INSERT. I think the Enum value of tagViewRecordCreateEnum.VIEW_RECORD_CREATE_INSERT is 1, but tagViewRecordCreateEnum.VIEW_RECORD_CREATE_NOINSERT is 0, so your C# code doesn't match your VB Macro code.

Finally, it helps to know what is your development/testing environment, XP/Vista/Win7? x86/x64? .Net2.0/3.5/4.0?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top