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

For Each CheckBox in... HELP! 1

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
Hallo.
I need to write a piece of code that accesses each check box on a form. I've tried looping thru the controls & testing to see if the left three characters are "chk" (the prefix I use for check boxes), but I can't get the Control object's name - I guess it doesn't have this property.
So maybe I need to do a for each CheckBox in Me, only I don't really understand the For Each... Next statement, and can't figure out the example given in MSAccess help (97).
If anyone could shed any light on my problem, I'd be very grateful.
Cheers,
Douglas JL Common sense is what tells you the world is flat.
 
Hi!

You can use this code:

Dim cntl As Control

For Each cntl In Me.Controls
If cntl.ControlType = acCheckBox Then
do your stuff here
End If
Next cntl

hth
Jeff Bridgham
bridgham@purdue.edu
 
That's fantastic, thanks - my problems are over!
Cheers, man!
Douglas JL Common sense is what tells you the world is flat.
 
The problem with checkboxes is that they can be enclosed in a frame object which removes most of their attributes. Nevertheless, you can still access their names if you don't cause an error by referring to other properties that have been eliminated by placement in an 'Option Group."

*Note in the code below that checkboxes outside a frame have a 'Value' property which can be True(-1), False(0), or Null, but those inside a frame have a numeric 'OptionValue' property - which won't change, only the parent frame's value will change.

Code:
Private Sub cmdGet_Click()
On Error Resume Next

  Dim ctl As Control
  Dim strMsg As String
  
  For Each ctl In Me.Controls
    If TypeOf ctl Is CheckBox Then
    
      If TypeName(ctl.Parent) = "OptionGroup" Then
        strMsg = strMsg & ctl.Parent.Name & "= " & ctl.Parent.Value & _
              ", " & ctl.Name & "= " & ctl.OptionValue & vbCrLf
      Else
        strMsg = strMsg & ctl.Name & "= "
        If IsNull(ctl.Value) Then
          strMsg = strMsg & "Null" & vbCrLf
        Else
          strMsg = strMsg & CBool(ctl.Value) & vbCrLf
        End If
      End If
    End If
  Next ctl
  
  MsgBox strMsg
        
End Sub
VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top