Cleaning Code (1 Viewer)

hllary

Registered User.
Local time
Yesterday, 17:03
Joined
Sep 23, 2019
Messages
80
I'm writing some code to hide the text box if the value is null. I have twenty text boxes.

I'm using the code below, but I was wondering if there is cleaner way of doing this.

Code:
Private Sub Form_Current()
If IsNull(Me.Text0) Then
    Me.Text0.Visible = False
    Else
    Me.Text0.Visible = True
    
End If

If IsNull(Me.Text1) Then
    Me.Text1.Visible = False
    Else
    Me.Text1.Visible = True
End If

If IsNull(Me.Text2) Then
    Me.Text2.Visible = False
    Else
    Me.Text2.Visible = True
End If

If IsNull(Me.Text3) Then
    Me.Text3.Visible = False
    Else
    Me.Text3.Visible = True
End If

If IsNull(Me.Text4) Then
    Me.Text4.Visible = False
    Else
    Me.Text4.Visible = True
End If
 

plog

Banishment Pending
Local time
Yesterday, 19:03
Joined
May 11, 2011
Messages
11,643
IsNull resolves to True/False, so just use it on the right side of the equality instead of all the If/Elses:

Me.Text0.Visible = Not IsNull(Me.Text0)
Me.Text1.Visible = Not IsNull(Me.Text1)
 

plog

Banishment Pending
Local time
Yesterday, 19:03
Joined
May 11, 2011
Messages
11,643
Sorry that was the big thing I saw. Since they are all named similarly you could wrap them in a loop and set them that way:

Code:
For i=0 to 20
Me.Controls("TextBox" & i).Visible = Not IsNull(Me.Controls("Textbox" & i)
Next

Above is psuedo code
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:03
Joined
Feb 19, 2002
Messages
43,257
Before you write all this code, you might consider giving proper names to the controls.
 

Users who are viewing this thread

Top Bottom