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

Option Groups - how do you use checkboxes?

Status
Not open for further replies.

christheprogrammer

Programmer
Jul 10, 2000
258
CA
Hi
I have 4 checkboxes inside an option group. Only one may be chosen. This code is not correct:

if not isnull(Check10) and Check10 = True then
call build_function(x)
end if

Etc, the same for all four boxes.
My Question is what is wrong with this?
Thanks all

Chris
 
If only one needs to be chosen then why not use the radio buttons instead of check boxes?
 
Thanks all for reading this, but I have figured it out - for anyone who wants to know here is the code -

if frame8.value = 1 then build(x)
if frame8.value = 2 then build(x1)
..
..
....
and so on. frame8.value is assigned the Option Value seen in the properties of each checkbox.
See ya
Chris
 
Chris, if
if frame8.value = 1 then build(x)
if frame8.value = 2 then build(x1)
why the (x1) I am just trying to understand this because I am working on something similar, here is the issue:
I have 15 check boxes on a 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
you give me a hand on the code?
Thanks.
 
Hi,

Why not simply use a variable that will be incremented when the user clicks on a checkbox. If the variable is equal to 5 on the next click...uncheck it and display the msgbox. When the user unchecks a box simply decrement the variable.You can write a function with a static variable to do it.

Have a good one!
BK
 
I understand this concept, I think. It is just that I am not clear on how to create the variable, and attach it to all 15 checkboxes, so that (the form maybe?) knows when a sixth box is trying to be checked. I thank you for your time. This is helping a lot. I was so happy when I found your post at 2am last night!
 
ISO,

You can use three Function statements to acomplish what you need to accomplish.

For this example, there are ten checkboxes (68, 70, 72, ... 84, 86), one Label (LblMessage) and an unbound textbox on the form named "Score" (visible or not).

The first function statement ("VoteCount") keeps a running tally of how many times the person has voted. Be sure to to set the default value of your checkboxes at zero in the property box.

Code:
     Private Function VoteCount()
        Score() = -1 * (Check68 + Check70 + Check72 + 
        Check74 + Check76 + Check78 + Check80 + Check82 +
        Check84 + Check86)
     End Function
_______________________________________

The second Function statement ("CountCheck") checks the tally and locks or unlocks the checkboxes as appropriate.

Code:
Private Function CountCheck()
If Score() > 4 Then
    If Check68() = 0 Then
    Check68.Locked = True
    End If
    '
    '
    '
    '
    If Check86() = 0 Then
    Check86.Locked = True
    End If
ElseIf Score() < 5 Then
    Check68.Locked = False
    Check70.Locked = False
    Check72.Locked = False
    Check74.Locked = False
    Check76.Locked = False
    Check78.Locked = False
    Check80.Locked = False
    Check82.Locked = False
    Check84.Locked = False
    Check86.Locked = False
    
End If

End Function
____
________________________________

The last Function statement changes the caption of the label to alert the user of how many votes they may cast.
Code:
Private Function LabelText()
If Score() < 5 Then
LblMessage.Caption = &quot;You may vote for a maximum of five candidates.&quot;
ElseIf Score() > 4 Then
LblMessage.Caption = &quot;Are these the five candidates you wish to vote for?&quot;
End If

End Function
____________________________________

After you have these function statements written, simply put their names in the Click events of each checkbox as well as in the OnLoad and OnCurrent events of the form.

eg:
Code:
____________________________________
Private Sub Check68_Click()
VoteCount
CountCheck
LabelText
End Sub
________________________________________
You'll want to add a Command Button for the user to commit to the votes. If people must cast five votes (no less no more) you can enable the command button as part of the LabelText Function.

Remember that a checked checkbox has a value of -1. You'll want to multiply the values by -1 so you can run a positive integer back to your table of total votes.

BoxHead
 
try this....

Option Compare Database
Option Explicit
Dim intChecks As Integer


Private Sub Form_Load()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acCheckBox Then
If ctl.Value = True Then
intChecks = intChecks + 1
End If
End If
Next ctl
End Sub
Function CheckValue(strCtl As String)
If Me(strCtl) = True Then
If intChecks >= 5 Then
MsgBox &quot;You already have 5 boxes checked, you must uncheck one before you can retain this value&quot;
Me(strCtl).Value = False
Else
intChecks = intChecks + 1
End If
Else
intChecks = intChecks - 1
End If
End Function


Then in the AfterUpdate Event for each CheckBox enter
= CheckValue(&quot;CheckBoxName&quot;)


PaulF
 
Thank you Boxhead and Paul. You both have good ideas! I knew it was something like what you both have described, but I could'nt find it laid out for me anywhere. I have a question for each of you though. Boxhead. Will this take care of any situation where any five boxes are chosen? What if the five that weren't declared in the 'if these five' part are chosen? Just checking(like that?) And Paul, I am little confused by the use of +1 and -1, I am just checking. (haha) also the last after update, is that for storing in the table? because I have already made the checkboxes control source to their respective candidate positions.
let me know (here is a snippet of the syntax I was confused with..)
intChecks = intChecks + 1
End If
Else
intChecks = intChecks - 1
End If
End Function

---------and-----
Then in the AfterUpdate Event for each CheckBox enter
= CheckValue(&quot;CheckBoxName&quot;)


Thank you both ver much.
 
The code only allows for 5 CheckBoxes to be checked (True) and accomplishes this by the use of a Module level variable (intChecks). Since I want to use the minimal amount of coding possible, I check the value of the CheckBox in the Function and not prior to sending it. The code is called regardless of whether the CheckBox is set to True or if it is Unchecked and set to False, this is done by setting the AfterUpdate Event for each CheckBox to call the Function and passing the name of the CheckBox that just changed. The -1 occurs if the CheckBox is not checked (False) and the +1 occurs if the CheckBox is checked, because we want to decrement the variable intChecks when a CheckBox is set to False, and increment it when it is set to True.
If there are already 5 CheckBoxes set to True, the 6th one checked or set to True will be reset to False and the value in the Field it is bound to will also be False.

The Function requires that you pass the name of the CheckBox to it. So if you have a CheckBox with the name of chkOne and another by the name of chkTwo then in the AfterUpdate Event for chkOne, instead of having it set to [Event Procedure], you would enter
= CheckValue(&quot;chkOne&quot;)
and likewise for the CheckBox named chkTwo you would use
= CheckValue(&quot;chkTwo&quot;)

To do a quick test, make a new form with 10 checkboxes, name them chkOne thru chkTen, add the
= CheckValue(&quot;chkOne&quot;)
in the AfterUpdate Event for chkOne,
= CheckValue(&quot;chkTwo&quot;)
for the second one, and so on for all ten Checkboxes, then start checking them off, and see what happens.

Hope this helps

PaulF
 
Thank you. I will try this out. I was trying it another way until now, but the way I had it gave the message box but still let the check box be set to true. I haven't been able to set it back to false. I know this sounds stupid, but I just can't figure out syntax very well.
Thank you again.
IS
 
ISO,

The function doesn't care. Since the function checks the running sum of the checkbox values, you can never have more than five checked. Once a fifth checkbox (&quot; > 4&quot;) is checked, the 2nd Function locks all checkboxes that are not checked. The user can unselect any checked boxes to change a vote, but cannot check a sixth. If a user does uncheck a box, the VoteCount Function totals a new sum and the CheckCount Function unlocks all boxes.



BoxHead
 
I have another question for you, Chris. I need the option groups to only store the click value if the user confirms that they want it by clicking a command button. Can I do this? Come in between the option groups intent to automatically send the data to the table on click?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top