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!

Checking for at least one check box 2

Status
Not open for further replies.

JasonPurdueEE

Technical User
May 21, 2002
131
US
Hello everybody. I have a form with 41 different check boxes on it and want to verify that at least one has been "checked" before allowing a user to exit the form. I am having difficulty getting my for loop to work, perhaps somebody could provide some assistance or reccomend a better way of accomplishing this.

The check boxes are titled chk1, chk2, chk3...chk41. They reside in a subform called subCause. I would like my if statement to read: "If (Me!subCause.Form.chk1.value = -1) Then" and increment from chk1 to chk41.

Here's what i have so far:
Code:
'*******************************************
Dim strIf as String
Dim i As Integer

For i = 1 To 41
    strIf = ("Me!subCause.Form.chk" & i & ".value = -1")
    If strIf Then     'here's where I run into problems

        'do stuff here
    End If
Next i
'********************************************

Perhaps I am making this too complicated, is there a simple way to check that at least one check box has been checked?

Thanks,
Jason

______________________________
Sleep is a poor substitute for coffee.
 
Something like this ?
oneChecked = False
For i = 1 To 41
If subCause.Form.Controls("chk" & i) Then
oneChecked = True
Exit For
End If
End If
If Not oneChecked Then
MsgBox "At least one CheckBox must be checked"
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
How are ya JasonPurdueEE . . . . .

In the [blue]BeforeUpdate[/blue] event of the form, add the following code:
Code:
[blue]   Dim ctl As Control, flg As Boolean
   Dim Msg As String, Style As Integer, Title As String
   
   For Each ctl In Me.Controls
      If ctl.ControlType = acCheckBox Then
         If ctl Then
            flg = True
            Exit For
         End If
      End If
   Next
   
   If Not flg Then
      Msg = "Can't save a record unless one checkbox is marked!"
      Style = vbInformation + vbOKOnly
      Title = "No Check Marks Notice!"
      MsgBox Msg, Style, Title
      Me.Undo
      Cancel = True
   End If[/blue]

Calvin.gif
See Ya! . . . . . .
 
Thanks for the quick reply PHV! It worked like a charm! Just for future reference for others who might stumble upon this in search of the same thing, I had to change PHV's sample a tad, here's what I did:

'**************************
oneChecked = False

For i = 1 To 41
If subCause.Form.Controls("chk" & i) Then
oneChecked = True
Exit For
End If
Next i

If Not oneChecked Then
MsgBox "You must select at least one checkbox.", vbOKOnly
Exit Sub
End If
'**************************

Again, thanks PHV!

Jason

______________________________
Sleep is a poor substitute for coffee.
 
Sorry for the typo :~/ and thanks for the pinky.
 
Hey AceMan1,
Thanks for your reply, you must have been typing it as I was replying to PHV and I missed it last night. Your method looks pretty slick. I have another form with a similar checkbox validation situation and will give your method a shot. Thanks again.

Jason

______________________________
Sleep is a poor substitute for coffee.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top