+ Reply to Thread
Results 1 to 2 of 2

Excel VBA TextBox navigation problems

  1. #1
    Registered User
    Join Date
    06-10-2010
    Location
    Stuttgart, Germany
    MS-Off Ver
    Excel 2003
    Posts
    4

    Excel VBA TextBox navigation problems

    OK. I'm new to this group, but now I need help!

    There is something fundamentally wrong with either the event handling in Excel VBA text boxes, or my
    • understanding of it.
    • I have been experimenting with this for two days now, and not getting anywhere. Maybe someone in the group can provide me with some insight.
    • I have built a toy application to reduce the problem to its simplest terms:
    • A UserForm1 object with four TextBoxes and a CommandButton;
    • Tab order is TextBox1, TextBox2, SwapButton, Chooser, TextBox3;
    • Initial TextBox values are 001, 002, 003;
    • Each TextBox1..3 has an Entry handler that selects the first n characters, where n is the box number;
    • Chooser is a TextBox whose BeforeUpdate handler sets the focus to TextBoxn, where n is Chooser.Value;
    • Chooser also sets the value of the global object variable objActive to be the chosen TextBox;
    • SwapButton uses objActive to toggle the focus between TextBox1 and TextBox2.
    • SwapButton works entirely as expected: the selection switches between the first char in TextBox1 and the first two chars in TextBox2.
    The behaviour of Chooser is entirely arcane:
    • when the value in Chooser is set to 3, it functions as expected: the selection switches to the first 3 chars of TextBox3;
    • when the value in Chooser is set to 1, the behaviour becomes a little unexpected: TextBox1.SetFocus causes Chooser_BeforeUpdate to run again (re-entered), during which TextBox1_Enter runs before the inner Chooser_BeforeUpdate exits, the outer exits without triggering anything, and selection switches to the first char of TextBox1;
    when the value in Chooser is set to 2, the behaviour becomes very strange:
    • Chooser_BeforeUpdate runs and does a SetFocus on TextBox2,
    • Chooser_BeforeUpdate runs again (re-entered),
    • the inner Chooser_BeforeUpdate exits,
    • TextBox2_Enter runs,
    • the outer Chooser_BeforeUpdate exits (there are now no event handlers running),
    • TextBox1_Enter runs !!! and selects the first char of TextBox1,
    • the selection is set to the entire contents of TextBox1 !!! as if it had been tabbed to;
    • objActive continues to point to TextBox2.
    The only difference I can see is that TextBox1..2 both come before Chooser in the TabOrder, but this turns out not to be the whole story.

    That TextBox3 would be the natural destination on exit from Chooser seems to account for the lack of re-entry of Chooser_BeforeUpdate (confirmed by adding another TextBox in the TabOrder between Chooser and TextBox3), but not for selection of TextBox1. The selection moves to the first 3 chars of TextBox3 as expected.

    In reality, what's happening is that if SetFocus is run on any other control than the next control in the TabOrder, the focus is moved by 2 !!! tabstops from Chooser, after the control to which the focus was moved is Entered.

    Setting Cancel=True in the outer Chooser_BeforeUpdate seems to solve the problem; setting Cancel=True in the inner iteration causes "Run-time error '80004005' Unspecified error", and exit the inner event handler sideways to the outer handler. Not very helpful!

    So, after much BS&T (bitching, speculating, and typing), I seem to have the framework for a workaround in my main program, but I crave an explanation. Am I doing something wrong? Is the Event-handling broken? Did they mean to do that (why?).

    Thanks for any insight,
    FWB

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Excel VBA TextBox navigation problems

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1