How to do this?

W

Will

I'm sure the solution is simple.

What I am trying to do is set specific attributes for a given field AND its
label AND the field that occurs NEXT according to tab order sequence.

So, as the user selects a field for data entry, the following should happen:

When a field [FIELD A] gets focus;
Its label text is changed to red
The field border color is set to red and its border width is set to 1
point

The NEXT field [FIELD B] (next here means the next field as determined
by tab order)
Sets its label text to black
Sets its field border to black 1 point

When the user tabs over to FIELD B and FIELD B gets focus then:
FIELD A label text is set to black
FIELD A text field border is set to black and its border width to
'hairline'
FIELD B label text is set to red
FIELD B text field border is set to red and its border width is set to 1
point.

And so on all the way through the form.

I know how to set these properties using the 'properties' dialog box, but
not how to do this programatically.


Thanks All!
Will
 
D

Dirk Goldgar

I'm sure the solution is simple.

What I am trying to do is set specific attributes for a given field
AND its label AND the field that occurs NEXT according to tab order
sequence.
So, as the user selects a field for data entry, the following should
happen:
When a field [FIELD A] gets focus;
Its label text is changed to red
The field border color is set to red and its border width is set
to 1 point

The NEXT field [FIELD B] (next here means the next field as
determined by tab order)
Sets its label text to black
Sets its field border to black 1 point

When the user tabs over to FIELD B and FIELD B gets focus then:
FIELD A label text is set to black
FIELD A text field border is set to black and its border width to
'hairline'
FIELD B label text is set to red
FIELD B text field border is set to red and its border width is
set to 1 point.

And so on all the way through the form.

I know how to set these properties using the 'properties' dialog box,
but not how to do this programatically.

Interesting problem. You need a function to determine what control is
next in the tab order, and one to apply the desired formatting. Try
these:

'----- start of code -----
Function HighlightControls()

Dim sNext As String

On Error Resume Next

With Screen.ActiveControl

.Controls(0).ForeColor = vbRed
.BorderColor = vbRed
.BorderWidth = 1

sNext = NextInTabOrder(Screen.ActiveControl)
If Len(sNext) > 0 Then
With .Parent.Controls(sNext)
.Controls(0).ForeColor = vbBlack
.BorderColor = vbBlack
.BorderWidth = 1
End With
End If

End With

With Screen.PreviousControl
.Controls(0).ForeColor = vbBlack
.BorderColor = vbBlack
.BorderWidth = 0
End With

End Function

Function NextInTabOrder(StartCtl As Access.Control) As String

' Returns the name of the next active control in the tab order,
' if possible. Returns a null string if there isn't one.

Dim astrControlNames() As String
Dim obj As Object
Dim ctl As Access.Control
Dim intI As Integer

On Error Resume Next
'to ignore errors we expect to raise

Set obj = StartCtl.Parent

ReDim astrControlNames(obj.Controls.Count)

' Build a list of available controls indexed by TabIndex.
' We aren't interested in controls that are invisible,
' disabled, not a tab stop, or not in the same section
' as StartCtl.
For Each ctl In obj.Controls
With ctl
If .Section = StartCtl.Section Then
If .Visible = True _
And .Enabled = True _
And .TabStop = True Then
astrControlNames(.TabIndex) = .Name
End If
End If
End With
Next ctl

Set obj = Nothing

' Loop forward through the list
For intI = StartCtl.TabIndex + 1 To UBound(astrControlNames)
If Len(astrControlNames(intI)) > 0 Then
NextInTabOrder = astrControlNames(intI)
Exit Function
End If
Next intI

' If we didn't find one, start at the beginning of the list.
For intI = 0 To StartCtl.TabIndex - 1
If Len(astrControlNames(intI)) > 0 Then
NextInTabOrder = astrControlNames(intI)
Exit Function
End If
Next intI

' If we get here, there's no available control.
Exit Function

End Function
'----- end of code -----

I assumed that when you spoke of a control's "label text", you meant the
text of the label that is attached to that control. If that assumption
was wrong, the code will have to be changed.

With the above functions stored in a standard module, you can select all
the relevant controls on the form, open their joint property sheet, and
set the "On Got Focus" property on the Event tab to

=HighlightControls()

The only problem I can see is that I cheated and used
Screen.PreviousControl to get a reference to the control that lost the
focus. If the user doesn't just tab from field to field, but instead
switches between forms, this may not work right. If necessary, one
could easily write an analog to the NextInTabOrder() function, to return
the *previous* control in the tab order.
 
G

Guest

Wouldn't it just be easiest to make your alterations on GotFocus and LostFocus?

So for instance Control1 has focus adjust colors, size, etc.. Move to the
next Control, Control1 resets attributes to whatever and now Control2 has the
focus and adjusts it's attributes.

Best of Luck!
 

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