If statement involving cursor possition

M

Michalis J.

If the cursor is on a certain textbox when I press a command button I want
the system to perform a certain task.
However how can I check in VBA if the cursor is on that textbox?
Thanks in advance

Michalis
 
K

Klatuu

Screen.ActiveControl.Name
will return the name of the control where the cursor is located.
 
M

Marshall Barton

Michalis said:
If the cursor is on a certain textbox when I press a command button I want
the system to perform a certain task.
However how can I check in VBA if the cursor is on that textbox?


If Screen.PreviousControl.Name = "textboxname" Then
 
M

Michalis J.

If the cursor is on a textbox in a subform and the command button on the main
form is this still supposed to work?
 
M

Marshall Barton

No. The focus must move back to the main form when you
click the command button so the "cursor" is no longer on
your text box. PreviousControl will work if both controls
are on the same form, but the subform control on the main
form is the previous control when the main form button
becomes the active control.

However, you can use the subform control's Exit event to
save the name of the Active control.
Me.hiddentextbox = Screen.ActiveControl.Name
Then the button can check the saved name against the certain
text box:
If Me.hiddentextbox = "certaintextboxname" Then
 
M

Michalis J.

Thanks a lot Barton. It works just fine.
Michalis

Marshall Barton said:
No. The focus must move back to the main form when you
click the command button so the "cursor" is no longer on
your text box. PreviousControl will work if both controls
are on the same form, but the subform control on the main
form is the previous control when the main form button
becomes the active control.

However, you can use the subform control's Exit event to
save the name of the Active control.
Me.hiddentextbox = Screen.ActiveControl.Name
Then the button can check the saved name against the certain
text box:
If Me.hiddentextbox = "certaintextboxname" Then
--
Marsh
MVP [MS Access]

If the cursor is on a textbox in a subform and the command button on the main
form is this still supposed to work?
 
Top