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

Excel Userform VBA - automatically exit or skip through a field

Status
Not open for further replies.

dean12

MIS
Oct 23, 2001
273
0
0
US
Imagine a userform with 3 text controls. We are in text field 1. We type something or we don't. Hit tab and we move to text field 2.

In text field 2 I want to do some work but I don't want to be left there. When I finish working I want to move on to text field 3 without the user having to do anything. In fact I make text field 2 invisible so the user wouldn't even know that it exists.

I have tried using the ENTER event and then after my logic adding: txt3.setfocus

I would prefer to not use the setfocus call - I think it has bad side effects that I don't fully understand.

Any thoughts?
 
A part of the problems can be solved by rethinking event procedures involved, their order, triggers, event chains and interactions. If you leave a textbox (by setting focus to other control for instance), a series of events fire: 'BeforeUpdate', 'AfterUpdate' and 'Exit'. If you coded any of them, you need to indicate that you don't need this code executed.
MS recommends to use 'Change' event to trap user actions.
In this case I would use something like:
Code:
Private Sub TextBox2_Change()
If Len(Me.TextBox2.Text) >= 5 Then Me.Controls("TextBox3").SetFocus
End Sub


combo
 
So if TextBox2 is invisible and the user doesn't need to know that it exists, why does it need to exist at all?

What "work" are you doing?

Why don't you just do your "work" in code, using variables and such as necessary.
 
mintjulep - you would need to read thread: 707-1651274 (Userform controls - SetFocus)


combo - many folks here talk about the CHANGE event. My tests show that this event fires every time the user types or "changes" the value in the control. If that is true when do you know the user has finished and it is time to execute the logic?
 
In text field 2 I want to do some work but I don't want to be left there. When I finish working I want to move on to text field 3 without the user having to do anything.
How do you know that the text field 2 is completed? If there are any validation rules, they can be implemented in TextBox2_Change event procedure. If all is automated, you rather need a label. If the text box has to be conditionally enabled, see the proposal below.

Going back to your thread707-1651274
1. we start with empty StartTime and disabled StopTime,
2. any change in StartTime validates entry and enables/disables StopTime (Change event),
3. tab key skips disabled controls, so you can jump directly to the Duration if StartTime is empty, otherwise you will go to StopTime.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top