Determining Where Cursor Was B4 Pushing Cmd Button

S

Sprinks

Is there any way to determine where the cursor last was prior to pushing a
command button?

Sprinks
 
A

Allen Browne

No. By the time the command button's event runs, it has focus, so there is
no chance of then determining where in Screen.PreviousControl the cursor was
previously placed.

Use a toolbar button instead. (Or you could get the same effect from a popup
form.)
 
S

Sprinks

Allen,

Thank you for your response.

Unfortunately, a command button on this continuous form is more convenient.
What do you think of using the OnEnter event of each control (except the
command button, naturally) to write the name or index to a textbox?

Sprinks
 
K

Klatuu

Create a function with a static varialbe:
Private Function WhereWasI() As String
Static strLastAt As String
If Screen.ActiveControl.Name <> "CmdMyCommandButton" Then
strLastAt = Screen.ActiveControl.Name
End If
WhereWasI = strLastAt
End Function

Then in the Enter event (or got focus)
=WhereWasI()

Then in the command button click event:

strLastControl = WhereWasI
 
D

Douglas J. Steele

As Allen pointed out, you can get a reference to the previous control using:

Screen.PreviousControl

I'm assuming that Allen assumed (like me) that "where the cursor last was"
meant the actual x-y coordinates of the cursor, not simply the active
control.
 
K

Klatuu

I know that, but perhaps is because of Allen's post that I thought there was
a problem with it that I was not aware of. Just using Screen.PreviousControl
is better.
 
A

Allen Browne

Right, Sprinks

If you just wanted to know which control had focus before the command
button, Screen.PreviousControl should do. Be sure to use error-handling:
I've always found Screen.PreviousControl a little flakey, so I tend to put
it in a separate function so it doesn't muck up the main one.

Doug is right: I assumed the command button was doing something like,
"Insert boilerplate text at the cursor position in the previous control."
 
Top