Detecting and setting focus

J

Jake

I know that you can use a controll's setfocus method to give it focus.
However, this fails if the form doesn't have focus. Even if I use error
handling to handle the error message, the focus inside the form still doesn't
change.

I can't find any way to detect what form has focus, and any attempt to
deturmine what controll has focus fails if the form it is on doesn't have
focus.

Also, if a controll contains invalid data (it won't vlidate or the
BeforeUpdate event sets Cancel=True) then an error results when attempting to
change focus in code. Is there a way to cancel then edit programatically
(i.e. the equivilant to hitting "Esc")?

Thanks,
Jacob
 
D

Dirk Goldgar

Jake said:
I know that you can use a controll's setfocus method to give it focus.
However, this fails if the form doesn't have focus. Even if I use error
handling to handle the error message, the focus inside the form still
doesn't
change.

What version of Access are you using? While I haven't tested it in Access
2007, in Access 97 to 2003 at least you can setfocus to a control without
the control's parent form having the focus. What exactly are you doing that
raises an error for you?
I can't find any way to detect what form has focus, and any attempt to
deturmine what controll has focus fails if the form it is on doesn't have
focus.

Screen.ActiveForm returns a reference to the form that currently has the
focus, so long as a form does in fact currently have the focus. If no form
has the focus, Screen.ActiveForm raises an error.

For the application as a whole, Screen.ActiveControl returns a reference to
the control that has the focus, provided that there is a control that has
the focus. If no control has the focus, Screen.ActiveControl raises an
error.

For any given form, the form's ActiveControl property returns a reference to
the control that has the focus *on that form*. Note that this may not be
the same a Screen.ActiveControl -- it won't be, if that form doesn't have
the focus.
Also, if a controll contains invalid data (it won't vlidate or the
BeforeUpdate event sets Cancel=True) then an error results when attempting
to
change focus in code. Is there a way to cancel then edit programatically
(i.e. the equivilant to hitting "Esc")?

Given a reference to the control, you can call the control's Undo method.
 
J

Jake

I am using 2003,

I will post a bit later with more details on the error I am getting.

You say that ActiveControll and ActiveForm throw an error of no controll or
form are active, respectively; is there a way to find out if there is a form
or controll active before (or without) actually causing the error?

Jacob
 
D

Dirk Goldgar

Jake said:
You say that ActiveControll and ActiveForm throw an error of no controll
or
form are active, respectively; is there a way to find out if there is a
form
or controll active before (or without) actually causing the error?


None that I know of. What I do is temporarily switch to inline
error-handling, get the value I want, and then switch back to my regular
error-handling. For example:

Dim strActiveFormName As String

On Error GoTo Err_Handler

' ... misc code ...

On Error Resume Next
strActiveFormName = Screen.ActiveForm.Name
On Error GoTo Err_Handler

If Len(strActiveFormName) = 0 Then
' There was no active form
Else
' Do something with the active form:

With Forms(strActiveFormName)
' ... code against form here ...
End With

End If
 

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