none
Creataing Form Objects RRS feed

  • Question

  • Hello all

    Is it possible to create form objects during run time from VBA code or a Macro?  I have a form that could potentially get quite a few text and image boxes and if I create every possible object during programming the form gets ungodly large and takes forever to load.


    Thanks a bunch as always Norm

    Monday, November 18, 2019 3:45 AM

All replies

  • Is it possible to create form objects during run time from VBA code or a Macro?

    Hi Norm,

    I have not yet the complete picture of what you want, but you could make some template forms, with already all necessary - hidden - controls.

    When opening the form(s) you can move such control to the right place, adjust its dimensions, bind it to the corresponding field, unhide it, etc.

    Imb.

    Monday, November 18, 2019 10:30 AM
  • Its possible but it works like this :

    You open the form in Design view HIDDEN

    You create the objects

    You close the form

    You reopen it...

    This requires (i have to check on it  don't remember) that your application is .accdb

    Check this for start :https://social.msdn.microsoft.com/Forums/office/en-US/49106193-9f13-4f4d-9725-34a07140d722/add-control-to-form-at-runtime-access-vba?forum=accessdev

    I have used it in the past in an extremely dynamic form...you can also add events to the controls and so on.

    Monday, November 18, 2019 10:48 AM
  • I have used it in the past in an extremely dynamic form...you can also add events to the controls and so on.

    Hi John,

    Very interesting!

    Can you enlarge a little more on this subject?

    Imb.

    Monday, November 18, 2019 1:03 PM
  • I tried this code in a test environment and received the error message that I could not add, rename or delete the control I requested.  I tried to do it directly from an open form where as I see that the code in the example required a save and a reopen - I was hoping for adding the control while in run mode and the form staying open.  Interesting though

    Thanks a bunch as always Norm

    Monday, November 18, 2019 3:13 PM
  • This is exactly what I had done.  What is it they say about great minds?... Or, is it fools never differ?  I get confused sometimes :-)

    In my dreams last night I realized I don't need to protect for million to one occurrences directly (especially 20 times on one form in a single test).  You can see that amounts to a lot of controls. 

    I can simply reserve the image and text boxes that will occur every time and have 30 or so others hidden in the background that I can call to action and place where needed as the case may or may not be.

     

    Thanks a bunch as always Norm

    Monday, November 18, 2019 3:23 PM
  • Also keep in mind a form only allows a certain number of controls to be created over it's lifetime, so if this was something you were thinking of doing at runtime, you will most probably hit this limit rather rapidly.

    Daniel Pineault, 2010-2019 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net

    Monday, November 18, 2019 3:29 PM
  • This is exactly what I had done.  What is it they say about great minds?... Or, is it fools never differ?  I get confused sometimes :-)

    Hi Norm,

    I lost track … What had you exactly done?

    Imb.

    Monday, November 18, 2019 4:44 PM
  • Sorry lmb,  I thought I was replying directly to you.  I had all the necessary controls on my form hidden and then sized, positioned, populated and displayed as needed.  Lots of controls (too many).  What I am doing right now is having only the specific controls that I know will get populated put on the form positioned and ready to use.  Then hidden in the background Ill have some generic controls that I will call into action and place where ever and as needed to cover the low probability cases.  

    Thanks a bunch as always Norm

    • Proposed as answer by Imb-hb Monday, November 18, 2019 5:36 PM
    Monday, November 18, 2019 5:26 PM
  • Then hidden in the background Ill have some generic controls that I will call into action and place where ever and as needed to cover the low probability cases.  

    Hi Norm,

    Ok, I understand now.

    If you give the generic control systematic names, you can use them for almost anything. With this way of working I make hundreds of completely different "forms", based on one template form and some standard routines.

    But now back to your original question. You have already the form with the hidden controls, and it is not adviced to add controls again and again, what next?

    Imb.

    Monday, November 18, 2019 5:50 PM
  • The original question was could I add controls during runtime.  I mean why have so many controls that in all probability will not ever get used when if possible create them as I need them.  You are the second 'very helpful' person that advised that even if possible don't do it.  One of my mantras - just because you can do it doesn't mean you should do it.

    There is no what's next.  I will go from 280 specific controls down to 15 specific and 30 generics that I can use as needed (or not).


    Thanks a bunch as always Norm

    Monday, November 18, 2019 6:04 PM
  • I will go from 280 specific controls down to 15 specific and 30 generics that I can use as needed (or not).

    Hi Norm,

    An additional way to enhance the number of "available" controls is to place these controls in a subform, and use many of these same subforms on the main form.

    It takes some administration to know distinguish between the different subforms …

    Imb.

    Monday, November 18, 2019 6:34 PM