Run time error adding controls to form programatically

R

ROBW

Hi,

I've got some VBA that programatically adds additional optionbox controls to
a userform (call this FRM_CHOOSE). The addition is actually driven by
another userform (call this FRM_UPDATE) which up til now only contained
optionbutton and commandbutton controls and everything has been working fine.


I've now added a combobox to FRM_UPDATE however now when the script gets to
the point of adding the new optionbox control to the other form it errors out
with "Run-time error '-2147319767 (80028029)': Invalid forward reference, or
reference to uncompiled type.".

The code I'm using is:

Dim my_form As Object
Set my_form = ThisWorkbook.VBProject.VBComponents("FRM_CHOOSE")
buttonnam = "opt_" & prog_sel
With my_form.codemodule
.insertlines 30, " Elseif FRM_CHOOSE.opt_" & prog_sel & "= true
then"
.insertlines 31, " rangechoice = """ & "all" & prog_sel & """"
.insertlines 32, " linechoice = """ & prog_sel & "lines"""
.insertlines 33, " progchoice = """ & prog_sel & "prog"""
.insertlines 34, " sheetchoice = """ & prog_sel & """"
End With

---***this is now the point at which the code crashes***---

Set my_new_opt = my_form.Designer.Controls.Add("Forms.OPTIONBUTTON.1")
With my_new_opt
.Caption = prog_sel
.Top = ((my_form.Designer.Controls.Count - 6) * 18) - 12
.Left = 12
.Width = 63
.Name! = buttonnam
End With

This has previoulsy worked perfectly, the ONLY change was to add the
combobox to FRM_UPDATE which is itself unloaded before the code reaches this
point, having passed the "prog_sel" variable to the script. I don't
understand how this additional control could cause a subsequent error of this
nature - any advice?!

Many thanks,

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top