Cannot Add Subform on at Runtime (1 Viewer)

cosmarchy

Registered User.
Local time
Today, 16:43
Joined
Jan 19, 2010
Messages
116
I'm trying to add controls to a form at runtime as I will need to make some small dynamic changes to the form once loaded so I'm starting with trying to add a subform before moving on to other controls.

So far, I have this short sub:
Code:
Private Sub Form_Open(Cancel As Integer)

    Dim ctlSub1 As SubForm
        
    Call DoCmd.RunCommand(acCmdLayoutView)
    
    Set ctlSub1 = CreateControl(Forms("compa").Name, acSubform, , Forms("subform").Name, , 20, 20, 50, 100)

    Call DoCmd.RunCommand(acCmdFormView)

End Sub

When I open the host form, I get the following error:

Run-time error '29054'
Microsoft Access can't add, rename, or delete the control(s) you requested

I'm not sure why it cannot add the form - it exists as does the subform.

Has anyone come across this before?

Thanks
 

theDBguy

I’m here to help
Staff member
Local time
Today, 16:43
Joined
Oct 29, 2018
Messages
21,358
Hi. When using the Runtime environment, design changes are not allowed/possible. I am guessing CreateControl is considered to be making a design change; therefore, it is being blocked.

One workaround, depending on your situation, is to add all the controls you think you will need on a form and simply show and hide them or change their properties at run time.

PS. I was assuming you were using the above code with the Access Runtime Version. If so, does your code run fine using the full version of Access?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 19:43
Joined
May 21, 2018
Messages
8,463
I'm trying to add controls to a form at runtime as I will need to make some small dynamic changes to the form once loaded so I'm starting with trying to add a subform before moving on to other controls
Creating objects at runtime is a horrible idea and a pretty much guaranteed way to corrupt a database. Also cannot be done in a runtime version or accde. IMO if you have to create objects at runtime your database or UI is likely poorly designed. For what reason do you need dynamic controls?
You can show, hide, move, format controls though.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:43
Joined
May 7, 2009
Messages
19,169
I'm not sure why it cannot add the form - it exists as does the subform.
i think, you just cannot.
add the subform in Design view and just change it's SourceObject.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 18:43
Joined
Feb 28, 2001
Messages
27,001
I'm joining in with the others. Whether this is .ACCDR, .ACCDE, or .ACCDB, it is a horrible idea. Among other things, you will run into a limit after a while because there is a limit of how many controls you can put in a section, something on the order of just under 770. However, if you can predict all of the controls, you just create them ALL and selectively disable them, make them invisible, and lock them. Then when you need to do something with a special control, just find it and enable it, unhide it,and unlock it.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 19:43
Joined
Feb 19, 2002
Messages
42,981
Piling on.
Additionally, users should be using a copy of a common SHARED FE database. Therefore, UserA changing a form affects only HIS version of the application. If the users all open the same copy of the FE, then it is even worse because the change affects ALL other users if they are all sharing the same FE which is yet another terrible idea and will lead to corruption issues.

YOU are the developer. Only YOU can make design changes PERIOD. Any other scenario is the road to perdition.
 

Users who are viewing this thread

Top Bottom