I have a poject with a form which has a Command Button, Text Box, and Label
control added to it at run time. It has taken me at least three hours (I'm
extremely new at programming) to figure out how to add the three controls to
my form at run time. Once I finally figured out how to do all that I
realized I didn't know how to add code to make my command button do
something. What I want to do is after my form is shown on the screen, the
label presents a question "How many text boxes do you want added to the
screen?". The user will enter the number (1 - 10) in the text box and then
hit ok. The code under that button should then remove the initial Label,
Text Box and Command Button controls, and add the specified number of text
boxes, and resize the screen according to the amount of text boxes added.

Re: Adding Code to a Command Button Added At Run Time by MikeD

MikeD
Wed Sep 20 08:22:44 CDT 2006


"Kevin E." <KevinE@discussions.microsoft.com> wrote in message
news:2389A0AE-3076-4A91-8BE4-A1BCC507C449@microsoft.com...
>I have a poject with a form which has a Command Button, Text Box, and Label
> control added to it at run time. It has taken me at least three hours
> (I'm
> extremely new at programming) to figure out how to add the three controls
> to
> my form at run time. Once I finally figured out how to do all that I
> realized I didn't know how to add code to make my command button do
> something.

You can't. Code must be written in the IDE at what's called design-time.

> What I want to do is after my form is shown on the screen, the
> label presents a question "How many text boxes do you want added to the
> screen?". The user will enter the number (1 - 10) in the text box and
> then
> hit ok. The code under that button should then remove the initial Label,
> Text Box and Command Button controls, and add the specified number of text
> boxes, and resize the screen according to the amount of text boxes added.

Use a control array. To create a control array, in the Properties window
provide a numeric value for the Index property. Normally, you use 0 but it
can really be anything. Then, at runtime you load new controls into the
control array using the Load statement and remove controls from the control
array using the Unload statement. Look up control arrays and the
Load/Unload statements in VB's help for more information.

--
Mike
Microsoft MVP Visual Basic



Re: Adding Code to a Command Button Added At Run Time by Sneha

Sneha
Wed Sep 20 10:50:52 CDT 2006


Kevin E. wrote:
> I have a poject with a form which has a Command Button, Text Box, and Label
> control added to it at run time. It has taken me at least three hours (I'm
> extremely new at programming) to figure out how to add the three controls to
> my form at run time. Once I finally figured out how to do all that I
> realized I didn't know how to add code to make my command button do
> something. What I want to do is after my form is shown on the screen, the
> label presents a question "How many text boxes do you want added to the
> screen?". The user will enter the number (1 - 10) in the text box and then
> hit ok. The code under that button should then remove the initial Label,
> Text Box and Command Button controls, and add the specified number of text
> boxes, and resize the screen according to the amount of text boxes added.
------------------------------------------------------------------------------
Hi Kevin

To provide you a useful solution some additional information from your
side is required.

Usually, people go for runtime-creation of controls, when, at
design-time they are not sure of the exact number of controls they
might need. Here, you need only one command button, One label and one
text box, that is You know in advance only these three are required,
then why you are going for runtime creation? Or am I missing some
point?

Also you said, these 3 controls are created at runtime. Do you mean a
blank form is loaded, then these 3 controls are added, next, the user
enters a number in text box and clicks the button, these 3 controls
vanish, and a number of textboxes are created?

What method you are using for the runtime creation of the 3 controls
and again the specified number of text boxes?

If you can describe what exactly you are trying to do, we will be in a
position to provide you a satisfactory solution.

Regards

Sneha
spiderangelo@gmail.com
---------------------------------------------------------------------------------


Re: Adding Code to a Command Button Added At Run Time by KevinE

KevinE
Wed Sep 20 11:52:01 CDT 2006

I am an engineer and i work with autodesk inventor, which utilizes the
Microsoft VBA, and i am trying to automate assembly creation with my project.
My idea entails a Form starting the project off, with a label which poses a
question, "How Many Parts In This Assembly?" a text box for the user to enter
a number, and a button to click when the user is ready to go on to the next
step. By clicking the buttion i was hoping to clear the current form and
replace the first three controls with the specified number of text boxes
(there are other text boxes, buttons, and labels that will also be added but
after i learn how to take care of my current issue the rest will go like
clock work). The addition of the text boxes is needed because the user/users
will need to browse our directory of parts to look for the needed
files, and the text boxes will hold the file path for said parts which my
project will use later so it knows what to insert, and constrain in the
assembly. Also the number of parts required to make the assembly is never
known until the assembly needs to be made so that is why the project poses
the question at runtime. I hope that helps.

