Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Nov 2004
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Length of Input String (VB6)

    Hi

    I would like to count the number of string characters entered in a textbox and make sure that the user enters at least 4.

    Appreciate that this is very basic. I don't know what is worse. The embarrassment that I'm having to ask this question, or the frustration that I can't get my code to work!

    Many thanks

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

    Re: Length of Input String (VB6)

    You can use the LostFocus event of the text box, and the Len function:

    Private Sub Text1_LostFocus()
    If Len(Text1) < 4 Then
    Me.Text1.SetFocus
    End If
    End Sub

    where Text1 is the name of the text box. If you want to allow the user to clear the text box, change

    If Len(Text1) < 4 Then

    to

    If Len(Text1) > 0 And Len(Text1) < 4 Then

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Length of Input String (VB6)

    Another approach I've seen is to keep the action buttons disabled unless and until there are at least 4 characters in a field. This is done, I believe, bu using a .Change event; if there is no such event, it might be done by watching the keyboard (e.g., keydown/keyup type events). This seems like tricky code to write; it might already be "out there" for study and downlaoding on VB boards. That's a big advantage of working with an "old" language like VB. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Length of Input String (VB6)

    Use the Validate event of the textbox to count the length of the string and put up a message if it's too short and set Cancel = True to keep them in the textbox. Validate is similar to BeforeUpdate in Access. It happens before the control actually loses focus and it has a cancel argument .

    Private Sub Text1_Validate(Cancel As Boolean)

    Dim intLen As Integer

    intLen = Len(Text1.Text)
    If intLen < 4 Then
    Cancel = True
    MsgBox "You must enter at least 4 characters."
    End If

    End Sub
    Charlotte

  5. #5
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Length of Input String (VB6)

    Hi

    Apologies for the delay in replying. And for all your suggestions. Unfortunately, I am still having difficulties. This is what I am actually trying to achieve:

    I want a user to enter a minimum of 4 characters in a string which is to contain the values A-Z ^(hat) - (hypen) only. If an incorrect character is input I would like it removed but leave the rest of the string intact. Oh and also display a message box to say what is going on if validation fails.

    At present I am trying to do this using two Event Procedures: Hans V's LostFocus Event along with the KeyPress Select Case validation he has given me on a previous occasion

    Private Sub txtInput_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    Case 45, 65 To 90, 94
    ' allowed
    Case Else
    ' not allowed
    KeyAscii = 0
    End Select
    End Sub

    Adding MsgBoxes and attempting to use a RIGHT function to remove the last character in the String in my text box at various points in the code. I cannot get this all to work together properly.

    I suppose it would have made more sense to ask for exactly what I wanted from the word go. However I really do try to work these things out for myself before screaming for help.

    Thanks

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

    Re: Length of Input String (VB6)

    Using code for the KeyPress event and the LostFocus event should do what you want. Why are you trying to remove the last character? The KeyPress event should prevent the user from ever entering a wrong character.

  7. #7
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Length of Input String (VB6)

    Hi

    Excellent question... I think I've managed to confuse myself somewhere along the line. Still good news. It now works fine.

    Thanks for your patience.

  8. #8
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Length of Input String (VB6)

    The thread Limiting textbox input (VBA Office 2000) might provide some useful ideas too.

    Alan

Posting Permissions

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