Hello Shankar,
The code in your userform detects when TextBox2 gets the focus and displays
a message before using SetFocus to return to TextBox1. Since the code is in
an "event" procedure for TextBox2, it only executes when the user tries to
enter TextBox2 -- not when they try to enter TextBox3 or any future controls
you might add to the form.
Rather than have an Enter event procedure for every control on the form
other than TextBox1, you can use the Exit event for TextBox1. No matter
what control the user clicks on next, your code can still execute.
To use this method, delete the existing code from your userform, and replace
it with this:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If ActiveControl = "" Then
MsgBox "Mandatory Field", vbInformation + vbOKOnly, "Alert"
Cancel = True
End If
End Sub
Notice that this code uses "Cancel = True" to prevent the user from leaving
the control. In an event procedure with "Cancel" mentioned in it's
delcaration (the line beginning "Private Sub"), you can use a statement like
"Cancel = True" to prevent the "event" from taking place. Event procedures
execute immediately before the action described by the event takes place.
In this case, the event is called "Exit", so your code will execute before
the control is actually exited (but after the user clicked the mouse on
another control or hit the tab or enter keys).
Asa
From: [email protected] [mailto:[email protected]]
On Behalf Of Shankar Bheema
Sent: Tuesday, February 21, 2012 12:48 AM
To: [email protected]; NOORAIN ANSARI
Subject: $$Excel-Macros$$ doubt with setfocus
Dear Experts
I am enclosing the attachment which was solved by NOORAIN. but when I add
third textbox to the userform, the code is not working. Please guide me.
regards
shankar sb
--
FORUM RULES (986+ members already BANNED for violation)
1) Use concise, accurate thread titles. Poor thread titles, like Please
Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will
not get quick attention or may not be answered.
2) Don't post a question in the thread of another member.
3) Don't post questions regarding breaking or bypassing any security
measure.
4) Acknowledge the responses you receive, good or bad.
5) Cross-promotion of, or links to, forums competitive to this forum in
signatures are prohibited.
NOTE : Don't ever post personal or confidential data in a workbook. Forum
owners and members are not responsible for any loss.
----------------------------------------------------------------------------
--------------------------
To post to this group, send email to [email protected]
--
FORUM RULES (986+ members already BANNED for violation)
1) Use concise, accurate thread titles. Poor thread titles, like Please Help,
Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get
quick attention or may not be answered.
2) Don't post a question in the thread of another member.
3) Don't post questions regarding breaking or bypassing any security measure.
4) Acknowledge the responses you receive, good or bad.
5) Cross-promotion of, or links to, forums competitive to this forum in
signatures are prohibited.
NOTE : Don't ever post personal or confidential data in a workbook. Forum
owners and members are not responsible for any loss.
------------------------------------------------------------------------------------------------------
To post to this group, send email to [email protected]