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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for which text box had focus last

Status
Not open for further replies.

daughtery

Programmer
Dec 12, 2006
66
US
I have a form that checks the format of a phone number on the textbox's OnLoseFocus event. I also have a cmdbutton that is used for saving the form data which also validates the data in that textbox. My problem is that if the textbox has focus then I click save and the data in the textbox isn't correct, I get 2 MsgBoxes warning of the incorrect format...one in the OnLoseFocus event of the textbox and one in the onclick of the cmdbutton. Is there a way to check which control on the form had focus last without having to set up a global variable that I set in each of the control's onfocus event? It would be nice if there's some property I could place in a conditional statement inside the cmdButton click event to decide wether or not to display the MsgBox.
 
So, you mean LostFocus and not OnLostFocus, as is a method in a MS Access form along with onclick I think.

Put your Validation in the textbox Validate event.
Make sure the command button's "CauseValidation" property is set to TRUE and it is not the default button, and do not use the LostFocus or Click events for your validation.



 
Thanks thats better but the validate function seems to run twice. Once after I click on save and again when I click on the textbox to correct the data. How can I stop it from running when I click on the textbox?
 
I fixed it, I needed to set the Validate function but set the Causes Validation property to false for the texbox.

Thanks again
 
You might experiment and see if Screen.ActiveControl and/or Screen.PreviousContol could be of use to you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top