creating check box dynamicly during run time

  • Thread starter Thread starter thread
  • Start date Start date
T

thread

hi all,

i'm trying to build a code that create check box object on open but i'm
getting 2147 error-must be in design view in order to create an object
what should be the best solution to overcome this?
now the next issue is an option to reduce check boxes depending on a
list from a table
i believe the best thing is to choose an array in with a maxsize and
progress with the counter when needed,please give comments regarding
this too
 
Creating controls dynamically will eventually cause your form to error out.
There is a LIFETIME limit of 754 controls on a form. Each time a control is
added, the count goes up by 1, but deleting a control does not reduce the
count.
The usual way to do this is to create the check boxes you might need and
make the Visible property No in design view. Then, in the Load event of the
form, or where ever else is appropriate make the controls visible.
 
You cannot create controls in the Open event of the form. As the man told
you, it must be in design view.

If you are trying to add/remove lots of check boxes, you probably have an
unnormalized design. Typically this occurs when you have lots of yes/no
choices per item, e.g.:
EmployeeID autonumber
Yellow yes/no
Blue yes/no
Red yes/no
Green yes/no
etc.

To normalize this kind of data, turn all those repeating fields into related
records in another table.

Employee table:
EmployeeID AutoNumber primary key

Color table:
ColorID Text primary key

EmployeeColor
EmployeeID Number relates to Employee.EmployeeID
ColorID Text relates to Color.ColorID

So, if employee 6 like Blue and Red, they have 2 *records* in the
EmployeeColor table.

The interface is:
- a main form bound to the Employee table, and
- a subform bound to the EmployeeColor table.
The subform has a combo for selecting the color.
Add as many rows as apply to the subform.
 
you there is no posibility becaouse i see that there is an option to
open a form in a design view and probbly an option to bring it back to
a run time view
can you send me an example for a code for a creation of a code anyway
if you have?

Allen Browne כתב:
 
See help for CreateControl()

But this is definitely the wrong way to go if you are planning to do this
every time the form is opened. As Klatuu points out, you will have to
compact the database often if you are constantly creating and destroying
controls.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

you there is no posibility becaouse i see that there is an option to
open a form in a design view and probbly an option to bring it back to
a run time view
can you send me an example for a code for a creation of a code anyway
if you have?

Allen Browne ???:
 
hi Allen,
i think i will go with your propusal but right now i'm searching for
the fastest way to produce all the check boxes and to control them
during the runtime.
do you think there as a way to make the check boxes to be produced bas
an array so there will not be a problem in identify them later on?
i know that its possible in visual basic but i think i didnt find a way
to do it in access
Allen Browne כתב:
 
Okay, this example shows how to create 256 check boxes in rows of 32. They
are named chk0 to chk255 so you can refer to them in a loop. The code
assumes Form1 is open in design view. Their visible property is No, so you
can display the ones you want with a loop in Form_Open.

Function CreateEm()
Dim frm As Form
Dim chk As CheckBox
Dim i As Integer
Const strcForm = "Form1"
Const lngcSpacer = 216&
Const lngcTopOffset = 1710&

Set frm = Forms(strcForm)
For i = 0 To 255
Set chk = CreateControl(strcForm, acCheckBox, , , , _
(i Mod 32) * 195, (i \ 32) * 195 + 240, 195, 225)
chk.Name = "chk" & i
chk.Visible = False
Next
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

hi Allen,
i think i will go with your propusal but right now i'm searching for
the fastest way to produce all the check boxes and to control them
during the runtime.
do you think there as a way to make the check boxes to be produced bas
an array so there will not be a problem in identify them later on?
i know that its possible in visual basic but i think i didnt find a way
to do it in access
Allen Browne ???:
 
Allen Browne כתב:
Okay, this example shows how to create 256 check boxes in rows of 32. They
are named chk0 to chk255 so you can refer to them in a loop. The code
assumes Form1 is open in design view. Their visible property is No, so you
can display the ones you want with a loop in Form_Open.

Function CreateEm()
Dim frm As Form
Dim chk As CheckBox
Dim i As Integer
Const strcForm = "Form1"
Const lngcSpacer = 216&
Const lngcTopOffset = 1710&

Set frm = Forms(strcForm)
For i = 0 To 255
Set chk = CreateControl(strcForm, acCheckBox, , , , _
(i Mod 32) * 195, (i \ 32) * 195 + 240, 195, 225)
chk.Name = "chk" & i
chk.Visible = False
Next
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

