cancel
Showing results for 
Search instead for 
Did you mean: 

ItemEvent and Query

Former Member
0 Kudos

Hi,

I created a program that open the BP Master Data form with FORM_LOAD (ItemEvent) and add a combobox on it.

When I write the ValidValues of this combobox in the code, it works perfectly.

But when I try to insert the ValidValues with the result of a query, it doesn't work...

Is it possible to do queries inside an ItemEvent ?

Thank you !

Thibault

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello,

What is the error that you get ?

Former Member
0 Kudos

Hello,

Here is my code

Friend Class AutrePartenaire

    Private WithEvents SBO_Application As SAPbouiCOM.Application
    Private SboGuiApi As SAPbouiCOM.SboGuiApi
    Private oCompany As SAPbobsCOM.Company
    Private oRecord As SAPbobsCOM.Recordset
    Private oDBDatabase As SAPbouiCOM.DBDataSource
    Private oForm As SAPbouiCOM.Form
    Private oItem As SAPbouiCOM.Item
    Private oCombo As SAPbouiCOM.ComboBox
    Private i As Integer

    Private Sub CreationApplication()
        Try
            SboGuiApi = New SAPbouiCOM.SboGuiApi
            SboGuiApi.Connect(Environment.GetCommandLineArgs.GetValue(1))
            SBO_Application = SboGuiApi.GetApplication()
        Catch
            SBO_Application.MessageBox(Err.Description)
        End Try
    End Sub

    Private Sub ConnectionSociete()
        Dim sCookie As String
        Dim sCookieContext As String
        Try
            oCompany = New SAPbobsCOM.Company
            sCookie = oCompany.GetContextCookie
            sCookieContext = SBO_Application.Company.GetConnectionContext(sCookie)
            oCompany.SetSboLoginContext(sCookieContext)

            If (oCompany.Connect() <> 0) Then
                SBO_Application.MessageBox("Erreur durant la connection à la base de données!")
                Exit Sub
            End If
        Catch
            SBO_Application.MessageBox(Err.Description)
        End Try
    End Sub


    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent
        If (pVal.EventType = SAPbouiCOM.BoEventTypes.et_FORM_LOAD And pVal.BeforeAction = False) Then
            If pVal.FormType = 134 Then
                AjoutComboATypePart()
            End If
        End If
    End Sub

    Private Sub AjoutComboATypePart()
        Try
            For i = 0 To SBO_Application.Forms.Count - 1
                oForm = SBO_Application.Forms.Item(i)

                If (oForm.TypeEx = 134) Then
                    oForm.Freeze(True)

                    oItem = oForm.Items.Add("Combo", SAPbouiCOM.BoFormItemTypes.it_COMBO_BOX)
                    oItem.Left = 283
                    oItem.Top = 7
                    oItem.Height = 14
                    oItem.DisplayDesc = True
                    oCombo = oItem.Specific

                    oRecord = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecord.DoQuery("SELECT Code, Name FROM [@ATPA] ORDER BY Code")
                    oRecord.MoveFirst()

                    While Not (oRecord.EoF)
                        oCombo.ValidValues.Add(oRecord.Fields.Item("Code").Value, oRecord.Fields.Item("Name").Value)
                        oRecord.MoveNext()
                    End While
                    oForm.Freeze(False)
                End If
            Next
            oDBDatabase = oForm.DataSources.DBDataSources.Add("OCRD")
            oCombo.DataBind.SetBound(True, "OCRD", "U_CTypePart")
        Catch
            SBO_Application.MessageBox(Err.Description)
        End Try
    End Sub

    Private Sub SBO_Application_AppEvent(ByVal EventType As SAPbouiCOM.BoAppEventTypes) Handles SBO_Application.AppEvent
        Try
            If (EventType = SAPbouiCOM.BoAppEventTypes.aet_ShutDown) Then
                System.Windows.Forms.Application.Exit()
            End If
        Catch
            SBO_Application.MessageBox(Err.Description)
        End Try
    End Sub

    Public Sub New()
        MyBase.New()
        CreationApplication()
        ConnectionSociete()
    End Sub

End Class

And I have the error :

QueryInterface error for SAPbobsCOM.ICompany interface...

Thanks !

Former Member
0 Kudos

What is the line that gives you this error ?

do you call the function ConnectionSociete right after CreationApplication ?

an other point, I don't think this line

oDBDatabase = oForm.DataSources.DBDataSources.Add("OCRD")

will work, because OCRD is already added to the form.

Former Member
0 Kudos

This line gives the error :

oRecord = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

And when I populate my combobox manually, it works very well...

Former Member
0 Kudos

The problem is not in the function that would add value to your combo, but with your oCompany object.

do you call the function ConnectionSociete before you catch the event ?

Former Member
0 Kudos

Well the programm calls the functions CreationApplication and ConnectionSociete.

And when I catch the itemEvent, the function that add my combo is called.

So I think, the function is called before the event, isn't it?

Former Member
0 Kudos

Thibault,

Sorry I didn't see the end of your program where you have the New sub.

I tried your code, as is, and it works on my computer.

You may try to reinstall the DI API.

Former Member
0 Kudos

Sébastien,

I reinstalled DI API like you said and it works !

Do you know why these kind of errors appears whereas the code is working?

I spent many hours trying to find....

But thank you very much for your help

Regards,

Thibault

Answers (1)

Answers (1)

AdKerremans
Active Contributor
0 Kudos

Hi Thibault,

It is possible to do a query inside an itemevent.

Can you post your code, so we can take a look at it and what error do you get?

Regards

Ad