+ Reply to Thread
Results 1 to 12 of 12

? set focus to a textbox when userform opens

Hybrid View

madhg ? set focus to a textbox when... 07-29-2009, 11:36 AM
Leith Ross Re: ? set focus to a textbox... 07-29-2009, 12:43 PM
madhg Re: ? set focus to a textbox... 07-29-2009, 03:58 PM
Leith Ross Re: ? set focus to a textbox... 07-29-2009, 04:02 PM
madhg Re: ? set focus to a textbox... 07-29-2009, 04:21 PM
  1. #1
    Registered User
    Join Date
    04-28-2009
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    38

    ? set focus to a textbox when userform opens

    I'm creating a userform for editing membership records of a small organisation. So I want the focus to default to textbox tboxFind which is used for searching the data.

    I've put tboxFind first in the tab order, and in subroutine Userform_Initialize, the last line is tboxFind.SetFocus

    Using Excel2000 on Windows Vista, it works OK.

    Opening the same workbook in Excel2000 on WinXP, no textbox has the focus when the form opens, but once the user has started to use the form, the focus defaults back to tboxFind, as it should.

    So why is the focus not where it should be when the form opens in WinXP?
    Last edited by madhg; 07-30-2009 at 04:54 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: ? set focus to a textbox when userform opens

    Hello madhg,

    You have to remember that Windows Vista is the first Microsoft operating system to break from the past methodology of backward compatibility. If you need the code to work on previous systems then you should develop the code using the correct Windows version, if possible.

    The UserForm Initialize and Activate events are different. The Initialize event happens only once when the UserForm is being created in memory as an object. The Activate event fires when the UserForm is actually displayed. You can't set focus to window or one of its controls, until it is displayed. Set the focus to your text box in the Activate event.
    Private Sub UserForm1_Activate()
      tboxFind.SetFocus
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-28-2009
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    38

    Re: ? set focus to a textbox when userform opens

    Thanks, Leith, that's very useful. I should have looked up Userform properties, methods and events in ExcelVBA Help!

    However, I can't get it to work. I put the subroutine you suggested into my code, with a Debug.Print command to check that the subroutine was being executed. The subroutine does run (after changing Userform1 to Userform, which is strange since the name of the form is Userform1). But on XP, which is where the workbook has been developed, it does not set focus to tboxFind. When I put a different textbox into subroutine Userform_Activate, it sets the focus to that OK. But not to tboxFind.

    But the other occurences of textboxFind.SetFocus in my code do set the focus whenever the user navigates to another record. The code is unfortunately too long and complicated for me to upload and expect anyone else to look at. I suppose I could try to concoct a heavily cut-down version that demonstrates the same problem, but that would take quite some time.

    Anyway, at least I've learnt about the activate event. Thanks again,
    David

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: ? set focus to a textbox when userform opens

    Hello David,

    If you post your workbook, I'll take a look to see where the problem is.

  5. #5
    Registered User
    Join Date
    04-28-2009
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    38

    Re: ? set focus to a textbox when userform opens

    OK, it's 1.2Mb, too big to upload, but I've put it where you can download it:

    http://www.zen22109.zen.co.uk/madhg.xls

    Many thanks!
    David

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: ? set focus to a textbox when userform opens

    Hello David,

    You are setting the focus to tboxLastName and not to tboxFind.
    Private Sub UserForm_Activate()
      Debug.Print "In Activate"
      tboxLastName.SetFocus
    End Sub
    Change it to tboxFind and it will work.

+ 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