簡體   English   中英

未觸發 VBA ComboBox 更改事件

[英]VBA ComboBox Change Event not triggered

我對 ComboBox 事件處理程序有這個問題。

我設法創建(並填充了項目)我想要的組合框,代碼似乎工作正常。 但是在程序運行后,如果我嘗試在其中一個組合框中選擇一個常規項目,似乎沒有調用 _Change 方法 --> 我無法處理更改事件。

這是我的類模塊(類名:“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

這是我的“主模塊”,我在其中創建組合框並將它們傳遞給“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

誰能告訴我我錯過了什么?

非常感謝

這看起來像是一個時間問題:在另一個打開的文件中運行此代碼將起作用。 在同一個文件中它沒有。 將添加到您的類與添加 OLEControl 分開,即:現在使用 Application.ontime

見下面的代碼:

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

我知道這不適用於您的具體問題,但我會在此處為可能遇到此問題的任何其他人發布此信息。 就我而言,事件停止觸發,因為我剛剛將我的數據庫復制到新的 Github 存儲庫中。

在重新打開 Access 時,事件在前一天還好時並沒有觸發,這完全讓我感到困惑,特別是因為沒有一個 SO 答案似乎解決了我的問題。 基本上,Access 會阻止宏和代碼,並要求通過單擊屏幕頂部黃色小警告上的“確定”來重新啟用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM