How to use loopings to get checkboxs’ values?

  • Thread starter hkgary33 via AccessMonster.com
  • Start date
H

hkgary33 via AccessMonster.com

Dear all,
In one of my form, I’ve created 20 check boxes c1,c2…c20 (these checkboxes
are not grouped), and a OK button and Cancel button. Now I’m interested in
which checkboxes the user ticked. So I’ve written a click event procedure for
the OK button. But it’s a mess if I check the value of each checkbox one by
one using the following if statement:
If c1 = True Then

End If
If c2 = True Then

End If

Now what I’d like to do is to use a loop instead of using 20 If…Then
statements, just like the following:
For counter = 1 To 20
temp = “c” & CStr(counter) ‘reconstruct the combo box names
If Me.temp = True Then

End If
Next

But it doesn’t work becuz it can’t detect my variable names…so what can I do
to solve this problem? Do I really need to use the first clumsy method to do
it?

Thanks so much!
Gary
 
S

Stefan Hoffmann

hi,

Charset?
Dear all,
Now what I�d like to do is to use a loop instead of using 20 If�Then
statements, just like the following:
For counter = 1 To 20
temp = �c� & CStr(counter) �reconstruct the combo box names
If Me.temp = True Then
�
End If
Next
For Count = 1 To 20
If Me("c" & Count).Value Then
End If
Next Count


mfG
--> stefan <--
 
O

OfficeDev18 via AccessMonster.com

Use the For Each Control.... construct, and check that the control is a
checkbox. See the For Each in the help for details, and study the examples.

Hope this helps,

Sam
 
H

hkgary33 via AccessMonster.com

Sorry, I still don’t understand how to do. Let me simplify my question,
assume in my form, it consists of 5 checkboxes (cb1-cb5) and 2 buttons. If
checkbox 1 is ticked, then it’ll get the value of cell 1 in a 1-D array named
arrInfo; if checkbox 2 is ticked, then it’ll get the value of cell 2 in
arrInfo, and so on.
But how can I use a For Each loop to do it? I don’t know how to call these
check boxes one by one, can anyone give some sample VBA codes for reference?
Thanks!

Gary

Use the For Each Control.... construct, and check that the control is a
checkbox. See the For Each in the help for details, and study the examples.

Hope this helps,

Sam
Dear all,
In one of my form, I’ve created 20 check boxes c1,c2…c20 (these checkboxes
[quoted text clipped - 24 lines]
Thanks so much!
Gary
 
S

Stefan Hoffmann

For your original problem:

For Count = 1 To 20
If Me("c" & Count).Value Then
End If
Next Count


mfG
--> stefan <--
 
R

RoyVidar

hkgary33 via AccessMonster.com said:
Sorry, I still don’t understand how to do. Let me simplify my
question, assume in my form, it consists of 5 checkboxes (cb1-cb5)
and 2 buttons. If checkbox 1 is ticked, then it’ll get the value of
cell 1 in a 1-D array named arrInfo; if checkbox 2 is ticked, then
it’ll get the value of cell 2 in arrInfo, and so on.
But how can I use a For Each loop to do it? I don’t know how to call
these check boxes one by one, can anyone give some sample VBA codes
for reference? Thanks!

Gary

Use the For Each Control.... construct, and check that the control
is a checkbox. See the For Each in the help for details, and study
the examples.

Hope this helps,

Sam
Dear all,
In one of my form, I’ve created 20 check boxes c1,c2…c20 (these
checkboxes [quoted text clipped - 24 lines] Thanks so much!
Gary

See Stefan Hoffmans reply on your original question. If you can't see
it, here's something close.

For counter = 1 To 20
If Me.Controls("c" & cstr(counter)).Value Then
' The control is checked
End If
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top