how to use vbe to add control to form?

how to use vbe to add control to form?

Anonymous
Not applicable
301 Views
3 Replies
Message 1 of 4

how to use vbe to add control to form?

Anonymous
Not applicable
Hi,
I'm trying to add a control to a form programatically which I've got a hint
from the help that that might be possible. I've never heard of it before
and the help seems really vague to me about the vbe methods.
I've tried variations of the following:

Sub testput()
Dim sFormName As String
Dim sTbName As String
Dim dVertPos As Double
Dim dHorizPos As Double
Dim dWidth As Double
Dim dHeight As Double

sFormName = "UserForm2"
sTbName = "TextBoxTest"
dVertPos = 45
dHorizPos = 45
dWidth = 45
dHeight = 15


PutTextBoxOnForm sFormName, sTbName, dVertPos, dHorizPos, dWidth, dHeight

End Sub

Sub PutTextBoxOnForm(sFormName As String, sTbName As String, dVertPos As
Double, dHorizPos As Double, dWidth As Double, dHeight As Double)
Dim ctlTbox1 As TextBox

'if I have a form named "UserForm2" that I want to put the control on
Dim frmTest As UserForm2
Set frmTest = New UserForm2
'this doesn't work
frmtest.Controls.Add textbox
'nor this
Set ctlTbox1 =
Application.VBE.SelectVBComponent.Designer.Controls.Add(vbext_ct_TextBox)

Set ctlTbox1 = Nothing
End Sub

I'm really just flailing away in the dark here, couldn't figure out from the
help files what I need to do.

Has anyone got this down ????
--



Mark@atreng.com
A.T. Renczarski and Co., inc.
p 816.587.0101
f 816.587.5691
0 Likes
302 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
Mark,

You don't need to declare an instance of UserForm2, just work with the form
directly. If you are in the form code you can refer to the form as "me" as
follows

'' get rid of these lines
''frm frmTest As UserForm2
''Set frmTest = New UserForm2
me.Controls.Add textbox

I haven't actually tried this so I don't know if it will work. It seems like
a lot of effort - what are you trying to accomplish? If you are trying to
create a control that just appears under certain conditions it's easier to
add the control to the form in the IDE then set it's visible property to
false. When you need it, just set visible to true.

Glen
0 Likes
Message 3 of 4

Anonymous
Not applicable
Hi Glen,

Glen Albert wrote in message
news:A9A0BEA4A75B56B599803C05FCB5D494@in.WebX.maYIadrTaRb...
> Mark,
>
> You don't need to declare an instance of UserForm2, just work with the
form


I just started my first dialog box and found it tedious to add contols via
the toolbox and mouse, move them around, align them, size them, name them
through the properties box, all very timeconsuming to me cause I'm not used
to it i guess. I thought it would be easier for me to design my form and
then just place controls at specific locations and sizes, name them, assign
properties, etc through a sub or function that reads a list of control names
to be created.

>If you are trying to
> create a control that just appears under certain conditions it's easier to
> add the control to the form in the IDE then set it's visible property to
> false. When you need it, just set visible to true.

Yes, I'm actually doing that with some of my controls, this other idea was
just for the initial design of the form.
I'll give your idea a try. But the help on vbe makes me think that's
exactly what it's supposed to be for, I just cant find how exactly the
syntax has to be. I can get some things from it like counts and names and
objects but haven't figured how to create the objects.

Thanks,
Mark
0 Likes
Message 4 of 4

Anonymous
Not applicable
Mark,
Prior to VB6, the only way to add a control dynamically to a form at
runtime
was to create a control array by creating the first one and giving it an
index, then
adding more from there. The process was never very impressive, but it did
work.

I have read that with VB6, it could be done without a control array, but
I have not tried it.
However, the beauty of VB is that you don't have to, in most cases. If you
are having trouble
placing or aligning controls, you might find it easier to reduce the size of
your grid to around
30 by 30 (vb) or 3 pts x 3pts (vba) and turning of the grid display.

See attached screencopy.

Brian D.



"MP" wrote in message
news:9138572F324CCDF76D8D04A13D0FE19F@in.WebX.maYIadrTaRb...
> Hi Glen,
>
> Glen Albert wrote in message
> news:A9A0BEA4A75B56B599803C05FCB5D494@in.WebX.maYIadrTaRb...
> > Mark,
> >
> > You don't need to declare an instance of UserForm2, just work with the
> form
>
>
> I just started my first dialog box and found it tedious to add contols via
> the toolbox and mouse, move them around, align them, size them, name them
> through the properties box, all very timeconsuming to me cause I'm not
used
> to it i guess. I thought it would be easier for me to design my form and
> then just place controls at specific locations and sizes, name them,
assign
> properties, etc through a sub or function that reads a list of control
names
> to be created.
>
> >If you are trying to
> > create a control that just appears under certain conditions it's easier
to
> > add the control to the form in the IDE then set it's visible property
to
> > false. When you need it, just set visible to true.
>
> Yes, I'm actually doing that with some of my controls, this other idea was
> just for the initial design of the form.
> I'll give your idea a try. But the help on vbe makes me think that's
> exactly what it's supposed to be for, I just cant find how exactly the
> syntax has to be. I can get some things from it like counts and names and
> objects but haven't figured how to create the objects.
>
> Thanks,
> Mark
>
>
0 Likes