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!

Passing the actual accpac error message to display in my program 2

Status
Not open for further replies.

SMI168

Programmer
Sep 29, 2009
18
ZA
Hi There, I'm currently inserting customers, invoices, credits into AccPac and whenever there are any problems with the data or if AccPac throws out an error messages, my code displays an generic error from my try catch statement:
Try...

Catch SqlExceptionErr As SystemException
MessageBox.Show("ACCPAR ERROR", "ACCPAC IE Customers Update", MessageBoxButtons.OK, _
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)

I then have to manually look for the error in the dataset or try to insert the line manually into AccPac to determine why my program fell over. Is there any way that the actual error message that is given by AccPac can be displayed via my code to the user? This will make pin pointing the error much more easy and save me lots of time.

Thanks in advance...
 
Record a macro in Accpac and look at how they create the error handler.
 
Hi Ettiene, thanks for the reply. I've recorded a macro in AccPac and see that it calls up On Error GoTo ACCPACErrorHandler and then after it runs through some code it use the code :

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

I've tried using this in my code but it doesnt seem to be working. Am i approching this correctly? Thank you...
 
I've got the code to work but it still not doing what i require it to do. If i try to manually insert an invoice into AccPac for a customer that hasnt yet been added to AccPac i get an error saying "Invalid Customer". When i try to add this same invoice via my code into AccPac I want that "Invalid Cutomer" error message to be displayed to the user. All i get now is a messagebox with the message "Error HRESULT E_FAIL has been returned from a call to a COM component" which is what the VB compiler shows. Below is my Code :

Imports System
Imports System.IO
'Imports Microsoft.SqlServer.Management.Common
'Imports Microsoft.SqlServer.Management.Smo
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports ACCPAC.Advantage
Public Class InvoiceUser1
Public session As Session = Nothing
'Public session2 As Session = Nothing
Public mDBLinkCmpRW As DBLink = Nothing

Dim ARINVOICE1batch As ACCPAC.Advantage.View
Dim ARINVOICE1batchFields As ACCPAC.Advantage.ViewFields
Dim ARINVOICE1header As ACCPAC.Advantage.View
Dim ARINVOICE1headerFields As ACCPAC.Advantage.ViewFields
Dim ARINVOICE1detail1 As ACCPAC.Advantage.View
Dim ARINVOICE1detail1Fields As ACCPAC.Advantage.ViewFields
Dim ARINVOICE1detail2 As ACCPAC.Advantage.View
Dim ARINVOICE1detail2Fields As ACCPAC.Advantage.ViewFields
Dim ARINVOICE1detail3 As ACCPAC.Advantage.View
Dim ARINVOICE1detail3Fields As ACCPAC.Advantage.ViewFields
Dim ARINVOICE1detail4 As ACCPAC.Advantage.View
Dim ARINVOICE1detail4Fields As ACCPAC.Advantage.ViewFields
Public Sub InitializeDBLink()
mDBLinkCmpRW = Session.OpenDBLink(DBLinkType.Company, DBLinkFlags.ReadWrite)
End Sub
Public Sub OpenInvoice()

ARINVOICE1batch = mDBLinkCmpRW.OpenView("AR0031")
ARINVOICE1batchFields = ARINVOICE1batch.Fields

ARINVOICE1header = mDBLinkCmpRW.OpenView("AR0032")
ARINVOICE1headerFields = ARINVOICE1header.Fields

ARINVOICE1detail1 = mDBLinkCmpRW.OpenView("AR0033")
ARINVOICE1detail1Fields = ARINVOICE1detail1.Fields

ARINVOICE1detail2 = mDBLinkCmpRW.OpenView("AR0034")
ARINVOICE1detail2Fields = ARINVOICE1detail2.Fields

ARINVOICE1detail3 = mDBLinkCmpRW.OpenView("AR0402")
ARINVOICE1detail3Fields = ARINVOICE1detail3.Fields

ARINVOICE1detail4 = mDBLinkCmpRW.OpenView("AR0401")
ARINVOICE1detail4Fields = ARINVOICE1detail4.Fields

Dim vb As ACCPAC.Advantage.View() = {ARINVOICE1header}
ARINVOICE1batch.Compose(vb)

Dim vh As ACCPAC.Advantage.View() = {ARINVOICE1batch, ARINVOICE1detail1, ARINVOICE1detail2, ARINVOICE1detail3}
ARINVOICE1header.Compose(vh)

