Controls.Add("forms.commandbutton.1")

  • Thread starter Thread starter Ron Dahl
  • Start date Start date
R

Ron Dahl

I am using the code posted below. The Commanbutton gets added just fine,
but when I click on it, it doesn't go to the cmdOK_click() procedure. It
just acts as if there is no procedure.
What am I missing?
Thanks in advance for any help.
Ron Dahl

With frmVersionInfo.Controls.Add("forms.commandbutton.1")
.Name = "cmdOK"
.Caption = "OK"
.Font.Size = 8
.Left = 196
.Width = 40
.Top = 318
.Height = 20
End With

Sub cmdOK_Click()
MsgBox "Hello!"
End Sub
 
I guess it runs the (empty/non-existing) frmVersionInfo's module's "cmdOK_Click", not the
one in your old module.
 
I don't really understand your answer. What can I do to fix the problem?
You mention old module, but all of the code is in one userform. I don't
know where the empty/non-existing module comes into play
Any additional help you could give me would be greatly appreciated.
Thanks in advance,
Ron Dahl
 
Hi Ron

So all this code is already in frmVersionInfo ? Why don't you have the button there too,
tested and ready to go ? Set its Visible to False and True to hide/show it -or Enabled to
False and True instead, it's even more obvoius that "everything is complete" or whatever
this is for. Just like the EULA buttons that come to life when you check "I agree to the
terms" box.
 
Yes, the code is already there. I posted another response that no one has
replied to yet, and put a better example of the code at the end of this
message.
It does indeed work fine if I add the button in advance. I actually have
branch statements in the UserForm_Initialize procedure, and it puts
different command buttons on the userform depending on which procedure is
calling the userform.

It will save me creating numerous userforms with different commandbutton
combinations on it if I can get this to work.
Thanks for your all your help so far.
Ron Dahl

Private Sub UserForm_Initialize()
Dim cmd1 As msforms.CommandButton
Set cmd1 = UserForm1.Controls.Add("Forms.CommandButton.1")
With cmd1
.Name = "cmdOK"
.Caption = "OK"
.Font.Size = 8
.Left = 10
.Width = 40
.Top = 10
.Height = 20
End With
End Sub

Sub cmdOK_Click()
MsgBox 7
End Sub
 
Yes, the code is already there. Please see my latest reply to Harald, and
I'll avoid reposting it here.
Thanks for all your help so far.
Ron
 
Hi Ron

Rethink this, for safety, stability and simplicity. Add the maximum amount
of needed buttons in advance and position/title them depending on your
needs. Here's a sample with commandbuttons Btn1, Btn2 and Btn3 on a
Userform1.
Userform code:

'*****************************
Option Explicit

Dim MyMode As Long

Public Sub SetMeUp(Mode As Long)
MyMode = Mode
Select Case Mode
Case 1
Btn1.Caption = "OK"
Btn1.Visible = True
'add positioning also !
Btn2.Caption = "Cancel"
Btn2.Visible = True
Btn3.Caption = ""
Btn3.Visible = False
Case 2
Btn1.Caption = "Next"
Btn1.Visible = True
Btn2.Caption = "Previous"
Btn2.Visible = True
Btn3.Caption = "Cancel"
Btn3.Visible = True
Case 3
Btn1.Caption = "Yes"
Btn1.Visible = True
Btn2.Caption = "No"
Btn2.Visible = True
Btn3.Caption = "Undecided"
Btn3.Visible = True
End Select
End Sub

Private Sub Btn1_Click()
Select Case MyMode
Case 1
MsgBox "OK Fine"
Case 2
MsgBox "Next OK"
Case 3
MsgBox "Glad you agree"
End Select
Unload Me
End Sub

Private Sub Btn2_Click()
Select Case MyMode
Case 1
MsgBox "Cancelling"
Case 2
MsgBox "Previous OK"
Case 3
MsgBox "Silly you"
End Select
Unload Me
End Sub

Private Sub Btn3_Click()
Select Case MyMode
Case 2
MsgBox "Cancel OK"
Case 3
MsgBox "Make up your mind"
End Select
Unload Me
End Sub
'*****************************

Test in a standard module:

'*****************************
Sub test()
Call UserForm1.SetMeUp(1)
UserForm1.Show
Call UserForm1.SetMeUp(2)
UserForm1.Show
Call UserForm1.SetMeUp(3)
UserForm1.Show
End Sub
'*****************************
 
Thanks again Harald,

What you gave me works great, and it also gave me some more ideas and a new
direction to go in.

I still wonder from a purely theoretical viewpoint why I can't add the
CommandButtons in the initialize event and have them work, but it's a moot
point now.

Thanks again,
Ron Dahl
 
Hi Ron

Glad you liked it.
As to the still remaining question, and as you probably guessed; I don't
know. But I believe that a new control will initialize it's own code and
discard what's already there. When you add a button, it's "CommandButtonX"
where X is the next free index number, and CommandButtonX_Click is its newly
generated event. After renaming split seconds later, you will probably get a
conflict that is secretly dealt with one way or another -not to your
advantage, if I understand your results. You may have to program the modules
themselves directly, as described in
http://www.cpearson.com/excel/vbe.htm
 
Back
Top