• Length of Input String (VB6)

    Author
    Topic
    #416950

    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

    Viewing 2 reply threads
    Author
    Replies
    • #933999

      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) 0 And Len(Text1) < 4 Then

    • #934525

      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. smile

    • #934547

      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

      • #934692

        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

        • #934695

          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.

    Viewing 2 reply threads
    Reply To: Length of Input String (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: