Emily Beth
Emily Beth

Reputation: 769

Can a form with visible controls have no controls with focus?

I have some Access VBA code that uses the ActiveControl properties. That would throw an error if there happened to be no control with focus. Do I need to worry about that if there are always visible controls that could have focus? That is, in a form where there are always controls that could have focus, is there any way for no control to have focus?

I see other posts about somewhat related concerns. They seem to indicate that the only way (if I wanted to have VBA that would prevent any control from having focus) would be to set the focus to some physically hidden (but visible=True) control. That implies that there is no way for no control to have focus. But I wanted to ask this question explicitly.

Upvotes: 0

Views: 731

Answers (2)

DataWriter
DataWriter

Reputation: 1040

You can definitely have an active form with visible and enabled controls, which has focus, yet has no active control. The documentation for both form.ActiveControl and screen.ActiveControl mention it: "If no control has the focus when you use the ActiveControl property, or if all of the active form's controls are hidden or disabled, an error occurs."

Here's a simple, and common case. Create a simple splash screen with a button and a label. I've made this one both Pop Up and Modal, but neither setting seems to matter for our test.

Sample Form

Next, create a FORM_LOAD() event and a FORM_CURRENT() even with the following code behind them:

Private Sub Form_Current()

'    Debug.Print Screen.ActiveControl.Name
'    Debug.Print Me.ActiveControl.Name

End Sub

Private Sub Form_Load()
    On Err GoTo PrintError

 '   Me.SetFocus
    Me.lbSample.Caption = "Welcome to my Application"

'    Debug.Print Form.ActiveControl.Name
'    Debug.Print Screen.ActiveControl.Name

ExitHere:
    Exit Sub
PrintError:
    Debug.Print Err.Description & " (" & Err.Number & ")"
    GoTo ExitHere
End Sub

Now, test the "ActiveControl" property by removing the comment-mark from the Screen.ActiveControl or form.ActiveControl statements under either the LOAD() or CURRENT() events.

Even though I'm setting the label's caption, it does not make it the active control. Also, neither giving the form itself focus or setting Form.Visible = True is enough to activate a control:no matter which way you try to access the property, through Screen.ActiveControl or Form.ActiveControl, you'll still receive error 2474:

Error 2474

I tried, but wasn't able to trap this error in either the event sub itself or the FORM_ERROR() event.

So here's a pretty common case--a splash screen--where there is no active control. You may be able to work around it by testing for a control having focus instead, it depends on what you're trying to accomplish.

Upvotes: 0

Andre
Andre

Reputation: 27634

No. If a form has controls that are visible and enabled (!), one of them will always have the focus, if the form has the focus.

Nevertheless, you should practice Defensive Programming and not let your code crash, if there is no control (because 3 years later, someone decided that there is a case where all the controls must be disabled), or an unexpected control (that someone added later) is the active one.

Upvotes: 0

Related Questions