Adding Controls at Runtime

NeroMaj

Registered User.
Local time
Today, 08:31
Joined
Sep 13, 2010
Messages
34
Here is my problem...

I have a form (frmWelcome), which contains a subform (frmNavigation).

My goal is to read a bunch of values from a record source into an array. Then I want to list each of these values in the subform, but list each one in a separate label so that it can be hyperlinked to the recordset it came from.

Question: I am getting the following error message when trying to programmatically add labels to the subform.

"The Microsoft Jet database engine does not recognize 'Text 10' as a valid field name or expression"

Here is what this part of my code looks like:

*In the form open
DoCmd.OpenForm "frmNavigation", acDesign
LayoutForm
*

*
Private Sub LayoutForm()
Dim lbl As Control
set lbl = CreateControl(Form_frmNavigation.Name, acTextBox, acDetail) *This line gives me the error
End Sub
*

I know that this isn't set up to add a different label for each one, but i will add the loop in after i figure out how to add one.

Any ideas on how to get rid of this error/approach it from a different way.
 
When you use 'Form_frmNavigation' Access tries to instantiate that object, but that's the object you are designing.
Try ...
Code:
set lbl = CreateControl("frmNavigation", acTextBox, acDetail)
 
I guess it should also be pointed out that:
1. In an MDB file you won’t be able to create much more than 750 labels during the life of the Form.
2. In an MDE file you won’t be able to do it at all.
 
NeroMaj, did you ever get a working solution for this? I have exactly the same problem, and I get the same error. I'm trying to create rectangles on a form at run-time, so the rectangles are drawn as required. When the CreateControl method runs I get the error as you do. Yes, the form is placed in design mode at the time of the error.
Thanks,
Stewart
 
I would suggest that you look at using an existing control on your form instead of tryiing to create controls on the fly. As pointed out by ChrisO there will be a point in the future where your process will not longer work be cause you will have exceeded the total number of controls that can be created on any one single form. This total count includes all controls that were added and then were deleted.

You can set all of the properties of a control without having to be concerned about the limitation when creating controls. You can move controls by changing the properties that locate it on the form. You can change the size by changing the properties that control these aspects of the control.

In short, you can add the controls you need in design mode and then mange their placement, size and content as needed, including hidding or diaplying them.
 
Thanks Mr B. Sounds like a plan. I'll see how that works later today.
Thanks.
 
I have never needed to add controls to a form at run time, and I prefer not even to make controls visible/not visible. I do gray controls out, though.

I think it is important not to change the layout that the user sees, unless it is really necessary.


so to take the OP's intial question. Why read the recordsource into an array, and then try and display it? Why not simply design a form (subform) based on the record source, and show that.
 

Users who are viewing this thread

Back
Top Bottom