Recursive function...

rivagelp

New Member
Joined
Mar 31, 2012
Messages
2
Hi all,

My UserForm.Initialize() call SetControlEvents. This function purpose is to find all my ComboBox and TextBox controls and attach to them, a class module events. I use recursive because Frame and MultiPage are container. My Initial call to this function is :

Code:
SetControlEvents(Me.Controls)
At runtime, I obtain this error : "Compile error: Argument not optional"
and the ".Controls" is highlighted

When I try with this argument :

Code:
SetControlEvents(Me)
I obtain a "Runtime error 13:Type mismatch" and the line where VBA stops is
Code:
                  SetControlEvents (Ctrl.Pages(I))
Here is my complete function code :

Code:
Private Sub SetControlEvents(AControls As MSForms.Controls)
    Dim Ctrl As MSForms.Control
    Dim I As Integer
    Dim CtrlEvents As clsControlEvents                   ' Définition de notre Class Module dans l'unité "clsControlEvents"

    For Each Ctrl In AControls
      Select Case TypeName(Ctrl)
          Case "Frame"
              SetControlEvents (Ctrl)
          Case "MultiPage"
              For I = 0 To Ctrl.Pages.Count - 1
                  SetControlEvents (Ctrl.Pages(I))
              Next I
          Case "ComboBox"
              ' Reprendre la boucle du UserForm_Initialize()
              If Left(Ctrl.Name, Len(CBXSECTION) - 1) = CBXSECTION Then
                    Ctrl.Tag = ""
                    If Val(Right(Ctrl.Name, Len(Ctrl.Name) - Len(CBXSECTION))) > 1 Then
                        Ctrl.Enabled = False
                    End If
                        
                    Set CtrlEvents = New clsControlEvents     ' Les variables ComboBox et Form sont déclarées
                    Set CtrlEvents.ComboBox = Ctrl            ' dans notre Class Module "clsControlEvents"
                    Set CtrlEvents.Form = Me                  ' La variable Collection cbxSections est déclarée
                    cbxSections.Add CtrlEvents                ' au niveau de cette UserForm (tout en haut de ce module)
              
              End If
          Case "TextBox"
              If Left(Ctrl.Name, 3) = "tbx" Then
                  ' to become...
              End If
      End Select
    Next Ctrl
End Sub
Thank you for any suggestion/help.

Laurent
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I can't reproduce you error with:

Code:
Call SetControlEvents(Me.Controls)

but it may help to see your Class module code. Also what is CBXSECTION?
 
Upvote 0
hi Andrew, thank you for your fast response.
Yes, I've forgotten the Call statement... Now, it works without compiler errors.

CBXSECTIONS is a constant string equal to "CbxSection". It's my prefix for some ComboBox that I need to add _change() event.

Once again, thank you for your help.

Laurent

The corrected code now is :

Code:
Private Sub SetControlEvents(AControls As MSForms.Controls)
    Dim Ctrl As MSForms.Control
    Dim I As Integer
    Dim CtrlEvents As clsControlEvents                   ' Définition de notre Class Module dans l'unité "clsControlEvents"

    For Each Ctrl In AControls
      Select Case TypeName(Ctrl)
          Case "Frame"
              Call SetControlEvents(Ctrl.Controls)
          Case "MultiPage"
              For I = 0 To Ctrl.Pages.Count - 1
                  Call SetControlEvents(Ctrl.Pages(I).Controls)
              Next I
          Case "ComboBox"
              ' Reprendre la boucle du UserForm_Initialize()
              If Left(Ctrl.Name, Len(CBXSECTION) - 1) = CBXSECTION Then
                    Ctrl.Tag = ""
                    If Val(Right(Ctrl.Name, Len(Ctrl.Name) - Len(CBXSECTION))) > 1 Then
                        Ctrl.Enabled = False
                    End If
                        
                    Set CtrlEvents = New clsControlEvents     ' Les variables ComboBox et Form sont déclarées
                    Set CtrlEvents.ComboBox = Ctrl            ' dans notre Class Module "clsControlEvents"
                    Set CtrlEvents.Form = Me                  ' La variable Collection cbxSections est déclarée
                    cbxSections.Add CtrlEvents                ' au niveau de cette UserForm (tout en haut de ce module)
              
              End If
          Case "TextBox"
              If Left(Ctrl.Name, 3) = "tbx" Then
                  ' to become...
              End If
      End Select
    Next Ctrl
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,490
Messages
6,125,094
Members
449,205
Latest member
ralemanygarcia

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