Results 1 to 3 of 3

Thread: Limiting # of Characters

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Posts
    55
    Situation: The program runs and the user is instructed to enter his or her name in a textbox(txtNAME)and then hit ok (cmdOK).

    What code would I put in for cmdOK so that if 10 or more characters are entered in txtNAME a message box saying "Your name is too long" appears?




    If you can help me out I would appreciate it.

    Thanks in advance


    Beres

    [email protected]
    Beres

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    'send a msgbox if maxlength is reached...


    Private Sub Text1_KeyPress(KeyAscii As Integer)

    ' dispose of the control keys

    If KeyAscii < 32 Then Exit Sub

    'set the max length of textbox

    text1.maxlength = 10

    If Len(Text1.Text) = 10 Then
    MsgBox "Sorry, 10 characters is all you get."
    End If
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Guest
    Two ways of doing this:

    Either set the MaxLength property to 10.

    Code:
    Private Sub Form_Load()
    Text1.MaxLength = 10
    End Sub
    Or:

    Code:
    Private Sub Text1_Change()
    If Len(Text1.Text) = 10 Then
    Text1.Locked = True
    End If
    End Sub

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