What's the best way to trigger activeX control change event

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,912
Office Version
  1. 365
Platform
  1. Windows
I have a userform with a bunch of controls. Some are comboboxes and they are dependent, fetching an array from an external database.

At some point a user might choose to 'reset' the form, clear all controls for new record entry. When this happens I want all change events to fire because they are used to regenerate the combo lists.

Generating a combo list example:
Code:
Public Property Get Divisions() As Variant
    Dim strSql As String
            
    With Me
        If CBool(Len(.cbxCountry.Text)) Then
            strSql = "SELECT DISTINCT DIVISION FROM BU_TBL WHERE COUNTRY = '" & .cbxCountry.Text & "'" & _
                     " AND SECTOR = '" & .cbxSector.Text & "';"
            Divisions = Application.Transpose(m_clsDB.RunScript(strSql).GetRows)
        Else
            Divisions = VBA.Array(vbNullString)
        End If
    End With
End Property
The property is used to load the combobox in form initialize event and combo change event.
Code:
Private Sub UserForm_Initialize()
    Call HookUp
    Call LoadRecord
    
    '// Load the dropdown lists
    With Me
        .cbxCountry.List = .Countries
        .cbxSector.List = .Sectors
        .cbxDivision.List = .Divisions
        .cbxBU.List = .Businesses
    End With
End Sub

Private Sub cbxSector_Change()
    With Me
        .cbxDivision.List = .Divisions
    End With
End Sub
The problem is when I clear the form for entry mode:
Code:
Private Sub EntryMode()
    Dim ctl As msForms.Control
    
    For Each ctl In Me.Controls
        With ctl
            If CBool(Len(.Tag)) Then
                .Object = Empty
                .Object.Locked = CBool(Split(.Tag, ";")(4))
            End If
        End With
    Next ctl
End Sub
Clearing the control does not fire the change event. Wondering if there is a cool way to trigger the change event within the loop? I don't especially want to write a line for each control who's list must be regenerated.
 
Last edited:
If this doesn't work then I don't know.

Try changing the cbxSector change event as follows - leaving your initial code as is.

Code:
Private Sub cbxSector_Change()

    With Me.cbxDivision
        If .Locked = True Then
           .Locked = False
           .List = .Divisions
           .Locked = True
        End If
    End With
    
End Sub

Do the same with the other combos.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Thanks Jaafar. I don't think it has to do with the locked property because the event fires up just as long as I don't clear control. Here I toggle the control value and the event fires up:

Code:
Private Sub EntryMode()
    Dim ctl As MSForms.Control
    
    m_objRECORDSET.MoveLast
    
    For Each ctl In Me.Controls
        With ctl
            If CBool(Len(.Tag)) Then
                .Object.Locked = CBool(Split(.Tag, ";")(4))
                'bug here
                Dim varval
                varval = .Object.Value
                .Object = Empty
                .Object.Value = varval
            End If
        End With
    Next ctl
End Sub
 
Upvote 0
Thanks Jaafar. I don't think it has to do with the locked property because the event fires up just as long as I don't clear control. Here I toggle the control value and the event fires up:

Code:
Private Sub EntryMode()
    Dim ctl As MSForms.Control
    
    m_objRECORDSET.MoveLast
    
    For Each ctl In Me.Controls
        With ctl
            If CBool(Len(.Tag)) Then
                .Object.Locked = CBool(Split(.Tag, ";")(4))
                'bug here
                Dim varval
                varval = .Object.Value
                .Object = Empty
                .Object.Value = varval
            End If
        End With
    Next ctl
End Sub

Hi jon.

I am afraid I couldn't replicate the problem. I can see this routine is part of a greater project so it is difficult to pinpoint where the problem is. Maybe , if you can break it down into a small example and replicate the same problem then it will become clearer why the change event doesn't fire.
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top