Here is my code for what i stared doing:.


'this is the module level code from where i start the procedure (macro) in
Inventor
Sub TextBox_Insert()
frmTextInsert.Show
End Sub

under the form is my code in the frmTextInsert Initialize

Private Sub UserForm_Activate()
frmTextInsert.height = 125
frmTextInsert.Left = 500
frmTextInsert.Top = 100
frmTextInsert.Width = 270
Dim cntLabel As Control
Set cntLabel = frmTextInsert.Controls.Add("Forms.Label.1", lblQuestion,
Visible)
Dim cntTextBox As Control
Set cntTextBox = frmTextInsert.Controls.Add("Forms.TextBox.1", txtShellQty,
Visible)
Dim btnButton As Control
cntLabel.Caption = "How Many Shells Are In This Assembly?"
cntLabel.TextAlign = fmTextAlignCenter
cntLabel.height = 13
cntLabel.Left = 16
cntLabel.Top = 48
cntLabel.Width = 150
cntLabel.TabIndex = 1
cntTextBox.TextAlign = fmTextAlignLeft
cntTextBox.height = 17
cntTextBox.Left = 162
cntTextBox.Top = 45
cntTextBox.Width = 25
End Sub

So after i run the procedure i see the form in all of its glory, but my hang
up came when i wanted to click the "OK" button but i dont know how to add
code to the button so i can clear the form and add specified number of text
boxes.

If you still have issues with what i explained let me know, im more than
willing to explain myself.


kevin

"Sneha Menon" wrote:

>
> Kevin E. wrote:
> > I have a poject with a form which has a Command Button, Text Box, and Label
> > control added to it at run time. It has taken me at least three hours (I'm
> > extremely new at programming) to figure out how to add the three controls to
> > my form at run time. Once I finally figured out how to do all that I
> > realized I didn't know how to add code to make my command button do
> > something. What I want to do is after my form is shown on the screen, the
> > label presents a question "How many text boxes do you want added to the
> > screen?". The user will enter the number (1 - 10) in the text box and then
> > hit ok. The code under that button should then remove the initial Label,
> > Text Box and Command Button controls, and add the specified number of text
> > boxes, and resize the screen according to the amount of text boxes added.
> ------------------------------------------------------------------------------
> Hi Kevin
>
> To provide you a useful solution some additional information from your
> side is required.
>
> Usually, people go for runtime-creation of controls, when, at
> design-time they are not sure of the exact number of controls they
> might need. Here, you need only one command button, One label and one
> text box, that is You know in advance only these three are required,
> then why you are going for runtime creation? Or am I missing some
> point?
>
> Also you said, these 3 controls are created at runtime. Do you mean a
> blank form is loaded, then these 3 controls are added, next, the user
> enters a number in text box and clicks the button, these 3 controls
> vanish, and a number of textboxes are created?
>
> What method you are using for the runtime creation of the 3 controls
> and again the specified number of text boxes?
>
> If you can describe what exactly you are trying to do, we will be in a
> position to provide you a satisfactory solution.
>
> Regards
>
> Sneha
> spiderangelo@gmail.com
> ---------------------------------------------------------------------------------
>
>

Re: Adding Code to a Command Button Added At Run Time by Jim

Jim
Wed Sep 20 12:55:54 CDT 2006

