From: Kurt Heisler on
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.
From: Dirk Goldgar on
"Kurt Heisler" <heislerkurt(a)gmail.com> wrote in message
news:dd07741c-c033-4bf9-b9a8-79018c5119e8(a)t34g2000prd.googlegroups.com...
> 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.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

From: Marshall Barton on
Kurt Heisler wrote:

>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?


This is more an FYI than an answer to your question.

If "efficient" means fewer lines of code and you really
meant to set the border style to solid or transparent, then
you can also avoid using the inner If block:

Dim ctl As Access.Control

For Each ctl In Me.Controls
If ctl.Tag Like "*StyleMe*" Then
ctl.BackStyle = -(Not IsNull(ctl.Value))
ctl.BorderStyle = -(Not IsNull(ctl.Value))
End If
Next ctl

Because that is kind of cryptic, relies on the internal
representation of True and False and is a fraction of a
microsecond slower than the If block Dirk posted, I can not
recommend this kind of code for this problem.

OTOH, I normally do use somthing similar when the properties
being set have True/False values. In your case, it may(?)
be that you do not need to mess with the border and back
style properties and can use the Visible property instead:
ctl.Visible = Not IsNull(ctl)

--
Marsh
MVP [MS Access]
From: david on
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)


"Kurt Heisler" <heislerkurt(a)gmail.com> wrote in message
news:dd07741c-c033-4bf9-b9a8-79018c5119e8(a)t34g2000prd.googlegroups.com...
> 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.


From: Kurt Heisler on
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.