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

Counting Checkboxes 3

Status
Not open for further replies.

Jeddiah

Programmer
Apr 30, 2002
17
US
I made a ballot for a yearly election using checkboxes but, I need to make sure that only (6) six candidates are choosen, so I created a event procedure in the on current field. Here it goes (though its been butcher about 10 times.) Can someone help me and tell me where I went wrong?

Dim CAN01, CAN02, CAN03, CAN04, CAN05, CAN06, CAN07, CAN08, CAN09, CAN10 As Boolean

Dim TotalVotes As IntegerDim TotalVotes As Integer

If CAN01 = -1 Then
TotalVotes = TotalVotes + 1
ElseIf CAN02 = -1 Then
TotalVotes = TotalVotes + 1
ElseIf CAN03 = -1 Then
TotalVotes = TotalVotes + 1

If TotalVotes >= -6 Then
MsgBox "To many Candidate selected", vbAbortRetryIgnore, "Votes Exceeded"
End If
This is the short version.
 
Stop going nuts with negative values. How about:

Totalvotes = 0
If CAN01 = -1 Then
TotalVotes = TotalVotes + 1
ElseIf CAN02 = -1 Then
TotalVotes = TotalVotes + 1
ElseIf CAN03 = -1 Then
TotalVotes = TotalVotes + 1
If TotalVotes > 6 Then
MsgBox "To many Candidate selected", vbAbortRetryIgnore, "Votes Exceeded"
End If
Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
I then get the error message Compile error: Block If without End If. I don't have much experience with error handling. Any/all additional help is greatly appreciated and welcomed.
 
dim totalvotes as integer

totalvotes = 0
if me.check1 = true then totalvotes = totalvotes + 1
if me.check2 = true then totalvotes = totalvotes + 1
if me.check3 = true then totalvotes = totalvotes + 1
if me.check4 = true then totalvotes = totalvotes + 1
if me.check5 = true then totalvotes = totalvotes + 1
if me.check6 = true then totalvotes = totalvotes + 1
if me.check7 = true then totalvotes = totalvotes + 1
if me.check8 = true then totalvotes = totalvotes + 1
if me.check9 = true then totalvotes = totalvotes + 1
ect...

if totalvotes > 6 then
cancel = true
MsgBox "To many Candidate selected", _ vbAbortRetryIgnore, "Votes Exceeded"
end if

JHauge@jmjpc.net
Life is change. To deny change is to deny life.
 
Thank a lot it works, but is there a way I can get the checkbox result to show my selections in the field as I go.
 
where do you want to see it(a message window, or an unbound text box...), and what is it you want to see??

(how many have been selected??)

--James JHauge@jmjpc.net
Life is change. To deny change is to deny life.
 
anyone use loops any more!

private sub votecounts()
dim intvotes as integer
dim intvotecnt as integer

for intvotes = 1 to 9
if me("can0" & intvotes)= true then
intvotescnt = intvotescnt + 1
next intvotes
msbox "You have selected " & intvotecnt
end sub


 
I want to see how many have been selected in an unbound field on my form.
 
totalvotes = 0
if me.check1 = true then totalvotes = totalvotes + 1
if me.check2 = true then totalvotes = totalvotes + 1
if me.check3 = true then totalvotes = totalvotes + 1
if me.check4 = true then totalvotes = totalvotes + 1
if me.check5 = true then totalvotes = totalvotes + 1
if me.check6 = true then totalvotes = totalvotes + 1
if me.check7 = true then totalvotes = totalvotes + 1
if me.check8 = true then totalvotes = totalvotes + 1
if me.check9 = true then totalvotes = totalvotes + 1

me.txtunboundfieldname = totalvotes

this can be put behind a button to total it, or some thing like that... if you want you can put a little bit of code in the afterupdate of each of the check boxes... that kind of code would be like this for the after update of one field...

if me.check1 = true
me.txtunboundfieldname = me.txtunboundfieldname + 1
else
me.txtunboundfieldname = me.txtunboundfieldname - 1
end if

check these both out...

--James JHauge@jmjpc.net
Life is change. To deny change is to deny life.
 
Jeddiah,

How about this: Put logic like this in the After Update event for each check box:
Code:
Me.txtCount = Abs([chkCAN01] + [chkCAN02] + [chkCAN03]....)
If txtCount >= 6 Then
    MsgBox "To many Candidate selected", vbAbortRetryIgnore, "Votes Exceeded"
    End If

txtCount would be the text box on the form to display the count. This will display a running count as each check box is selected........
 
Will try. Thanks for all your help, you guys are the greatest.
 
Cosmo has the right ideal but i think you are doing it the hard way typing in after each update.

create a sub

Private Function Votecounts()
Dim intvotes As Integer
Dim intvotecnt As Integer
'loops thru 9 checkboxes
For intvotes = 1 To 9 'assumes boxes named can01 to can09
If Me("can0" & intvotes) = True Then intvotecnt = intvotecnt + 1
Next intvotes
me!unboundtextbox.value = intvotecnt'change to proper name
Select Case intvotecnt
Case 6
If MsgBox("You have selected " & intvotecnt & " Are you sure of your choices", vbYesNo) = vbYes Then
'code to move to new form or turn edits off
Else
'do nothing return to form
End If
Case Is > 6
MsgBox "you have selected more then the 6 allowed please unselect " & intvotecnt - 6 & " Candidates"
Case Else
'do nothing
End Select
End Function

then multiselect your checkboxes and under the afterupdate event type in
=votecounts()


 
now that solution i like:)

--James JHauge@jmjpc.net
Life is change. To deny change is to deny life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top