Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"
Hi Chris
just to point out really, the title of your post reflects the answers that you get. If you specifically look for answers relating to "lost focus" and "got focus", then they would generally be the answers you get. It would be quite obvious that asking for these 2 handlers for the 1 object would mean the exact thing which you say is not feasible due to the amount of text boxes.
Option Explicit
Option Compare Text
Private Sub Form_Open(ByRef intCancel As Integer)
InitialiseEvents Me
End Sub
In a Standard Module: -
Code:
Option Explicit
Option Compare Text
Public Sub InitialiseEvents(ByRef frmThisForm As Form)
Dim ctl As Control
Dim strControlName As String
For Each ctl In frmThisForm
On Error Resume Next
ctl.OnGotFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Got')"
ctl.OnLostFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Lost')"
Err.Clear
Next ctl
End Sub
Public Function HandleFocus(ByVal strFormName As String, _
ByVal strControlName As String, _
ByVal strChange As String)
With Forms(strFormName)(strControlName)
Select Case strChange
Case "Got"
.ForeColor = vbBlue
.FontBold = True
Case "Lost"
.ForeColor = vbBlack
.FontBold = False
End Select
End With
End Function
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"
This is the method that I (would) use. I use a variation on it for changing the backcolor property of textboxes and comboboxes, so that the user can see what item the tab is at and needs data input.
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"
Hello,
I'm new to this forum so I hope it's ok I bring this topic back up. I absolutely LOVE ChrisO's module, I've been looking for something like this for days, so thank you, it's almost exactly what I need.
So here's my question : would there be a way to slightly change the code so that, instead of the control on/off focus, it highlights the label associated with it ? Assuming I use some naming convention, like every label has the name of the control followed by "_lbl" for example, so [Textbox1] is associated with [Textbox1_lbl], [Combo4] with [Combo4_lbl], etc.
Basically, I think I would need to add something like :
Code:
lbl.Name = ctl.Name & "_lbl"
... and then make it so that the changes are applied to it.
I've been trying a few things with no success, I'm not very good with VBA. Can somebody help me please ?
Many thanks !
Also sorry for my poor english, I hope I'm clear enough.
You should be able to just add to the list in Function HandleFocus: -
Code:
Option Explicit
Option Compare Text
Public Sub InitialiseEvents(ByRef frmThisForm As Form)
Dim ctl As Control
On Error Resume Next
For Each ctl In frmThisForm
ctl.OnGotFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Got')"
ctl.OnLostFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Lost')"
Next ctl
Err.Clear
End Sub
Public Function HandleFocus(ByVal strFormName As String, _
ByVal strControlName As String, _
ByVal strChange As String)
Static lngForeColour As Long
Static lngFontWeight As Long
Static lngBorderStyle As Long
Static lngBorderColour As Long
Static lngBackStyle As Long
Static lngBackColour As Long
On Error Resume Next
With Forms(strFormName)(strControlName)
Select Case strChange
Case "Got"
[COLOR=green]' Save current configuration.[/COLOR]
lngForeColour = .ForeColor
lngFontWeight = .FontWeight
lngBorderStyle = .BorderStyle
lngBorderColour = .BorderColor
lngBackStyle = .BackStyle
lngBackColour = .BackColor
[COLOR=green]' Set required configuration.[/COLOR]
.ForeColor = vbBlue
.FontWeight = 700
.BorderStyle = 1
.BorderColor = vbRed
.BackStyle = 1
.BackColor = vbYellow
Case "Lost"
[COLOR=green]' Restore saved configuration.[/COLOR]
.ForeColor = lngForeColour
.FontWeight = lngFontWeight
.BorderStyle = lngBorderStyle
.BorderColor = lngBorderColour
.BackStyle = lngBackStyle
.BackColor = lngBackColour
End Select
End With
Err.Clear
End Function