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!

Looping Through a Forms Controls Collection

Status
Not open for further replies.

Lightning

Technical User
Jun 24, 2000
1,140
AU
Hi All

I have a form with about 50 Check boxes on it. These are used to indicate which of the forms within a survey db have been completed, and which still need to be completed by the user. What I am trying to do is display the check box's label in red if the indicated form has not been completed (checkbox value = 0).

I've tried adapting this code from the on-line help, but it errors out on the line indicated. I've tried declaring the ctlLabelName variable as a Control and get an error "Object Variable or With Block variable not set". If I use the Set keyword, I get the error message "Object required".

The code is called by the form's Open event. because it is an advisory form, all the controls are Locked so that the user has to go back to the relevent form to make any changes.

*****
Code:
Sub SetCheckBoxProperties(frm As Form)
    Dim ctl As Control
    Dim ctlLabelName As Control
    
    ' Enumerate Controls collection.
    For Each ctl In frm.Controls
        ' Check to see if control is check box.
        If ctl.ControlType = acCheckBox Then
            If ctl.Value = 0 Then
                Set ctlLabelName = "lbl" & ctl.Name       'This line gives the error
                ctlLabelName.BackColor = 255
                ctlLabelName.ForeColor = 16777215
            End If
        End If
    Next ctl
End Sub

Can someone show me what I am doing wrong?

Thanks in advance

Lightning
 
The error occurs because you're trying to set a control to a text string. Replace the line causing the error with this:

Set ctlLabelName = frm("lbl" & ctl.NAME)

Jeff
 
You need to make that line:
Set ctlLabelName = frm.Controls("lbl" & ctl.Name)

Rick Sprague
 
Thanks People!!

I thought the problem was passing the name as a text string - I just didn't know how to resolve it.

Thanks again

Lightning
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top