Programmely written code don't work

  • Thread starter Thread starter Flemming Dahl
  • Start date Start date
F

Flemming Dahl

Hi,

On a userform certain actions course extra controls AND extra event-code for
thise controls - however the code looks right........... it will not
work......

A OptionButton is created "OptionButton1" and this code is made one the
userform's code module:
Private Sub OptionButton1_Click()
MsgBox "YES"
End Sub

What do i do to make the new code work ?

Thanks,
Flemming
 
Works for me in OfficeXP.
Are you trying to get the msgbox within the VBA window, or are you actually
bringing up the Userform in Excel and clicking the optionbutton there? Do
you have macros enabled?
Keith
 
The code that is create in runtime is created like this:

Sub AddButtonsCode()
Dim UFvbc As Object 'VBComponent
Dim code As String
Dim n As Long
Set UFvbc = ThisWorkbook.VBProject.VBComponents("UserForm1")
For n = 1 To 10
With UFvbc.CodeModule
code = ""
code = code & "Private Sub OptionButton" & n & "_Click" & vbCr
code = code & "Msgbox ""YES - OptionButton" & n & """" & vbCr
code = code & "End Sub"
.InsertLines .CountOfLines + 1, code
End With
Next
End Sub
 
Hey Keith

Looking at you answer made me think, that i have not explained my self
properly.

You see - the userform IS running, and by selection on a combobox - NEW
controls are added to the userform, and so is the event code for thise
controls. But no event takes place for the new controls.

Here a NEW control is a control that is created in runtime.

Flemming
 
Flemming,

This works fine for me too.

You haven't renamed the button, or anything like that? Do you see the button
on the form, do other controls work?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Then you have stepped outside the bounds of my experience- I'm just starting
to figure out how to add controls at runtime in .net :-)

Just out of curiosity, have you considered the option of creating the
controls (assuming there is a limited number) and just showing/hiding (and
possibly moving) them based on the option button events?
 
Yes - have considered show/hide......... but that will not give the
flexibility that i need.
 
Flemming,

That would be my approach. What flexibility does it not provide?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Show/Hide dos not provide the flexiblity of how many controls the user
needs.
In this case i am not going to make alle those controls and show/hide
procedures..........

Lets stop it here. It seems that you can not provide any extra information
to solve my problem.

Thanks,
Flemming
 
Flemming,

How about a different approach, namely to create a class module that can
handle all optionbutton controls.

First, create a class module, and name it clsOpt, with this code

Public WithEvents OptButton As MSForms.OptionButton

Private Sub OptButton_Click()
'add your specific code here
MsgBox "You clicked: " & OptButton.Caption
End Sub

Then in your userform event, add this code

Dim collOpt As Collection
Dim formButton As clsOpt

Private Sub UserForm_Initialize()
Dim oCtl As MSForms.Control
Set collOpt = New Collection
For Each oCtl In Me.Controls
If TypeOf oCtl Is MSForms.OptionButton Then
Set formButton = New clsOpt
Set formButton.optButton = oCtl
collOpt.Add formButton
End If
Next oCtl
End Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Back
Top