Adding controls

Eichenbaum

New member
Local time
Today, 18:39
Joined
Jul 22, 2011
Messages
8
Hello,

I am using Access 2010 and I'm trying to create some labels dynamically:

Dim myLabel as Label
Set myLabel = frmMyForm.Controls.Add ("Forms.Label.1", "lblPrompt")

But Access does not recognize the property 'Add'. Do I need to add some library?

Thank you in anticipation. :-)
 
Hi. Welcome to the forum. Check out the CreateControl method of the Access.Application object.
Cheers,
Mark
 
Like Lagbolt says, CreateControl is good.

You need to open the form in Edit mode though. Something like this:

Code:
Dim ctrl As Control

DoCmd.OpenForm "frmMyForm", acDesign

Set ctrl = CreateControl("frmMyForm", acLabel, , , "My Label", 200, 200, 1000, 300)
ctrl.Name = "label1"    'if you want to give it a custom name

DoCmd.Close acForm, "frmMyForm", acSaveYes

Why are you doing this out of curiosity?

Chris
 
Creating Controls dynamically is generally a very bad idea. It's a cumbersome process, requiring that the Form be opened in Design Mode (Chris misspoke when he said Edit, his code shows that he meant Design). Since presumably you want to do this to an open Form, you'd have to
  1. Close the Form
  2. Open the Form, as per Chris' code, in Design View
  3. Create your Labels
  4. Close the Form
  5. Open the Form again in one of the Form Views
This aside, a Form has a Lifetime limit of 754 Controls, meaning that even if you later deleted the dynamically created Controls, they'd still count towards this limit, and sooner or later you'd reach this limit and the Form would bomb out.

This sort of thing is usually handled by creating the Controls at Design Time, then controlling their Visibility thru code.

Linq ;0)>
 
Unfortunately with the Ms Access can not be added object in the runtime :(
 
First of all, thank you everybody for the answers.

As missinglinq mentioned, I think that it will be a very bad idea.

My intention was to create a form where the users can see the values of a table. But it will not work properly creating objects in running time.

I think that the better solution is to create a listbox with all records.

The front end (accde) does not have any linked table and I'm getting the values of the back end through a DAO recordset and using the addItem to populate the listbox. I tried to avoid this option because somebody said to me that there's a data limit when you use this method. Then I thought that if I show the records as labels in a form was the better solution.

Thanks again for all tips!
 
(Chris misspoke when he said Edit, his code shows that he meant Design).
Yes, my bad. Thanks.

Since presumably you want to do this to an open Form, you'd have to
  1. Close the Form
  2. Open the Form, as per Chris' code, in Design View
  3. Create your Labels
  4. Close the Form
  5. Open the Form again in one of the Form Views
This aside, a Form has a Lifetime limit of 754 Controls, meaning that even if you later deleted the dynamically created Controls, they'd still count towards this limit, and sooner or later you'd reach this limit and the Form would bomb out.

This sort of thing is usually handled by creating the Controls at Design Time, then controlling their Visibility thru code.

Linq ;0)>
What about using text boxes and putting say 10 text boxes on the form to start with. Then just hide unused text boxes?

Text boxes could also be used as labels.

Chris
 
Hi Stopher,

you gave me a good idea!

Thank you.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom