none
add control to form at runtime (access VBA)

    Question

  • How can I programatically add a textbox to a form at runtime? (I'd like to do this "remotely", i.e. from outside the form itself. For instance, right after the database starts, and before the form is opened). 

    Many thanks in advance.


    DragonForest

    Saturday, June 02, 2012 9:14 AM

Answers

  • To add a control, you need to open the form in design view. This will lock the database exclusively, so if the database is used by multiple users, you MUST have a split frontend/backend design where each user has an individual copy of the frontend.

    Sub ModifyForm()
        Const strForm = "frmTest"
        Const strCtl = "txtTest"
        Dim frm As Form
        Dim ctl As Control
        DoCmd.OpenForm FormName:=strForm, View:=acDesign
        Set frm = Forms(strForm)
        Set ctl = CreateControl(FormName:=strForm, ControlType:=acTextBox, _
            Left:=1440, Top:=2160, Width:=2880, Height:=288)
        ctl.Name = strCtl
    ' Switch to form view
        RunCommand acCmdFormView
        Forms(strForm).Controls(strCtl).Value = "Hello World"
    ' Or save the form, close and reopen it
        'DoCmd.Close ObjectType:=acForm, ObjectName:=strForm, Save:=acSaveYes
        'DoCmd.OpenForm FormName:=strForm, View:=acNormal
        'Forms(strForm).Controls(strCtl).Value = "Hello World"
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by DragonForest Monday, June 04, 2012 1:45 AM
    Saturday, June 02, 2012 9:59 AM

All replies

  • To add a control, you need to open the form in design view. This will lock the database exclusively, so if the database is used by multiple users, you MUST have a split frontend/backend design where each user has an individual copy of the frontend.

    Sub ModifyForm()
        Const strForm = "frmTest"
        Const strCtl = "txtTest"
        Dim frm As Form
        Dim ctl As Control
        DoCmd.OpenForm FormName:=strForm, View:=acDesign
        Set frm = Forms(strForm)
        Set ctl = CreateControl(FormName:=strForm, ControlType:=acTextBox, _
            Left:=1440, Top:=2160, Width:=2880, Height:=288)
        ctl.Name = strCtl
    ' Switch to form view
        RunCommand acCmdFormView
        Forms(strForm).Controls(strCtl).Value = "Hello World"
    ' Or save the form, close and reopen it
        'DoCmd.Close ObjectType:=acForm, ObjectName:=strForm, Save:=acSaveYes
        'DoCmd.OpenForm FormName:=strForm, View:=acNormal
        'Forms(strForm).Controls(strCtl).Value = "Hello World"
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by DragonForest Monday, June 04, 2012 1:45 AM
    Saturday, June 02, 2012 9:59 AM
  • How can I programatically add a textbox to a form at runtime? (I'd like to do this "remotely", i.e. from outside the form itself. For instance, right after the database starts, and before the form is opened). 


    Do you really need to add a text box to the form, or could you just have a text box, created at design time, that is invisible until it is made visible at run time?  You could do that without needing to lock the database exclusively or open the form in design view at run time.

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Saturday, June 02, 2012 11:53 PM
  • In addition to Dirk's comment, there is also a limit on the number of controls that can be added to a form over it's lifetime. So, it you do it on a regular basis, you'll run into that limit. Additionally, if you are distributing a mde/accde, you won't be able to do it at runtime, at all.
    Sunday, June 03, 2012 12:28 AM
  • Many thanks. That does the trick . . . 

    DragonForest

    Monday, June 04, 2012 1:46 AM
  • Agree it would be easier to set up the form in advance, but was aiming for maximum flexibility/scalability by allowing the form to "scale" in line with user inputs . . . 

    DragonForest

    Monday, June 04, 2012 1:48 AM
  • Many thanks for pointing that out. This is proving somewhat trickier than I was originally hoping . . . 

    DragonForest

    Monday, June 04, 2012 1:49 AM
  • Are you using a bound form? If so, just adding a textbox won't do any good without a field in the underlying table to bind it to. Allowing users to add fields is not recommended.

    Perhaps you should explain more.

    Monday, June 04, 2012 10:48 AM
  • Many thanks for your follow-up. I'm using an Unbound form. Upon review and reflection, I'm quite happy with the explanations/solutions you and the others have provided here, so probably no need to delve any further at this time. Many thanks again! 

    DragonForest

    Monday, June 04, 2012 11:12 AM