Ajout de combobox

  • To start with - do not use 'On Error Resume Next' when developing. It masks errors making it harder to debug - which is what happened here.

    You declare a variable called 'cbox' in the General section of the userform. That's simply 'good programming practise' but it really should be defined as 'MSForms.ComboBox' rather than a generic control.

    You later refer to cbox1 with the line

    Set cbox1 = Me.Controls.("Forms.Combobox.1")

    ... but cbox1 does not exist as a control, it was only declared, not created. The 'On Error Resume Next' did what you expect it to do, ignored the error.

    The changes:

    • Define cbox1 as MSForms.ComboBox
    • Comment out the On Error Resume Next - if you then get errors while testing correct them as they arise. If it is an expected error that cannot be avoided then add an 'On Error Resume Next' before the line that causes the error but also add an 'On Error Goto 0' as the next line to cancel the Resume Next.
    • You need to include an 'Add' statement in the 'Set cbox...' line to actually create the combobox. I'm confused if you want to add this as a child of the userform or the frame, so both...
      1. Set cbox1 = fraInput.Controls.Add("Forms.Combobox.1")
      2. Set cbox1 = Me.Controls.Add("Forms.Combobox.1")
    • You may need to also change the location as it will overlap other controls as a child of both the userform and frame as the top position (zz) is set to 15
    • You need to Name the combobox exactly the same as the other controls so you can refer to it later in code. This is not strictly necessary if there is only 1 combobox as it'll always be named 'ComboBox1' but it's a good idea not to rely on default names.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!