add a textbox at run time

J

johnk

I am tring to create a textbox at runtime, with this code taken from
the help file in Acess 2000.
Unfortunately creating the text boxs at design time has run into
problems.
And this code gives a type mismatch error. I don't know why. Help?

Private Sub AddDevice_Click()

Dim txtB As Control

Set txtB = CreateControl(Forms!frm_Device, acTextBox, acDetail, "100",
"100", "100", "100")
' Set ctlText = CreateControl(frm.Name, acTextBox, , "", "", intDataX,
intDataY)

End Sub
 
D

Duane Hookom

First, most of us experienced developers would use hidden text boxes and
make them visible as needed. This is much preferable to CreateControl.

Otherwise, I think the values "100",... need to lose the quotes.
 
J

johnk

I lost the quotes and still get a type mismatch error.
I would prefer to use hidden textboxes but there is simply no way of
knowing how many to create at design time. That really why this all
started. Its that occasional instanst that always seems arrives just
in time to mess everything up.
 
D

Duane Hookom

You must change your reference to the form object to a form name and add
more arguements. This worked for me:

Set txtB = CreateControl(Forms!frm_Device.Name, _
acTextBox, acDetail, "", "", 100, 100, 100, 100)
 
A

asc4john

I can now create a new form with new textboxes, but I still can not
create a text box on the existing form. This,
Dim txtB as Control
Set txtB = CreateControl(Forms!frm_Device.Name, acTextBox,
acDetail, "", "", _
1000, 1000, 1000, 1000)
gives the error: "You must be in design view to create or delete
controls"
On this form, frm_Device, I can create a completely new form with
dozens of controls, but can not create controls on the form frm_Device,
where I want it. Can this actually be done? Does it have to be in a
seperate module? Or can I create a fake Form fake_frm_Device that would
make the "real" Form frm_Device?
 
R

Rick Brandt

asc4john said:
I can now create a new form with new textboxes, but I still can not
create a text box on the existing form. This,
Dim txtB as Control
Set txtB = CreateControl(Forms!frm_Device.Name, acTextBox,
acDetail, "", "", _
1000, 1000, 1000, 1000)
gives the error: "You must be in design view to create or delete
controls"
On this form, frm_Device, I can create a completely new form with
dozens of controls, but can not create controls on the form
frm_Device, where I want it. Can this actually be done? Does it have
to be in a seperate module? Or can I create a fake Form
fake_frm_Device that would make the "real" Form frm_Device?

As stated in the error CreateControl only works in design view.

Access forms are not well suited to control creation on the fly because they
have a lifetime limit of around 700 controls and you don't get those allotments
back when a control is deleted. Even if you got your code to work you would
eventually hit this ceiling and then it would fail again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top