Results 1 to 5 of 5

Thread: Rules for TextBoxes

  1. #1

    Thread Starter
    Hyperactive Member vbzero's Avatar
    Join Date
    Aug 2000
    Location
    Vienna
    Posts
    347

    Exclamation

    I have a TextBox and I want the user to enter
    a number between 0 and 6 (1 to 5). No other characters
    are allowed except integer numbers between 0 and 6.
    No floating point numbers or negative numbers and
    no letters. Just 1, 2, 3, 4, 5.
    If the user hits a key which doesn't match with
    these numbers a MsgBox should appear.

    Any ideas?

    thx, vbzero

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Code:
    Text1_KeyPress(KeyAscii As Integer)
    '1 = 49, 5 = 53
    If KeyAscii < 49 Or KeyAscii > 53 Then
    'Display Box
    MsgBox "Only 1,2,3,4,5 are allowed here!"
    'Stop the ASCII from getting passed to the textbox
    KeyAscii = 0
    End If
    End Sub
    Hope it helped ya!


    [Edited by Jop on 09-16-2000 at 09:28 AM]
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You can use the KeyPress event:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case vbKey1 To vbKey5
                'just accept
            Case Else
                KeyAscii = 0
                MsgBox "Invalid character"
        End Select
    End Sub
    Good luck!

  4. #4
    Junior Member
    Join Date
    Oct 1999
    Location
    Sydney NSW Australia
    Posts
    23
    adding this to the change event will only let the user enter in 1, 2, 3, 4 and 5s.

    Code:
    Private Sub Text1_Change()
    If Text1.Text <> "" Then
        If Asc(Mid$(Text1.Text, Len(Text1.Text), 1)) < 49 Or Asc(Mid$(Text1.Text, Len(Text1.Text), 1)) > 53 Then
            Text1.Text = Mid$(Text1.Text, 1, Len(Text1.Text) - 1)
        End If
    End If
    End Sub

  5. #5

    Thread Starter
    Hyperactive Member vbzero's Avatar
    Join Date
    Aug 2000
    Location
    Vienna
    Posts
    347

    Exclamation

    Thanks! It really helped!

    thx, vbzero

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