Personally, I never use Screen.ActiveControl, as I don't find it reliable
enough. It's kinda like SendKeys, where you hope the right thing has the
focus at the time your code runs, and I do (not infrequently) call one
routine from another.
So, yes, I hard-code the name of the control. Even if I am calling a generic
routine in a standard module, I will pass in the control that's being
processed rather than rely on Screen.ActiveControl.
Simple example: client had a form with 184 controls on it, and wanted the
active one yellow. Could probably have used Screen.ActiveControl for that,
but we preferred to programmatically set the OnGotFocus and OnLostFocus so
that each text box/combo box identified itself by name:
Public Function MakeHighlight(strFormName As String) As Boolean
Dim ctl As Control
DoCmd.OpenForm strFormName, acDesign
For Each ctl In Forms(strFormName).Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
ctl.OnGotFocus = "=Highlight([" & ctl.Name & "],True)"
ctl.OnLostFocus = "=Highlight([" & ctl.Name & "],False)"
End If
Next
Set ctl = Nothing
End Function
Public Function Highlight(ctl As Control, bShow As Boolean)
ctl.BackColor = IIf(bShow, vbYellow, vbWhite)
End Function
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
Gary Schuldt said:
Thanks, Allen,
OK, so then I have a style question:
In your own coding, do you use the ActiveControl reference, or do you
explicitly state the fully qualified name of the control when you can assume
that the event is a user-triggered one?
For example, on update of an option box MyOptionBox, would you code:
Select Case Me.ActiveControl.Value '(is Value the correct coding here?)
Case 1 . . .
Case 2 . . .
etc.
End Select
or would you code the reference Me.MyOptionBox?
Thanks,
Gary
refer
to