From: HaydenT on
'\\\\\\\\\\'MICROSOFT EXCEL 97 VISUAL BASIC EDITOR'//////////


I am trying to create at runtime. I can create a control at runtime, but I
can't get the control that was created at
runtime to execute any code for example see below

What I have done is:

'Create Userform and create a command button on the form. Put the following
code in the newly
'created form.
'

Dim myCmd As Control
Dim myButton As Boolean

Private Sub UserForm_Initialize()
myButton = False
End Sub

Private Sub CommandButton1_Click()
MsgBox "This Button was created at Run-time"
End Sub

Private Sub cmdCreate_Click()
If myButton = True Then GoTo skip
Set myCmd = frmRuntimeProblem.Controls.Add("Forms.CommandButton.1", ,
Visible)
myCmd.Left = 102
myCmd.Top = 50 '186
myCmd.Width = 100
myCmd.Height = 24
myCmd.Caption = "Show Message"
myButton = True
MsgBox "myCmd.Name = " & myCmd.Name
skip:
End Sub

From: PeterD on
On Thu, 3 Jul 2008 00:21:01 -0700, HaydenT
<HaydenT(a)discussions.microsoft.com> wrote:

>'\\\\\\\\\\'MICROSOFT EXCEL 97 VISUAL BASIC EDITOR'//////////
>
>
>I am trying to create at runtime. I can create a control at runtime, but I
>can't get the control that was created at
>runtime to execute any code for example see below
>
>What I have done is:
>
>'Create Userform and create a command button on the form. Put the following
>code in the newly
>'created form.
>'
>
>Dim myCmd As Control
>Dim myButton As Boolean
>
>Private Sub UserForm_Initialize()
> myButton = False
>End Sub
>
>Private Sub CommandButton1_Click()

^^^^^^^^^^^^^^

> MsgBox "This Button was created at Run-time"
>End Sub
>
>Private Sub cmdCreate_Click()
> If myButton = True Then GoTo skip
> Set myCmd = frmRuntimeProblem.Controls.Add("Forms.CommandButton.1", ,

^^^^^^^^^^^^^^^

>Visible)
> myCmd.Left = 102
> myCmd.Top = 50 '186
> myCmd.Width = 100
> myCmd.Height = 24
> myCmd.Caption = "Show Message"
> myButton = True
> MsgBox "myCmd.Name = " & myCmd.Name
>skip:
>End Sub

You never create CommandButton1... (Look at your Controls.Add name).
Also you call is wrong, you need to code it as:

.... frmRuntimeProblem.Controls.Add(VB.CommandButton, CommandButton1)

Your call doesn't give it a name, and as well, doesn't tell it
properly what type of control to add.

This is well documented in the VB help section.
From: PeterD on
On Thu, 03 Jul 2008 10:50:31 -0400, PeterD <peter2(a)hipson.net> wrote:

>On Thu, 3 Jul 2008 00:21:01 -0700, HaydenT
><HaydenT(a)discussions.microsoft.com> wrote:
>
>>'\\\\\\\\\\'MICROSOFT EXCEL 97 VISUAL BASIC EDITOR'//////////
>>
>>
>>I am trying to create at runtime. I can create a control at runtime, but I
>>can't get the control that was created at
>>runtime to execute any code for example see below
>>
>>What I have done is:
>>
>>'Create Userform and create a command button on the form. Put the following
>>code in the newly
>>'created form.
>>'
>>
>>Dim myCmd As Control
>>Dim myButton As Boolean
>>
>>Private Sub UserForm_Initialize()
>> myButton = False
>>End Sub
>>
>>Private Sub CommandButton1_Click()
>
> ^^^^^^^^^^^^^^
>
>> MsgBox "This Button was created at Run-time"
>>End Sub
>>
>>Private Sub cmdCreate_Click()
>> If myButton = True Then GoTo skip
>> Set myCmd = frmRuntimeProblem.Controls.Add("Forms.CommandButton.1", ,
>
> ^^^^^^^^^^^^^^^
>
>>Visible)
>> myCmd.Left = 102
>> myCmd.Top = 50 '186
>> myCmd.Width = 100
>> myCmd.Height = 24
>> myCmd.Caption = "Show Message"
>> myButton = True
>> MsgBox "myCmd.Name = " & myCmd.Name
>>skip:
>>End Sub
>
>You never create CommandButton1... (Look at your Controls.Add name).
>Also you call is wrong, you need to code it as:
>
>... frmRuntimeProblem.Controls.Add(VB.CommandButton, CommandButton1)
>
>Your call doesn't give it a name, and as well, doesn't tell it
>properly what type of control to add.
>
>This is well documented in the VB help section.

Uh, with quotes around the name... (sorry)