Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LOOPING THROUGH CONTROLS!

Status
Not open for further replies.

akasmia

MIS
Apr 12, 2002
30
US
This is a simple thing but I am a simple user!
I have form with many check boxes, radio boxes and text boxes. I would like to loop through the controls to changes their values.
example
There are 40-50 check boxes to be reset to false all at once (i.e. cleared or reset).

I have tried using things like:
Dim ctrTemp as control
for each ctrTemp in me.controls
me.ctrTemp.value = FALSE
next

but I was not able to acoomplish any thing.

It seems that I am able to madify the control properties but not their values...!.
 
Hi

When you say 'not accomplish anything', what happened, was there an error message?

Dim ctrTemp as CheckBox
for each ctrTemp in me.controls
ctrTemp.value = FALSE
next
DoEvents

should do what you require





Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Thanks for the the help Ken. It still gives me this error: 'type mismach'. I even cpioed your exact code!.

I will try to teak it a bit more uless you have a suggesstion.

AK
 
You're trying to set all of your controls to false...not just check boxes...that's why you're getting the type mismatch I think. Check box controls have a specific id, but I can't remember it right now...hopefully Ken knows it..

Kevin
 
Hi

I would have thought that since we dimmed the control as acheck box, it owuld loop through only check boxes, but if you tell me it is still not working, then maybe I am wrong, so try

Dim ctrTemp as CheckBox
for each ctrTemp in me.controls
If ctrTemp.ControlType = acCheckBox Then
ctrTemp.value = FALSE
end If
next
DoEvents



Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Hi,

You need to change

Dim ctrTemp as CheckBox

to

Dim ctrTemp as Control

Ken kind of got the point, because you are looping through all controls in the example shown you can't make ctrTemp = to the next control if it's not a CheckBox, this is why you get the Type Missmatch error.

The line...

If ctrTemp.ControlType = acCheckBox Then

will ensure the code is only applied to CheckBox's

Hope this helps.





There are two ways to write error-free programs; only the third one works.
 
Here is the code segment:

Dim ctrTemp As Control
On Error GoTo Err_cmdSelectAll_Click

For Each ctrTemp In Me.Controls
If ctrTemp.ControlType = acCheckBox Then
ctrTemp.Value = False
End If
Next
DoEvents

I am getting this error:
2448
You can't assign a value to this object.
 
I built a form and added several check boxes and option buttons and this code works fine:
Private Sub Command12_Click()
Dim ctl As Access.Control
For Each ctl In Me.Controls
If ctl.ControlType = acCheckBox Then
ctl.Value = False
ElseIf ctl.ControlType = acOptionButton Then
ctl.Value = False
End If
Next
End Sub

I'm pretty sure that the reason you are getting the error is because you have option buttons within an Option Group. If that's the case then you have to address the Option Group. This will work:

Dim ctl As Access.Control
For Each ctl In Me.Controls
If ctl.ControlType = acOptionGroup Then
ctl.DefaultValue = ""
End If
Next
 
1. Are the checkboxes calculated? If yes, you indeed can't change their values.
2. Are they bound to the fields of a query? If yes, is the query updatable? You can check it by opening the query and trying to change the value directly.
3. What event do you use to fire the code? If by any chance it's the Open event, move the code to the Load event.


Just some thoughts...

HTH


[pipe]
Daniel Vlas
Systems Consultant

 
Thanks for your help.
Finally got to work for the entire form. How do I limit my loop to a subset of checkboxes, is it possible?. There are times where I don't want to reset all check boxes!.

Once again thanks for a big help!.
AK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top