Hello,
I'm using ChrisO's module (https://access-programmers.co.uk/forums/showpost.php?p=915277&postcount=10) to highlight controls on focus event in forms, however I can't make it work with subforms. I get a Run-time error "2450" : Application cannot find form.
I tried several changes, like :
With :
And :
I tried several expressions in the With statement, using bangs or ".Form" after the strFormName, but nothing seems to work and I'm running out of ideas. I think I'm failing to get the right combination of changes, or maybe I'm just missing the obvious.
It was suggested here (https://access-programmers.co.uk/forums/showthread.php?t=275143) to call the function in the subform but that's already the case, unless I'm doing it wrong.
Solutions have been found here (https://access-programmers.co.uk/forums/showthread.php?t=275003) but the link is dead, and here (https://experts-exchange.com/questions/29069325/Change-backcolor-on-control-that-has-focus.html) but they weren't shared.
I attached ChrisO's database demo where I simply put frmTest2 as a subform inside frmTest1 without touching the code.
Can someone help me please ? Many thanks !
I'm using ChrisO's module (https://access-programmers.co.uk/forums/showpost.php?p=915277&postcount=10) to highlight controls on focus event in forms, however I can't make it work with subforms. I get a Run-time error "2450" : Application cannot find form.
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"
' Save current configuration.
lngForeColour = .ForeColor
lngFontWeight = .FontWeight
lngBorderStyle = .BorderStyle
lngBorderColour = .BorderColor
lngBackStyle = .BackStyle
lngBackColour = .BackColor
' Set required configuration.
.ForeColor = vbBlue
.FontWeight = 700
.BorderStyle = 1
.BorderColor = vbRed
.BackStyle = 1
.BackColor = vbYellow
Case "Lost"
' Restore saved configuration.
.ForeColor = lngForeColour
.FontWeight = lngFontWeight
.BorderStyle = lngBorderStyle
.BorderColor = lngBorderColour
.BackStyle = lngBackStyle
.BackColor = lngBackColour
End Select
End With
Err.Clear
End Function
Code:
=HandleFocus('" & frmThisForm.Parent.Name & "', '" & frmThisForm.Name & "', '" & ctl.Name & "', '" & "', 'Got')"
Code:
Public Function HandleFocus(ByVal strParentFormName As String, _
ByVal strFormName As String, _
ByVal strControlName As String, _
ByVal strChange As String)
Code:
With Forms(strParentFormName)(strFormName)(strControlName)
It was suggested here (https://access-programmers.co.uk/forums/showthread.php?t=275143) to call the function in the subform but that's already the case, unless I'm doing it wrong.
Solutions have been found here (https://access-programmers.co.uk/forums/showthread.php?t=275003) but the link is dead, and here (https://experts-exchange.com/questions/29069325/Change-backcolor-on-control-that-has-focus.html) but they weren't shared.
I attached ChrisO's database demo where I simply put frmTest2 as a subform inside frmTest1 without touching the code.
Can someone help me please ? Many thanks !
Attachments
Last edited: