Dynamically add control to MSAcess Form.

G

Guest

Hello, I am trying to build a form based off of a table. I have tried a
couple of different methods for adding a control at runtime and none have
work so far. I have tried:

Dim ctr as TextBox

Set ctr = Me.Controls.Add ("Form.Textbox.1","txtTest",TRUE)

Even though I've read that this should work, when ran I get a compire error
that the ".Add" method or data member is not found.

I've also tried:

Dim ctr as TextBox

Set ctl = CreateControl("frmTimeEntry", acTextBox, acDetail, , , 10, 10, 50,
10)

But get the error message: "You must be in Design view to create or delete
controls."

Any help that can be offered would be greatly appreciated.
 
M

Marshall Barton

molemoore said:
Hello, I am trying to build a form based off of a table. I have tried a
couple of different methods for adding a control at runtime and none have
work so far. I have tried:

Dim ctr as TextBox

Set ctr = Me.Controls.Add ("Form.Textbox.1","txtTest",TRUE)

Even though I've read that this should work, when ran I get a compire error
that the ".Add" method or data member is not found.

I've also tried:

Dim ctr as TextBox

Set ctl = CreateControl("frmTimeEntry", acTextBox, acDetail, , , 10, 10, 50,
10)

But get the error message: "You must be in Design view to create or delete
controls."


Right. Those messages mean what they say.

Actually, you should not try to modify the design of a form
at runtime. Instead, create as many controls as you might
need at design time and make them visible as needed at
runtime. If needed you can set their properties (e.g. Left,
Top, etc) when you make them visible.
 
G

Guest

I've considered that option. The problem with it is that I don't know in
advance how many controls I'll need. The number will be driven by how many
records there are in a particular table. I was hoping to be able to read in
the number of records from this table and then put the appropriate number of
controls on the form. To me, this is the cleanest way. The other option that
I'm considering is to use a subform. I don't like this idea as it gives up
some control, and allows the user to directly interact with the data in the
table.

So, I guess my question has become: Is it possible to add controls to a
MSAccess form at run time? If not, are there any suggestions other than using
a subform?

Thanks for the help, I'm still fairly new to programming, and even newer to
VBA.
 
M

Marshall Barton

I think you are barking up the wrong tree. It seems like
your question should be how to make a subform readonly.
This is easily done by setting the subform's AllowEdits and
AllowDeletions properties to No.

To follow up on your original question, No, you can not add
controls at runtime. Besides, it is easier and cleaner to
precreate a lot of invisible controls at design time and
just manipulate their properties than it is to create them.
 
G

Guest

Unfortunatly, simply setting the subforms readonly property will not allow me
the level of control that I need. I'm going to work with pre-populating the
form with controls and hiding/unhiding them as needed.

Thanks!

Marshall Barton said:
I think you are barking up the wrong tree. It seems like
your question should be how to make a subform readonly.
This is easily done by setting the subform's AllowEdits and
AllowDeletions properties to No.

To follow up on your original question, No, you can not add
controls at runtime. Besides, it is easier and cleaner to
precreate a lot of invisible controls at design time and
just manipulate their properties than it is to create them.
--
Marsh
MVP [MS Access]

I've considered that option. The problem with it is that I don't know in
advance how many controls I'll need. The number will be driven by how many
records there are in a particular table. I was hoping to be able to read in
the number of records from this table and then put the appropriate number of
controls on the form. To me, this is the cleanest way. The other option that
I'm considering is to use a subform. I don't like this idea as it gives up
some control, and allows the user to directly interact with the data in the
table.

So, I guess my question has become: Is it possible to add controls to a
MSAccess form at run time? If not, are there any suggestions other than using
a subform?

Thanks for the help, I'm still fairly new to programming, and even newer to
VBA.
 
M

Marshall Barton

I really don't want to beat this issue into the ground, but
you have yet to explain what effect you are trying to
achieve. If you need to lock specific controls instead of
the entire subform, that's easily done too.

Subforms are far and away the standard way to present data
records in a many side table. If you need to display
multiple records, set the subform's DefaultView to
Continuous.
 
G

Guest

Molemoore,
You caan create controls with the createcontrol function.
Here are the steps I use for this purpose:
1. Ensure that the subform is not opened with the main form.
(me.subform.SourceObject="")

2. Open the subform in design view:
DoCmd.OpenForm msub_name, acDesign, , , acFormEdit, acHidden

3. Delete unnecessary controls from the subform
Call DeleteControl(msub_name, mcont.name)

4. Use the createcontrol function to add the necessary controls

5. Save the subform
DoCmd.Close acForm, msub_name, acSaveYes

6. In the main form, restore the sourceobject property of the subform

Best regrards, Gábor

You only need to open the form in design view with the following code:


Marshall Barton said:
I really don't want to beat this issue into the ground, but
you have yet to explain what effect you are trying to
achieve. If you need to lock specific controls instead of
the entire subform, that's easily done too.

Subforms are far and away the standard way to present data
records in a many side table. If you need to display
multiple records, set the subform's DefaultView to
Continuous.
--
Marsh
MVP [MS Access]

Unfortunatly, simply setting the subforms readonly property will not allow me
the level of control that I need. I'm going to work with pre-populating the
form with controls and hiding/unhiding them as needed.
 

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