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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Viewfinder error 1

Status
Not open for further replies.

ankie320

Programmer
May 18, 2011
12
CA
I used the code that i found here for access to the viewfinder. How can i fix this problem, is it due to my session config?

It is in VB.net and im using Accpac 6.0A. And I'm getting this error:

Error HRESULT E_FAIL has been returned from a call to a COM component.

heres the code:

Dim fnd As AccpacFinder.ViewFinder
Dim mySearchArray() As Object = {1, 2, 3}
Dim myDisplayArray() As Object = {1, 2, 3}
Dim myReturnArray() As Object = {1, 2}

'Me.Cursor = Cursors.WaitCursor
fnd = New AccpacFinder.ViewFinder

fnd.Session = session
fnd.ViewID = "OE0520"
fnd.ViewOrder = 0
'fnd.DisplayName = session.CompanyName

fnd.SearchFieldIDs = mySearchArray
fnd.DisplayFieldIDs = myDisplayArray
fnd.ReturnFieldIDs = myReturnArray

fnd.Filter = ""
fnd.Finder() 'this line throws the error

If Not IsNothing(fnd.ReturnFieldValues) Then
lblDesc.Text = Trim(fnd.ReturnFieldValues(0))
End If

fnd = Nothing

 
Look at inner exception to see what the exact error is. Or use try {} catch {} to catch the session error.

The code seems to be okay.
 
thanks for the reply. but i have tried error trapping with try-catch and it always causing a problem to a program "vshost32.exe". seems like it would crash it whenever i add try-catch to this code only, the rest of my code doesnt have any problem when i have exception handling.

ill try to check deeper into the exception thrown to find out what this error really is.
 
i still cant fix this problem. this what im getting whenever i run my code.

System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2147467259
Message=Error HRESULT E_FAIL has been returned from a call to a COM component.
Source=WindowsApplication2
StackTrace:
at AccpacFinder.IViewFinder.Finder()
at WindowsApplication2.Form1.Button3_Click(Object sender, EventArgs e) in C:\Users\aaron\Documents\Sage Accpac\Solution\NET\Automation\WindowsApplication2\WindowsApplication2\Form1.vb:line 317
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApplication2.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
InnerException:

I can't try/catch the code for some reason it will crash my application, so instead i copied the entire exception detail.

any help is appreciated. thanks

 
Is your session variable valid? You're also initializing your array of visible fields as an array of objects. Try an array of Integer. Here is some code that I use:

Code:
        Dim fnd As New AccpacFinder.ViewFinder
        Dim VisibleFields() As Integer = {1, 3, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}
        fnd.Session = Session
        fnd.ViewID = "GL0001"
        fnd.DisplayFieldIDs = VisibleFields
        fnd.ReturnFieldIDs = 1
        If fnd.Finder = True Then
            ExcelApp.ActiveCell.Value = "'" & Trim(fnd.ReturnFieldValues)
        End If
        fnd = Nothing
 
ive tried it as an integer array, but didnt work. my session config is pretty basic i think as im only doing a sample code, ive used it in another code and it works but maybe it's the problem:

Dim session As AccpacCOMAPI.AccpacSession
session = New AccpacCOMAPI.AccpacSession

session.Init("", "OE", "OE1100", "60A")
session.Open("ADMIN", UCase(""), "SAMLTD",System.DateTime.Now, 0, "")

i explicitly converted the string to UCase just to be sure, but originally it was just a plain "" text.

for program name, i did try mix-match it with the viewID's.
 
Code:
Session.Init("", "XY", "XY0001", "60A")
Session.Open("ADMIN","ADMIN","SAMLTD",System.DateTime.Now,0,"")

Use XY and XY0001 when automating Accpac. It let's the System Manager know that you're an external application.
Even when there is no password you use ADMIN.
 
thanks for that advice about session's config. but unfortunately, i still get the same error. i've included a try/catch to determine if its session error but it isnt. it is a general type of exception ive been getting.

what could it be?
 
One big pain in the rear with macros and sample data is that the sample date is in the year 2018-2020. System.DateTime.Now will cause a failure.

Put a watch on your Session variable and step through the lines of code to see what it looks like as it passes through the .Init and .Open.
 
