Results 1 to 8 of 8

Thread: Changing the default button on my form based on text input

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Changing the default button on my form based on text input

    Hi all. Question.
    I have a default button (cmd1) on my form which works great.. The user hits enter at any time and it is the active button... but I've just added a text field (txt2) and another command button (cmd2) that would only be pressed when text is entered into this new field (txt2). Is there a way to now switch the default to this new cmd2 when text is entered into the txt2?
    Thanks all!
    jk

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Changing the default button on my form based on text input

    You could set the focus to the button

    VB Code:
    1. Private Sub Text2_LostFocus()
    2.    if len(text1.text) > 0 then   cmd2.SetFocus
    3. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Re: Changing the default button on my form based on text input

    Doesn't quite work.. The field will need to accept 4 - 6 digit numbers. Using this code, I input the first number and the focust jumps.. have to click back in the text field, input the second number, the focus jumps again.. etc.

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Changing the default button on my form based on text input

    sorry. i edited it after i noticed that.

  5. #5
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Changing the default button on my form based on text input

    Try this:

    VB Code:
    1. Private Sub Text2_LostFocus()
    2.     If Len(text1.Text) < 4 Or Len(text1.Text) > 6 Then
    3.         cmd2.SetFocus
    4.     End if
    5. End Sub

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Changing the default button on my form based on text input

    You don't need to SetFocus to a specific command button in order to make it the default. Command buttons have a Default property that you can set to True to make it the default. When any given command button's Default property is set to True, all others are automatically set to False.

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    56

    Resolved Re: Changing the default button on my form based on text input

    Thx Martin! I was still having the "jump focus" problem with the other code. This makes it much easier! Should have figured that out on my own! Next time.
    Thx to all who responded.

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Changing the default button on my form based on text input

    Thanks for trying to let us know that your question was answered. However the only way to make the green checkmark show up in the thread list is to edit the first post and add the checkmark there.

Posting Permissions

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



Click Here to Expand Forum to Full Width