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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

limit check boxes that may be chosen on form

Status
Not open for further replies.

Donkeygirl

Technical User
Nov 30, 2000
52
US
I have 15 check boxes on this form. The user may only choose up to five. How do make it so that if a user checks a sixth box, it does not get checked? (msgbox, no check) Do I have to dim, set, declare the checkboxes and then write an if then or select case statement? If yes or no, could
anyone give me a hand on the code?
Thanks.
 
hi,

to make it fast and easy: write a private function for your form that checks how many checkboxes are "true" already:

Private Function lf_check(int_nr As Integer)
If Me("check" & int_nr) = False Then
int_aant = int_aant - 1
lf_check = True
Else
If int_aant > 1 Then
MsgBox "stop checking"
lf_check = False
Else
int_aant = int_aant + 1
lf_check = True
End If
End If


in the beforeupdate event of each checkbox you add:

Private Sub Check2_BeforeUpdate(Cancel As Integer)
If Not lf_check(2) Then
DoCmd.CancelEvent
Exit Sub
End If

End Sub

 
Thank you, I know this is what I need. Just to clarify -->
how do I call up or create the If_check function, do I have to declare the checks, what is the aant in int_aant? I hope this is not too much. Where am I putting in the max of five? and one more thing, for my understanding (it is hard for me to write code correctly if I don't understand its purpose) what does the -1 and +1 do in the code? I just want to understand so I make the max amount right.
Private Function lf_check(int_nr As Integer)
If Me("check" & int_nr) = False Then
int_aant = int_aant - 1
lf_check = True
Else
If int_aant > 1 Then
MsgBox "stop checking"
lf_check = False
Else
int_aant = int_aant + 1
lf_check = True
End If
End If

in the beforeupdate event of each checkbox you add:

Private Sub Check2_BeforeUpdate(Cancel As Integer)
If Not lf_check(2) Then
DoCmd.CancelEvent
Exit Sub
End If

End Sub

Thank you so much!!!!!!! s-)
 
hi ISO,

sorry for the "aant", I'm dutch you see, the "aant" is my abbreviation in dutch for "the number of items checked", I guess this explains a lot already. So, the int_aant is a variable declared at modele level that keeps count of the number of checks that are set to true. the maximum amount is set in the line " if int_aant > 1" -> in your case that must be "int_aant > 5"

hope this clarifies matters, sorry it took me so long, but the easter week-end you see..

if you need any more help, let me know

the kid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top