More efficient code for multiple If...End If statements

K

Kurt Heisler

In the on current event of my form, I'm setting the Backstyle and
Borderstyle of several controls based on the whether the control is
null.

For example:

' 0 = Transparent
' 1 = Normal

If IsNull(Me.FirstName) Then
Me.FirstName.BackStyle = 0 ' Transparent
Me.FirstName.BorderStyle = 1
Else
Me.FirstName.BackStyle = 1 ' Normal
Me.FirstName.BorderStyle = 1
End If

If IsNull(Me.LastName) Then
Me.LastName.BackStyle = 0 ' Transparent
Me.LastName.BorderStyle = 1
Else
Me.LastName.BackStyle = 1 ' Normal
Me.LastName.BorderStyle = 1
End If

etc.

Is there is a more efficient way to write this code? Maybe a select
statement or a function that I can call for each control? I'm just
unsure on the syntax.

Thank you for any tips.
 
D

Dirk Goldgar

Kurt Heisler said:
In the on current event of my form, I'm setting the Backstyle and
Borderstyle of several controls based on the whether the control is
null.

For example:

' 0 = Transparent
' 1 = Normal

If IsNull(Me.FirstName) Then
Me.FirstName.BackStyle = 0 ' Transparent
Me.FirstName.BorderStyle = 1
Else
Me.FirstName.BackStyle = 1 ' Normal
Me.FirstName.BorderStyle = 1
End If

If IsNull(Me.LastName) Then
Me.LastName.BackStyle = 0 ' Transparent
Me.LastName.BorderStyle = 1
Else
Me.LastName.BackStyle = 1 ' Normal
Me.LastName.BorderStyle = 1
End If

etc.

Is there is a more efficient way to write this code? Maybe a select
statement or a function that I can call for each control? I'm just
unsure on the syntax.

Thank you for any tips.


You can use the Tag property of each control to identify it as one that you
want to do this with. Then you can have your code loop through the form's
controls, like this:

Dim ctl As Access.Control

For Each ctl In Me.Controls
If ctl.Tag Like "*StyleMe*" Then
If IsNull(ctl.Value) Then
ctl.BackStyle = 0 ' Transparent
ctl.BorderStyle = 1
Else
ctl.BackStyle = 1 ' Normal
ctl.BorderStyle = 1
End If
End If
Next ctl

In this code, I don't see any reason to be setting BorderStyle, since it's
staying the same, but that may be a typo, or you may be doing something
different with it elsewhere.
 
D

david

If you want to do this to all the text controls
on the form, you don't have to use the tag
property to tag them:

Dim ctl As Access.Control

For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If IsNull(ctl.Value) Then
ctl.BackStyle = 0 ' Transparent
ctl.BorderStyle = 1
Else
ctl.BackStyle = 1 ' Normal
ctl.BorderStyle = 1
End If
End If
Next ctl

(david)
 
K

Kurt Heisler

Thank you everyone for pointing out the tag option.

And I'm not sure why I set the border style the same each time.

To reclaim some screen real estate I deleted the labels for the
controls. To tell the user what to enter I've added a label (with no
border) under each control with text like "First Name," "Last Name,"
etc., and set the font to gray and italics (much like how you often
see the word "Search" inside a search box.) When the control is null,
the controls' backstyle is set to transparent, so the label shows
through. (Not sure why I even messed with the controls' borders as
they should stay normal regardless.) When the user enters the control,
OnEnter code hides the label by setting the control's back style to
normal. I have similar code in the OnExit event to evaluate whether
the control is still null and, if so, set the back style back to
transparent. I guess I could just toggle the visible properties for
the labels and controls, but would need to do some set focus work to
avoid a 'you can't hide a control that has the focus' kind of error.
Same amount of code, either way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top