简体   繁体   English

未触发 VBA ComboBox 更改事件

[英]VBA ComboBox Change Event not triggered

I have this issue with the ComboBox Event Handler.我对 ComboBox 事件处理程序有这个问题。

I managed to create (and fill with items) the Comboboxes I wanted, the code seems to work fine.我设法创建(并填充了项目)我想要的组合框,代码似乎工作正常。 But after the program has run, if I try to pick one general item inside one of the comboboxes, it seems like the _Change Method is not called --> I cannot handle change events.但是在程序运行后,如果我尝试在其中一个组合框中选择一个常规项目,似乎没有调用 _Change 方法 --> 我无法处理更改事件。

Here is my class module (class name: "DB_ComboBox")这是我的类模块(类名:“DB_ComboBox”)

    Option Explicit

    Public WithEvents DB_ComboBoxEvents As MSForms.ComboBox
    Private DB_ComboBox_Line As Integer

    Private Sub DB_ComboBoxEvents_Change()
        MsgBox ("Line : " & DB_ComboBox_Line)
        'Here I want handle The comboboxes changes
        'But this routine is not called!

    End Sub

    Sub Box(CBox As MSForms.ComboBox)
        Set DB_ComboBoxEvents = CBox
    End Sub


    Public Property Let Line(value As Integer)
        DB_ComboBox_Line = value
    End Property

    Public Property Get Line() As Integer
        Line = DB_ComboBox_Line
    End Property

And here is my "Main module", in which I create the comboboxes and pass them to a Collection of "DB_ComboBox"这是我的“主模块”,我在其中创建组合框并将它们传递给“DB_ComboBox”的集合

        Sub CreateComboBox(IncCBoxes)

        Dim curCombo As MSForms.ComboBox
        Dim rng As Range
        Dim tot_items As Integer
        Dim incAddItem As Integer
        Dim incAddItemBis As Integer
        Dim itemBaseArray() As String
        Dim TEMP_ComboBoxInst As New DB_ComboBox


        Set rng = ActiveSheet.Range("J" & IncCBoxes)

        Set curCombo = ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, DisplayAsIcon:=False, Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height).Object



         'Add the items
        itemBaseArray = Split(Foglio7.Cells(IncCBoxes, DBColFileComboIndexErrori), ";")

        For incAddItem = 0 To UBound(itemBaseArray)

            Dim itemLastArray() As String
            itemLastArray = Split(itemBaseArray(incAddItem), ",")

            For incAddItemBis = 0 To UBound(itemLastArray)
                curCombo.AddItem (itemLastArray(incAddItemBis))
            Next

        Next


        TEMP_ComboBoxInst.Box curCombo
        TEMP_ComboBoxInst.Line = IncCBoxes
        customBoxColl.Add TEMP_ComboBoxInst


    End Sub

Can anyone please tell me what I'm missing?谁能告诉我我错过了什么?

Thank you very much非常感谢

This looks like a timing-issue: Running this code in another open file will work.这看起来像是一个时间问题:在另一个打开的文件中运行此代码将起作用。 In same file it does not.在同一个文件中它没有。 Seperate the adding to your class from the adding of the OLEControl ie: use Application.ontime now将添加到您的类与添加 OLEControl 分开,即:现在使用 Application.ontime

see code below:见下面的代码:

Private customBoxColl As New Collection

Sub CreateComboBox(IncCBoxes As Long)

        Dim curCombo As MSForms.ComboBox
        Dim rng As Range
        Dim tot_items As Integer
        Dim incAddItem As Integer
        Dim incAddItemBis As Integer
        Dim itemBaseArray() As String
        Dim itemLastArray() As String

        Set rng = ActiveSheet.Range("J" & IncCBoxes)

        With ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, DisplayAsIcon:=False, Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height)
            Set curCombo = .Object
        End With

         'Add the items
        itemBaseArray = Split(Foglio7.Cells(IncCBoxes, DBColFileComboIndexErrori), ";")
        For incAddItem = 0 To UBound(itemBaseArray)
            itemLastArray = Split(itemBaseArray(incAddItem), ",")
            For incAddItemBis = 0 To UBound(itemLastArray)
               curCombo.AddItem (itemLastArray(incAddItemBis))
            Next
        Next
    Application.OnTime Now, "'CallToClass """ & curCombo.Name & """,""" & IncCBoxes & "'"
End Sub

Sub CalltoClass(ctl As String, myline As Long)
Dim TEMP_ComboBoxInst As New DB_ComboBox
        TEMP_ComboBoxInst.Box ActiveSheet.OLEObjects(ctl).Object
        TEMP_ComboBoxInst.line = myline
        customBoxColl.Add TEMP_ComboBoxInst
End Sub

I know this doesn't apply to your specific problem, but I'll just post this here for any others who may have this problem.我知道这不适用于您的具体问题,但我会在此处为可能遇到此问题的任何其他人发布此信息。 In my case, the events stopped firing because I had just copied my database into a new Github repo.就我而言,事件停止触发,因为我刚刚将我的数据库复制到新的 Github 存储库中。

On reopening Access, the events weren't firing while they had been fine the day before, which completely stumped me, especially since none of the SO answers seemed to address my issue.在重新打开 Access 时,事件在前一天还好时并没有触发,这完全让我感到困惑,特别是因为没有一个 SO 答案似乎解决了我的问题。 Basically, Access blocks macros and code, and requires it to be reenabled by clicking OK on the little yellow warning at the top of the screen.基本上,Access 会阻止宏和代码,并要求通过单击屏幕顶部黄色小警告上的“确定”来重新启用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM