423,335 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,335 IT Pros & Developers. It's quick & easy.

Can Access Create Controls At Runtime?

P: 50
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?
Oct 15 '08 #1
Share this Question
Share on Google+
6 Replies


ADezii
Expert 5K+
P: 8,584
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.
Oct 15 '08 #2

P: 50
Awesome. That was just what I needed.

Thanks.
Oct 15 '08 #3

P: 50
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.
Expand|Select|Wrap|Line Numbers
  1. Dim MyForm As Form, MyControl As Control
  2. 'Set MyForm = CreateForm()
  3. Set MyControl = CreateControl(Me.Name, 106)
  4. MyControl.Width = 2000
  5.  
Any ideas on how to make this work? And is there a way to assign a name to the control?
Oct 15 '08 #4

P: 50
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.
Oct 15 '08 #5

ADezii
Expert 5K+
P: 8,584
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.
Oct 15 '08 #6

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1.         DoCmd.OpenForm "Form1", acDesign
  2.         Dim t as TextBox
  3.         set t = CreateControl("Form1", acTextBox)
  4.         t.FontName = "Arial"  'Any sample as test.   
  5.         .ForeColor = vbRead   'Any sample as test.   
  6.         .DefaultValue = 10    'Any sample as test.   
  7.         'etc...
  8.  
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:
Expand|Select|Wrap|Line Numbers
  1.         DoCmd.OpenForm "Form1", acDesign
  2.         With CreateControl("Form1", acTextBox)
  3.             .FontName = "Arial"  'Any sample as test.   
  4.             .ForeColor = vbRead  'Any sample as test.   
  5.             .DefaultValue = 10   'Any sample as test.   
  6.             'etc...     
  7.         End With
  8.  
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:
Expand|Select|Wrap|Line Numbers
  1.         DoCmd.OpenForm "Form1", acDesign
  2.         Dim strName as string
  3.         With CreateControl("Form1", acTextBox)
  4.             .FontName = "Arial"  'Any sample as test.   
  5.             .ForeColor = vbRead     'Any sample as test.   
  6.             .DefaultValue = 10   'Any sample as test.   
  7.             ' etc...
  8.             strName = .Name   'Get the default name from the control (or you can give one).
  9.         End With
  10.         'And to be able to edit it later use the name you got in the previous lines.
  11.         Me.Controls(strName).FontName = "Times New Roman"    'Any sample as test.   
  12.         Me.Controls(strName).ForeColor = vbGreen            'Any sample as test.   
  13.         Me.Controls(strName).DefaultValue = 20                    'Any sample as test.   
  14.         ' etc...
  15.  
Sep 14 '15 #7

Post your reply

Sign in to post your reply or Sign up for a free account.