Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move to Control that last had focus 2

Status
Not open for further replies.

StumblingThrough

Technical User
Nov 7, 2002
88
US
What is the best way to move the focus to the control that had the focus last, using VBA. In other words, if the focus is moved from a control to another control, how do you tell Access to remember what control had focus previously so you can move back to it?
 
If you were to give the control to a command button called cmd1, this could would show you which control had focus prior to this.


Private Sub cmd1_GotFocus()
Dim prevCtl As Control
Set prevCtl = Screen.PreviousControl

MsgBox prevCtl.Name

End Sub

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
There is an easier way:

Screen.PreviousControl.SetFocus


HTH
Mike

[noevil]
 
Lonnie / Mgolla,

Thanks. I've never used the SCREEN command before. Screen.PreviousControl.SetFocus works, for the most part. I've got a multi-tabbed form. It's apparently moving focus to the previous control, but it's not displaying the tab on which that control (which now has the focus) is located. Is that a separate command?

 
It probably is moving to last thing that has focus. What is probably happing is this. You are on tab1, you click tab2 then go to the control in question. It is then moving the focus back to the FIRST CONTROL ON THE CURRENT PAGE. The reason is because once you clicked another tab, the focus went to the control on the new tab that has the first tab order. So when you click the control in question, you cursor probably goes to the first text box or command button on the current page.

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
Lonnie,

Actually what's happening is I am programatically sending the focus to a control on another page. When I try to send it back, the previous page does not display.
 
I did a little test with command button 1 on tab 1. I coded it's click event to set the focus to command button 2 on tab 2. Command button 2 has the previous control code. I seem to get an error when sending the focus back to the control that set the focus to the control that has the prev control code.

There is only a short blurp in help about an errors that may occur...


Remarks

You can't use the PreviousControl property until more than one control on any form has received the focus after a form is opened. Microsoft Access generates an error if you attempt to use this property when only one control on a form has received the focus.


Which I could help more...

If you want to go into detail about what you are trying to do, maybe we can help you work around it with this command.

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
I didn't get that error message you were talking about. I did, however, programatically give the focus to an arbitrary control just in case.

Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top