Results 1 to 12 of 12
  1. #1
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Text Box Exit (VB 6.0 / W98)

    Is there a way to tell when the max number of characters have been entered into a text box without the operator pressing Enter or clicking or some other action? For example, I have a text box with the max number of characters set to 4. I would like the program to automatically exit the box when the fourth character is entered so that I can set the focus elsewhere.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Text Box Exit (VB 6.0 / W98)

    Hi Chuck,

    Use something like the following in the OnChange event of the text box:

    Private Sub Text1_Change()
    If Len(Text1.Text) >= 4 Then
    Text1.Text = Left(Text1.Text, 4)
    Text2.SetFocus
    End If
    End Sub

    The first line after the If...Then ensures that the user can't enter more than 4 characters. The second line moves the focus somewhere else.

  3. #3
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Text Box Exit (VB 6.0 / W98)

    Hi Hans,

    Your suggestion doesn't quite work because it exits the Sub as soon as the first character is entered (i.e. the first Change occurs). So, it looks like one can't use the Private Sub Text1_Change() event. Is there some other event that can be triggered upon entering a certain number of characters in a text box?

    My only other thought would be to come up with a loop that upon entering a character in the text box it redisplays that character in the box and then puts the focus back in the box for the next entry (change) and exit the loop after the fourth character. However, I'm not sure you can enter a text box and place the insertion point anywhere but at the beginning of the box which would defeat this idea.

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Text Box Exit (VB 6.0 / W98)

    Did you actually try Hans suggestion. It looks like it ought to work.

    The first time you enter a character in the text box, the change event will occur and the sub will run, it immediately exits because there is only one character in the text box. The next time you enter a character it runs again and immediately exits again, when you enter the fourth character it will move you to the next field.

    The nice detail in the code is that if the user pastes something longer than 4 characters into the text box then it will be truncated to 4 characters.

    StuartR

  5. #5
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Text Box Exit (VB 6.0 / W98)

    Hi Stuart and Hans,

    Thanks to both of you as it works just like Hans said it would. My problem was that I was using a variable instead of a hard coded 4 for the number of digits. But that particular variable was defined within another Sub, so it wasn't picked up in the Change Sub. it gave me fits for a while, but once I defined the variable as global (within the form I'm working with) everything works great!

    I should have spotted it earlier. Thanks again.

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Text Box Exit (VB 6.0 / W98)

    The AutoTab and MaxLength textbox properties are a thrilling alternative to code. Or code up one-liners:

    me.textbox1.autotab = true
    me.textbox1.maxlength = 4
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  7. #7
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Text Box Exit (VB 6.0 / W98)

    Hi Kevin,

    I found the AutoTab property in VBA for Access, but it doesn't show up as a text box property in my copy of VB 6.0. However, thanks for the suggestion, it may prove useful in the future.

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Text Box Exit (VB 6.0 / W98)

    Ah, you're right. Not in vb6.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Text Box Exit (VB 6.0 / W98)

    Chuck, just add Microsoft forms 2.0 controls to your toolbox. Now you can use a "real" textbox. HTH --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  10. #10
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Text Box Exit (VB 6.0 / W98)

    Hi Sammy,

    I only found Microsoft Forms 2.0 objects in VBA for Access, but not in VB 6.0.

    What's different about Forms 2.0 from those that are already present in VB and VBA?

  11. #11
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Text Box Exit (VB 6.0 / W98)

    Assuming that you have MS Office on your VB computer, you can right-click on the VB toolbox, select components, and scroll-down to Microsoft Forms 2.0. Check this box, press OK, and 14 new controls will be added to your toolbox. Now you can use either the VB textbox at the top (yuk!) or the Office textbox (yum!) at the bottom. The only drawback to the Forms 2.0 controls is that you cannot distribute your application to non-Office users. HTH --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  12. #12
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Text Box Exit (VB 6.0 / W98)

    Thanks Sammy,

    I'll have to give it a try.

Posting Permissions

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