Can Access Create Controls At Runtime?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Krandor
    New Member
    • Aug 2008
    • 50

    Can Access Create Controls At Runtime?

    I have a screen where I will need a LOT of checkboxes. The number I will need is based on how many records in the table - currently 194 but certain to grow larger in the future.

    Can I create all these checkboxes at runtime? I know that in classic Visual Basic this was possible. But can it be done in Access? I have Access 2003.

    I realize that creating controls at runtime slows down the app. This is not a high volume screen so if the user has to wait for a minute or more, it won't be a problem.

    Any ideas or links?
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Can I create all these checkboxes at runtime? I know that in classic Visual Basic this was possible. But can it be done in Access? I have Access 2003.
    Yes and Yes! Reference the CreateControl() Method in the Help Files, and if you have any problem, let us know.

    Comment

    • Krandor
      New Member
      • Aug 2008
      • 50

      #3
      Awesome. That was just what I needed.

      Thanks.

      Comment

      • Krandor
        New Member
        • Aug 2008
        • 50

        #4
        Originally posted by ADezii
        Yes and Yes! Reference the CreateControl() Method in the Help Files, and if you have any problem, let us know.
        It worked but so far not exactly like I expected. What I wanted was for the user to open the screen and have all the controls created on the fly.

        What I am getting is an error message that says Controls can only be created while in design mode. Here is the test code I was using.
        Code:
        Dim MyForm As Form, MyControl As Control
        'Set MyForm = CreateForm()
        Set MyControl = CreateControl(Me.Name, 106)
        MyControl.Width = 2000
        Any ideas on how to make this work? And is there a way to assign a name to the control?

        Comment

        • Krandor
          New Member
          • Aug 2008
          • 50

          #5
          What I am trying to do is this. As part of a software inventory system, I need to identify which pieces of software have been installed on a particular user's computer. That means we need to select the user from one list and then assign all the relevant software packages.

          I thought to do this by dynamically creating one checkbox for each software package and then handling the updating of the relevant table via code.

          If you have a simpler way to accomplish this I would love to hear it.

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by Krandor
            What I am trying to do is this. As part of a software inventory system, I need to identify which pieces of software have been installed on a particular user's computer. That means we need to select the user from one list and then assign all the relevant software packages.

            I thought to do this by dynamically creating one checkbox for each software package and then handling the updating of the relevant table via code.

            If you have a simpler way to accomplish this I would love to hear it.
            1. Are you creating Controls on an existing Form, and if so what is its Name?
            2. If you are creating a New Form then adding Controls to the New Form, what are the Parameters?
            3. Are you selecting the User from a Combo Box, if so what is its Name? What is the relevant info for the Combo Box such as: Row Source, Bound Column, etc.
            4. How do you know which Software Packages are applicable to a specific User? List any Relationships, Table Names, and all Field Names and Data Types involved.
            5. You should get the idea by know, I simply do not have enough information to go on.

            Comment

            • kaddoura1
              New Member
              • Sep 2015
              • 2

              #7
              There is bug in MS Access that even if you follow the instructions from the official website of MS still you won't be able to "create" and "edit" controls at "run-time".
              I found one way to go around this bug (witch is a combination of solutions I got from others).
              Non working solution: Don't tray to define a variable to create the object:-

              Sample 1:
              Code:
                      DoCmd.OpenForm "Form1", acDesign
                      Dim t as TextBox
                      set t = CreateControl("Form1", acTextBox)
                      t.FontName = "Arial"  'Any sample as test.   
                      .ForeColor = vbRead   'Any sample as test.   
                      .DefaultValue = 10    'Any sample as test.   
                      'etc...
              This way will give you error (by the way it is what you get from MS official site).

              If you use the "With" it will work, but....
              Sample 2:
              Code:
                      DoCmd.OpenForm "Form1", acDesign
                      With CreateControl("Form1", acTextBox)
                          .FontName = "Arial"  'Any sample as test.   
                          .ForeColor = vbRead  'Any sample as test.   
                          .DefaultValue = 10   'Any sample as test.   
                          'etc...     
                      End With
              This way Won't give you any error (bug free) But....
              You won't be able to edit it later through the code; because we didn't use an object or variable to hold it or point to it (as in Sample 1).


              My solution is: To use the name of the control to call it using "Controls() ", and that is after useing "With" to create the control.:-
              Sample 3:
              Code:
                      DoCmd.OpenForm "Form1", acDesign
                      Dim strName as string
                      With CreateControl("Form1", acTextBox)
                          .FontName = "Arial"  'Any sample as test.   
                          .ForeColor = vbRead	 'Any sample as test.   
                          .DefaultValue = 10   'Any sample as test.   
                          ' etc...
                          strName = .Name   'Get the default name from the control (or you can give one).
                      End With
                      'And to be able to edit it later use the name you got in the previous lines.
                      Me.Controls(strName).FontName = "Times New Roman"	'Any sample as test.   
                      Me.Controls(strName).ForeColor = vbGreen	        'Any sample as test.   
                      Me.Controls(strName).DefaultValue = 20	                'Any sample as test.   
                      ' etc...

              Comment

              Working...