I transferred the same code into VB6 and worked perfectly. now, i wonder what could be causing the problem (in Vb.net), when i'm only calling the finder and not modifying it. it has to be the same AccpacCOMAPI 1.0 and accpac finder 1.0 right?

i will take a look along those lines djang.
 
Those are the correct versions. Confirm that the System.DateTime.Now method is returning what Accpac wants.
 
Im getting this after tweaking code and thoroughly checkin the session and date. Both seemed correct.

System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageA(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApplication2.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


My output window shows these lines:

1. A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in WindowsApplication2.exe
2. A first chance exception of type 'System.AccessViolationException' occurred in System.Windows.Forms.dll
3. Error HRESULT E_FAIL has been returned from a call to a COM component.
 
Just to confirm - once you make the call to Session.Open you're able to access other properties of your session variable?

Do you have multiple versions of Accpac installed on your computer?
 
Yes, as well as the finder's sessions, after being set. I only have one version of accpac installed. (Sage ERP Accpac 500 6.0A)
 
The code i translated from vb6 is this one:

Dim session2 As AccpacCOMAPI.AccpacSession
session2 = CreateObject("ACCPAC.Session") 'tried this too: session2 = New AccpacCOMAPI.AccpacSession

session2.Init("", "XY", "XY0001", "60A")
session2.Open("ADMIN", "ADMIN", "SAMLTD", System.DateTime.Now.AddYears(9), 0, "")
Dim oFind As ViewFinder
Dim SearchArray() As Integer = {1, 2, 3}
Dim DisplayArray() As Integer = {1, 2, 3}
Dim ReturnArray() As Integer = {1, 2}

Try


oFind = New ViewFinder
oFind.Session = session2
oFind.ViewID = "OE0520"
oFind.ViewOrder = 0
oFind.DisplayFieldIDs = DisplayArray
oFind.SearchFieldIDs = SearchArray
oFind.ReturnFieldIDs = 1
oFind.Filter = ""

If oFind.Finder = True Then 'this is where it blows up
If Not IsNothing(oFind.ReturnFieldValues) Then
lblDesc.Text = Trim(oFind.ReturnFieldValues(0))
End If
End If

oFind = Nothing

session2.Close()
Catch ex As Exception


For j = 0 To session2.Errors.Count - 1
Console.WriteLine(j.ToString() + ". " + session2.Errors.Item(j))
Next

End Try
 
Probably this code will work if you try and test it, but I think may have the answer. My Sage accpac intelligence analysis 6.0a and connector 6.0a has expired. Im getting a warning saying that whenever i open accpac. This could be the issue, but i maybe wrong again.
 
Possibly but that doesn't quite make sense.

Let's try having you create four integer variables called aMajor, aMinor, aBuild and aRevision and calling:
Code:
Session.GetVersion(aMajor, aMinor, aBuild, aRevision)

Then see what they contain.

The biggest difference between your code and mine at this point is that I've created a signon session and generated the session through that. In order to run macros, for example, you have to have a signon session created:
Code:
    Dim lSignonID As Integer
    Dim sessmgr As AccpacSessionManager.AccpacSessionMgr

I use this to log on to Accpac but it does ask for a user ID and password.

Code:
    Private Function SignOnToAccpac() As Boolean
        SignOnToAccpac = True
        If Not (Session Is Nothing) Then
            Exit Function
        End If
        lSignonID = 0 ' MUST be initialized to 0 since you don't have a signon ID yet
        sessmgr = New AccpacSessionManager.AccpacSessionMgr
        With sessmgr
            .AppID = "XY"
            .AppVersion = "56A" '<-- You'll have 60A here.
            .ProgramName = "XY0001"
            .ServerName = "" ' empty string if running on local computer
            .ForceNewSignon = True
            .CreateSession("", lSignonID, Session) ' first argument is the object handle (if you don't have one, pass "")
        End With ' mSessMgr
        If IsNothing(Session) Then
            SignOnToAccpac = False
        Else
            OpenAndComposeViews()
        End If

    End Function

I don't know if that will help. Give it a try and see if it does help. Then we can work backwards.
 
DEP - i disabled it and now every code i have works. including printing reports.

Thanks for your help, djang. Still great advice youve given me as Im very new on accpac programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top