Code to look for empty fields

3699wat

Registered User.
Local time
Today, 17:25
Joined
Oct 28, 2002
Messages
83
I am using the following code to set a label to invisible if the textbox is empty. It works great for each textbox / label on the report, but the problem is that I have to set it for each one.


If Me.Textbox = “ “ or IsNull (Me.Textbox) Then
Me.Label.Visible = False
Else
Me.Label.Visible = True
End If

Is there an expression / code that would loop through the fields and set all labels to invisible if the textbox is empty?
 
Not sure if you want all the labels invisible if one or more text boxes is empty or if you want the only the label that refers to the empty text control to be invisible...

I think it is the latter. If so then you can use the Controls collection to refer to all the controls on the active object. Set a couple of object variables to represent target controls and away you go.

I should preface this example by saying that there are doubtless many better solutions to you problem, but this one should give you a start...

Dim ctl As Control
Dim ctl_label As Control

For Each ctl In Controls
If ctl.ControlType = acTextBox Then
If IsNull(ctl) Then
For Each ctl_label In Controls
If ctl_label.ControlType = acLabel Then
If ctl_label.name = "lbl" & ctl.name Then
ctl_label.Visible = False
End If
End If
Next ctl_label
End If
End If
Next ctl

HTH
Chris
 

Users who are viewing this thread

Back
Top Bottom