"Kevin E." <KevinE@discussions.microsoft.com> wrote:
> I have a poject with a form which has a Command Button, Text Box,
> and Label control added to it at run time. It has taken me at least
> three hours (I'm extremely new at programming) to figure out how
> to add the three controls to my form at run time. Once I finally
> figured out how to do all that I realized I didn't know how to add
> code to make my command button do something.

"MikeD" <nobody@nowhere.edu> wrote:
> You can't. Code must be written in the IDE at what's called design-
> time.

Well, I think what Mike means to say is that it's possible to do things
during runtime but that's really the task at hand when writing a script
engine, and otherwise the Command1_Click() event gets filled in
during the design-time aspect.

For instance, when creating a scripting engine, which VB is quite capable
of, you can get an application to read a script to create the appropriate
controls and then there's the sub-classing details where you get a form
or control to provide more than the default events, properties and methods
provided by VB.

Take a look at the Command1_Click() event and put the code inside
that event to create your controls. You can create a subroutine to
create a certain number of controls and pass a parameter to that
subroutine (if so desired). That's where the .Index property Mike
suggested comes into play, along with the Load and Unload methods.
Or inside the IDE, double-click upon your Command button control
and the VB editor window will open the _Click() event for that control.
If you have more than one Command button and you're creating those
dynamically for whatever reason, Click() will appear with a parameter
as cmdButton_Click (Index As Integer). Other controls behave in the
same manner once you place a zero into the .Index field (in the tool's
property window).

One thing you can do, is to right click upon your command button,
then click upon Copy. Then click inside the form somewhere else
so that the form gets highlighted, then right click there and select
Paste. VB then asks you if you'd like to create an array for that
control. If you don't create an array, VB gives the control a new
name (Command2, if the first control's name was left as Command1).

If you elect to "create an array", VB places a 0 into the Index
property of the first control, keeps the same name and places a
1 into the Index property of the new control. It keeps most
of the same properties of the original control, like the .Width
and the .Height and the .Caption. The .Left and .Top properties
which position the control on the form tend to change.

Do it a few times with each control and you'll get the hang of
it pretty quick.

--
Jim Carlock
Post replies to the group.



Re: Adding Code to a Command Button Added At Run Time by Ken

Ken
Wed Sep 20 13:20:14 CDT 2006

"Kevin E." <KevinE@discussions.microsoft.com> wrote in message
news:99F5E122-3D54-4F6D-AAA2-5C37BBFBCAAE@microsoft.com...
>I am an engineer and i work with autodesk inventor, which utilizes the
> Microsoft VBA, and i am trying to automate assembly creation with my
> project.
> My idea entails a Form starting the project off, with a label which poses
> a
> question, "How Many Parts In This Assembly?" a text box for the user to
> enter
> a number, and a button to click when the user is ready to go on to the
> next
> step. By clicking the buttion i was hoping to clear the current form and

Since, if you need events, Controls.Add is useless for the most part, try
this... fresh off the presses (attempt at converting my
http://www.vbsight.com/CodeA.htm#AddControls2 project to VBA)

I've never done any VBA work but, this seems to do the trick... funny that I
can't seem to get the Enter/Exit events to fire correctly... hmmmm... dang
VBA! <g>
http://www.vbsight.com/zips/book1.zip

If you did want to use Controls.Add directly, you'd need to declare a module
scope variable 'WithEvents' for each new control you want to load...

Private WithEvents MyNewControl As SomeControlType

later...

Set MyNewControl = Controls.Add("whatever")

--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm



Re: Adding Code to a Command Button Added At Run Time by Sneha

Sneha
Wed Sep 20 17:11:23 CDT 2006


Kevin E. wrote:
> I am an engineer and i work with autodesk inventor, which utilizes the
> Microsoft VBA
----------------------------------------------------------------------
Sorry Kevin
VBA is alien to me

Try other responses
Best of luck

Sneha
-----------------------------------------------------------------------


Re: Adding Code to a Command Button Added At Run Time by DanS

DanS
Wed Sep 20 17:25:46 CDT 2006

=?Utf-8?B?S2V2aW4gRS4=?= <KevinE@discussions.microsoft.com> wrote in
news:99F5E122-3D54-4F6D-AAA2-5C37BBFBCAAE@microsoft.com:

> I am an engineer

STOP! There's the whole problem ! (Just kidding ! That's an ongoing joke
at the office.)

Is this VBA and not VB ? (a non-VBA user, so I'm not sure what applies
from VB, most of it I guess)......

Can I ask a questions ? What if after the initial design, and for some
unforseen reason, the list needs to be modified ? Another part or 2 need
to be added to the assembly ? How do you then edit the list ?

I know you want to use text boxes, or rather started off with using these
text boxes, and if that's what you want, by all means, go that way, with
a control array.

What I can visualize is a single form. In it's most basic form it
contains one (1) textbox, three (3) command buttons, and one (1) listbox.

The textbox is for manually entering the path/filename. One (1) button is
next to the textbox for the 'Browse...' function. One (1) button is
marked 'Add', and the third is 'Delete'. The listbox is used to display
those filenames.

Manually type a path/filename, click 'Add'. It goes in the list.
Clcik 'Browse...' and pick a file, click 'Add', it goes in the list.
Highlight a list entry, click 'Delete', it is removed from the list.

Of course there is other work to do for saving it, or whatever you need
to do. I haven't used Inventor at all, as SolidWorks/ProE has near
dominance over the solid modelling market, so I'm not really sure what
you need to do with the data.

Or go the textbox route. It's your UI.

I apologize if the above sounds rude, that is not what was intended. Have
you asked the other engineers what they would like to see ?

Regards,

DanS




> and i work with autodesk inventor, which utilizes the
> Microsoft VBA, and i am trying to automate assembly creation with my
> project.
> My idea entails a Form starting the project off, with a label which
> poses a
> question, "How Many Parts In This Assembly?" a text box for the user
> to enter a number, and a button to click when the user is ready to go
> on to the next step. By clicking the buttion i was hoping to clear
> the current form and replace the first three controls with the
> specified number of text boxes (there are other text boxes, buttons,
> and labels that will also be added but after i learn how to take care
> of my current issue the rest will go like clock work). The addition
> of the text boxes is needed because the user/users will need to browse
> our directory of parts to look for the needed files, and the text
> boxes will hold the file path for said parts which my project will use
> later so it knows what to insert, and constrain in the assembly. Also
> the number of parts required to make the assembly is never known until
> the assembly needs to be made so that is why the project poses the
> question at runtime. I hope that helps.
>
> Here is my code for what i stared doing:.
>
>
> 'this is the module level code from where i start the procedure
> (macro) in Inventor
> Sub TextBox_Insert()
> frmTextInsert.Show
> End Sub
>
> under the form is my code in the frmTextInsert Initialize
>
> Private Sub UserForm_Activate()
> frmTextInsert.height = 125
> frmTextInsert.Left = 500
> frmTextInsert.Top = 100
> frmTextInsert.Width = 270
> Dim cntLabel As Control
> Set cntLabel = frmTextInsert.Controls.Add("Forms.Label.1",
> lblQuestion, Visible)
> Dim cntTextBox As Control
> Set cntTextBox = frmTextInsert.Controls.Add("Forms.TextBox.1",
> txtShellQty, Visible)
> Dim btnButton As Control
> cntLabel.Caption = "How Many Shells Are In This Assembly?"
> cntLabel.TextAlign = fmTextAlignCenter
> cntLabel.height = 13
> cntLabel.Left = 16
> cntLabel.Top = 48
> cntLabel.Width = 150
> cntLabel.TabIndex = 1
> cntTextBox.TextAlign = fmTextAlignLeft
> cntTextBox.height = 17
> cntTextBox.Left = 162
> cntTextBox.Top = 45
> cntTextBox.Width = 25
> End Sub
>
> So after i run the procedure i see the form in all of its glory, but
> my hang up came when i wanted to click the "OK" button but i dont know
> how to add code to the button so i can clear the form and add
> specified number of text boxes.
>
> If you still have issues with what i explained let me know, im more
> than willing to explain myself.
>
>
> kevin
>
> "Sneha Menon" wrote:
>
>>
>> Kevin E. wrote:
>> > I have a poject with a form which has a Command Button, Text Box,
>> > and Label control added to it at run time. It has taken me at
>> > least three hours (I'm extremely new at programming) to figure out
>> > how to add the three controls to my form at run time. Once I
>> > finally figured out how to do all that I realized I didn't know how
>> > to add code to make my command button do something. What I want to
>> > do is after my form is shown on the screen, the label presents a
>> > question "How many text boxes do you want added to the screen?".
>> > The user will enter the number (1 - 10) in the text box and then
>> > hit ok. The code under that button should then remove the initial
>> > Label, Text Box and Command Button controls, and add the specified
>> > number of text boxes, and resize the screen according to the amount
>> > of text boxes added.
>> ----------------------------------------------------------------------
>> -------- Hi Kevin
>>
>> To provide you a useful solution some additional information from
>> your side is required.
>>
>> Usually, people go for runtime-creation of controls, when, at
>> design-time they are not sure of the exact number of controls they
>> might need. Here, you need only one command button, One label and one
>> text box, that is You know in advance only these three are required,
>> then why you are going for runtime creation? Or am I missing some
>> point?
>>
>> Also you said, these 3 controls are created at runtime. Do you mean a
>> blank form is loaded, then these 3 controls are added, next, the user
>> enters a number in text box and clicks the button, these 3 controls
>> vanish, and a number of textboxes are created?
>>
>> What method you are using for the runtime creation of the 3 controls
>> and again the specified number of text boxes?
>>
>> If you can describe what exactly you are trying to do, we will be in
>> a position to provide you a satisfactory solution.
>>
>> Regards
>>
>> Sneha
>> spiderangelo@gmail.com
>> ----------------------------------------------------------------------
>> -----------
>>
>>


Re: Adding Code to a Command Button Added At Run Time by Ken

Ken
Thu Sep 21 09:12:48 CDT 2006

"DanS" <t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote in message
news:Xns9844BB87E591Fidispcom@216.196.97.142...
>
> I know you want to use text boxes, or rather started off with using these
> text boxes, and if that's what you want, by all means, go that way, with
> a control array.

fwiw, I couldn't find a "straight forward" way to create a control array in
VBA... but, like I said, I don't use VBA so I may've missed something
obvious. It does expose a Load method though... so, there must be "some" way
to do it. The sample I posted doesn't require a control array to enable
event trapping for an unknown number of controls. (still need to know the
types)

> Or go the textbox route. It's your UI.
>
> I apologize if the above sounds rude, that is not what was intended. Have

See how you are? Geezzz ;-)


--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm



Re: Adding Code to a Command Button Added At Run Time by DanS

DanS
Thu Sep 21 10:36:42 CDT 2006

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in
news:uy3bZhY3GHA.4764@TK2MSFTNGP05.phx.gbl:

> "DanS" <t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote in message
> news:Xns9844BB87E591Fidispcom@216.196.97.142...
>>
>> I know you want to use text boxes, or rather started off with using
>> these text boxes, and if that's what you want, by all means, go that
>> way, with a control array.
>
> fwiw, I couldn't find a "straight forward" way to create a control
> array in VBA... but, like I said, I don't use VBA so I may've missed
> something obvious. It does expose a Load method though... so, there
> must be "some" way to do it. The sample I posted doesn't require a
> control array to enable event trapping for an unknown number of
> controls. (still need to know the types)
>
>> Or go the textbox route. It's your UI.
>>
>> I apologize if the above sounds rude, that is not what was intended.
>> Have
>
> See how you are? Geezzz ;-)
>

You are right Ken, there SEEMS to be no _simple_ way to handle control
arrays in VBA (I don't use VBA ever either). I just opened Word and added a
UserForm with a text box. Since you (apparently) can't do control array's,
the OP must figure out some other way of doing what they want.


Re: Adding Code to a Command Button Added At Run Time by Ken

Ken
Thu Sep 21 10:45:01 CDT 2006

"DanS" <t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote in message
news:Xns98457623D50Bidispcom@216.196.97.142...
>>
>>> Or go the textbox route. It's your UI.
>>>
>>> I apologize if the above sounds rude, that is not what was intended.
>>> Have
>>
>> See how you are? Geezzz ;-)
>>
>
> You are right Ken, there SEEMS to be no _simple_ way to handle control
> arrays in VBA (I don't use VBA ever either). I just opened Word and added
> a
> UserForm with a text box. Since you (apparently) can't do control array's,
> the OP must figure out some other way of doing what they want.

Yeah... one way is to try the sample I posted ;-) As far as I can tell,
it'll work for what he's trying to do. It uses polymorphism (I knew I'd
eventually use that word in a sentence ;-) on controls that have been added
using Controls.Add.

--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm



Re: Adding Code to a Command Button Added At Run Time by Jim

Jim
Thu Sep 21 14:08:19 CDT 2006

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote:
> As far as I can tell, it'll work for what he's trying to do. It uses
> polymorphism (I knew I'd eventually use that word in a sentence ;-)
> on controls that have been added using Controls.Add.

It's been awhile since I've messed with VBA as well.

Creating controls dynamically in VBA presents some problems.
There's no Index property on the controls and Microsoft prevented
the standard VB mechanisms from working. I tried:

Dim MyNewTextBoxes() As TextBox
ReDim MyNewTextBoxes(10)
For i = 1 to 10
Set MyNewTextBoxes(i) = New TextBox
Next i

The VBA IDE compiled it fine. However, starting the form up in
Access presents a "Run-time error '429'", "ActiveX component
can't create object".

I ended up googling through the newsgroups to look for help
and came up with the following:
http://j-walk.com/ss/excel/tips/tip76.htm

Some of the MSForms.<controltype> items there convert
to Access.<controltype> pretty easily, take for instance,
a Form...

Dim NewOptionButton As Msforms.OptionButton

converts to:

Dim NewOptionButton As Access.OptionButton

But I ran into some other problems involving VBComponents.
Those seem to be a part of the Visual Basic for Applications
Extensibility... whew that's a long one.

The link above mentions that the VBA Extensibility Library does
NOT need to be referenced but... and that perhaps only applies
to Excel. :-/

Some folks in the Excel newsgroup advise taking a gander at
the following page, http://www.cpearson.com/excel/vbe.htm. If
the OP is using Excel, the VBA questions should be directed to
the following group:

microsoft.public.excel.programming

I've gotten some help there in the past with a variety of
things when messing with forms and such inside of Excel.

I messed with the VB environment for Access and I feel so
trapped in that environment. :-)

I like the Excel VBA environment a little better than the
Access VBA environment, but that's not saying much. It
serves a purpose occasionally whereas the Access macros
served a purpose when I was first working through some
things.

--
Jim Carlock
Post replies to the group.