hi Allen,
i think i will go with your propusal but right now i'm searching for
the fastest way to produce all the check boxes and to control them
during the runtime.
do you think there as a way to make the check boxes to be produced bas
an array so there will not be a problem in identify them later on?
i know that its possible in visual basic but i think i didnt find a way
to do it in access
Allen Browne ???:
 
hi allen,
i'm working on your example
i have an issue for the next stage
after we are producting the controls we have chk1-chk40,lbl1-lbl40
now one of my aims are to change the lbls names during the open form
for dynamic check box list
but i dont see any option beside clumsy of making the lables changes
for example
lbl1.caption.....
lbl2.caption.....
.....
lbl40.caption
it will force alot of code lines
do you have any idea how do make a loop over those controls?

Allen Browne כתב:
 
hi allen,
i'm working on your example
i have an issue for the next stage
after we are producting the controls we have chk1-chk40,lbl1-lbl40
now one of my aims are to change the lbls names during the open form
for dynamic check box list
but i dont see any option beside clumsy of making the lables changes
for example
lbl1.caption.....
lbl2.caption.....
.....
lbl40.caption
it will force alot of code lines
do you have any idea how do make a loop over those controls?

Allen Browne כתב:
 
You can refer to the controls of the form by using a string variable like
this:
Dim strControlName As String
Dim i as Integer
For i = 1 to 40
strControlName = "lbl" & i
Me(strControlName).Caption = "What?"
Next

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

hi allen,
i'm working on your example
i have an issue for the next stage
after we are producting the controls we have chk1-chk40,lbl1-lbl40
now one of my aims are to change the lbls names during the open form
for dynamic check box list
but i dont see any option beside clumsy of making the lables changes
for example
lbl1.caption.....
lbl2.caption.....
.....
lbl40.caption
it will force alot of code lines
do you have any idea how do make a loop over those controls?

Allen Browne ???:
 
hi Allen,
maybe you can comment me on this problem:
i build a form and inside there are several or textbox contain formulas
from queries
now my problem is like this most of the computers that using this form
have no problem but there is 1 or 2 computers that everytime i'm trying
to open the form the access is crashing down,now when i tried to search
the problem i found out that its becaouse of one textbox that contain
heav formula but its very stange that most of the computers have no
problem with this and those 2 computers are crashing down,
did you ever bump into this kind of a problem?
maybe you know about something in the configuration of the access or in
windows that should be change in order to fix this problem?

thread כתב:
 
The problem can be caused by several things, such as Name AutoCorrect,
compilation problem, corrupt index, corrupt form, JET version issues,
reference ambiguity, naming clash, or a JET bug.

1. Uncheck the boxes under:
Tools | Options | General | Name AutoCorrect
Explanation of why:
http://allenbrowne.com/bug-03.html

2. Compact the database to get rid of this junk:
Tools | Database Utilities | Compact/Repair

3. Close Access. Make a backup copy of the file. Decompile the database by
entering something like this at the command prompt while Access is not
running. It is all one line, and include the quotes:
"c:\Program Files\Microsoft office\office\msaccess.exe" /decompile
"c:\MyPath\MyDatabase.mdb"

4. Open Access (holding down the Shift key if you have any startup code),
and compact again.

5. Open a code window.
Choose References from the Tools menu.
Uncheck any references you do not need.
For a list of the ones you typically need in your version of Access, see:
http://allenbrowne.com/ser-38.html

6. Still in the code window, choose Compile from the Debug menu.
Fix any errors, and repeat until it compiles okay.

At this point, you should have a database where the name-autocorrect errors
are gone, the indexes are repaired, inconsistencies between the text- and
compiled-versions of the code are fixed, reference ambiguities are resolved,
and the code syntax is compilable.

If it is still a problem, the next step would be to get Access to rebuild
the database for you. Follow the steps for the first symptom in this
article:
Recovering from Corruption
at:
http://allenbrowne.com/ser-47.html

If it only happens on one machine, it may also need a JET service pack:
http://support.microsoft.com/kb/239114

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

hi Allen,
maybe you can comment me on this problem:
i build a form and inside there are several or textbox contain formulas
from queries
now my problem is like this most of the computers that using this form
have no problem but there is 1 or 2 computers that everytime i'm trying
to open the form the access is crashing down,now when i tried to search
the problem i found out that its becaouse of one textbox that contain
heav formula but its very stange that most of the computers have no
problem with this and those 2 computers are crashing down,
did you ever bump into this kind of a problem?
maybe you know about something in the configuration of the access or in
windows that should be change in order to fix this problem?

thread ???:
 
Back
Top