Dim vd1 As ACCPAC.Advantage.View() = {ARINVOICE1header, ARINVOICE1batch, ARINVOICE1detail4}
ARINVOICE1detail1.Compose(vd1)

Dim vd2 As ACCPAC.Advantage.View() = {ARINVOICE1header}
ARINVOICE1detail2.Compose(vd2)

Dim vd3 As ACCPAC.Advantage.View() = {ARINVOICE1header}
ARINVOICE1detail3.Compose(vd3)

Dim vd4 As ACCPAC.Advantage.View() = {ARINVOICE1detail1}
ARINVOICE1detail4.Compose(vd4)

End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

On Error GoTo ACCPACErrorHandler

'GroupBox4.BackColor = Color.AliceBlue
txtStart.Text = TimeOfDay
'Button1.Enabled = False
'btnClose.Enabled = False
lblDetails.Refresh()
lblDetails.Text = "Inserting User1 Invoice : "

'Create session to accpac...
session = New Session()

'Initialise session and login...
session.Init("", "", "", "")
'session.Open("", "", "", System.DateTime.Now, 0)
session.Open("", "", "", 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=;database=;user id=;password=")
Dim SQL As String

SQL = "select * from AI_Invoices_User1_Test order by BatchDesc, HeaderCustNumber, HeaderDocNumber, DetailTaxStatus"

objconnection.Open()

Dim cmd As New SqlCommand(SQL, objconnection)

Dim ds As New DataSet
Dim SqlDataAdapter = New SqlDataAdapter(cmd).Fill(ds, "AI_Invoices_User1_Test")
Dim WS_BatchDesc As String
Dim WS_Customer As String
Dim WS_DocNo As String
Dim VAT As String

WS_BatchDesc = ""
WS_Customer = ""
WS_DocNo = ""

'Try

For Each dr As DataRow In ds.Tables(0).Rows

Application.DoEvents()
txtDetail3.Refresh()
txtDetail4.Refresh()
txtDetail3.Text = (dr("HeaderDocNumber").ToString)
txtDetail4.Text = (dr("HeaderCustNumber").ToString)
VAT = (dr("DetailTaxStatus").ToString)

If WS_BatchDesc <> dr("BatchDesc").ToString() Then
'Create the batch
ARINVOICE1batchFields.FieldByName("PROCESSCMD").SetValue("1", False)
ARINVOICE1batch.RecordCreate(1)
ARINVOICE1batchFields.FieldByName("BTCHDESC").SetValue(dr("BatchDesc").ToString(), False)
ARINVOICE1batch.Update()

'Set the description to storage
WS_BatchDesc = dr("BatchDesc").ToString()
End If

If (WS_Customer = dr("HeaderCustNumber").ToString()) And (WS_DocNo = dr("HeaderDocNumber").ToString()) Then
ARINVOICE1detail1.GoBottom()
ARINVOICE1detail1.RecordClear()
ARINVOICE1detail1.RecordGenerate(False)
ARINVOICE1detail1Fields.FieldByName("IDACCTREV").SetValue(dr("DetailRevAccount").ToString(), False)
ARINVOICE1detail1Fields.FieldByName("AMTEXTN").SetValue(dr("DetailValue").ToString(), False)
ARINVOICE1detail1Fields.FieldByName("TAXSTTS1").SetValue(dr("DetailTaxStatus").ToString(), False)
ARINVOICE1detail1Fields.FieldByName("SWDISCABL").SetValue("0".ToString(), False)
If VAT = 1 Then
ARINVOICE1detail1Fields.FieldByName("SWTAXINCL1").SetValue("1".ToString(), False)
Else
ARINVOICE1detail1Fields.FieldByName("SWTAXINCL1").SetValue("0".ToString(), False)
End If
ARINVOICE1detail1.Insert()
ARINVOICE1header.Update()
Else
ARINVOICE1header.RecordClear()
ARINVOICE1header.RecordGenerate(False)
ARINVOICE1headerFields.FieldByName("INVCTYPE").SetValue(dr("HeaderInvType").ToString(), False)
ARINVOICE1headerFields.FieldByName("IDCUST").SetValue(dr("HeaderCustNumber").ToString(), False)
ARINVOICE1headerFields.FieldByName("IDINVC").SetValue(dr("HeaderDocNumber").ToString(), False)
ARINVOICE1headerFields.FieldByName("TAXSTTS1").SetValue(dr("DetailTaxStatus").ToString(), False)
ARINVOICE1detail1Fields.FieldByName("IDACCTREV").SetValue(dr("DetailRevAccount").ToString(), False)
ARINVOICE1detail1Fields.FieldByName("AMTEXTN").SetValue(dr("DetailValue").ToString(), False)
ARINVOICE1headerFields.FieldByName("SPECINST").SetValue(dr("HeaderInstructions").ToString(), False)
ARINVOICE1headerFields.FieldByName("ORDRNBR").SetValue(dr("HeaderOrderNumber").ToString(), False)
ARINVOICE1headerFields.FieldByName("DATEINVC").SetValue(dr("HeaderInvoiceDate").ToString(), False)
ARINVOICE1headerFields.FieldByName("CUSTPO").SetValue(dr("HeaderPO").ToString(), False)
ARINVOICE1headerFields.FieldByName("IDSHIPNBR").SetValue(dr("HeaderShipNumber").ToString(), False)

ARINVOICE1detail1.GoBottom()
ARINVOICE1detail1.RecordClear()
ARINVOICE1detail1.RecordGenerate(False)
ARINVOICE1detail1Fields.FieldByName("IDACCTREV").SetValue(dr("DetailRevAccount").ToString(), False)
ARINVOICE1detail1Fields.FieldByName("AMTEXTN").SetValue(dr("DetailValue").ToString(), False)
ARINVOICE1detail1Fields.FieldByName("TAXSTTS1").SetValue(dr("DetailTaxStatus").ToString(), False)
ARINVOICE1detail1Fields.FieldByName("SWDISCABL").SetValue("0".ToString(), False)
If VAT = 1 Then
ARINVOICE1detail1Fields.FieldByName("SWTAXINCL1").SetValue("1".ToString(), False)
Else
ARINVOICE1detail1Fields.FieldByName("SWTAXINCL1").SetValue("0".ToString(), False)
End If
ARINVOICE1detail1.Insert()
ARINVOICE1header.Insert()

WS_Customer = dr("HeaderCustNumber").ToString()
WS_DocNo = dr("HeaderDocNumber").ToString()

End If
Next


txtEnd.Text = TimeOfDay
btnClose.Enabled = True
'Display a message that the record was added...
MessageBox.Show("Invoices User1 Added", "Click OK to continue", MessageBoxButtons.OK, _
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
'GroupBox4.BackColor = Color.Transparent
lblDetails.Text = ""
txtDetail3.Text = ""
txtDetail4.Text = ""
'Catch SqlExceptionErr As SystemException
' MessageBox.Show("ACCPAR ERROR", "ACCPAC IE Invoices User1 Insert", MessageBoxButtons.OK, _
' MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)

'End Try

ACCPACErrorHandler:
Dim lCount As Long
Dim lIndex As Long
Dim Errors As ACCPAC.Advantage.Errors

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

DisposeConnection()
End Sub
Public Sub DisposeConnection()
mDBLinkCmpRW.Dispose()
session.Dispose()
End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
 
Hi Tuba2007, I have data in my dataset, that once i try to insert it via my program to AccPac, it will cause AccPac to give me an error. If there is no error handling in my code it would fall over on the line :
ARINVOICE1headerFields.FieldByName("IDCUST").SetValue(dr("HeaderCustNumber").ToString(), False)
with the error message :
Error HRESULT E_FAIL has been returned from a call to a COM component

But with the error handling code i have currently, it gives me same error :
Error HRESULT E_FAIL has been returned from a call to a COM component
but its in a message box.
I need to get the actual AccPac error which reads :
Description: Invalid input. Customer number "5478525" does not exist.

Source: 13:48:12

thanks....
 
You need to use Session.Errors, as in

If Session.Errors Is Nothing Then ...
 
Thanks ettienne, it seems to be better now, but i still get an error message on line :
MsgBox(session.Errors.Item(lIndex))
saying : Argument 'Prompt' cannot be converted to type 'String'.

How do i get this to dispaly as string? Below is the code...

ACCPACErrorHandler:
Dim lCount As Long
Dim lIndex As Long
'Dim Errors As ACCPAC.Advantage.Errors

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

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

End If

Thanks for all the help thus far...
 
Hi tuba2007, 'description' didnt work but 'message' did. this is how the line of code looks now :
MsgBox(session.Errors.Item(lIndex).Message)
Many thanks to you and ettienne for the help :)

Take care & God bless...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top