Consulting

Results 1 to 6 of 6

Thread: Blocking Alphabetical Entries into Textboxes

  1. #1
    VBAX Regular Shrout1's Avatar
    Joined
    Sep 2004
    Location
    Maryland, USA.
    Posts
    37
    Location

    Blocking Alphabetical Entries into Textboxes

    There is an error on my form when a user makes an alphabetical entry into one of the text fields. I have it set to automatically run a calculation subroutine when all the required fields are filled in and so this is giving me grief.

    Any help would be greatly appreciated! Thanks!

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hello Shrout,

    Do you mean UserForm? If so, are you using a TextBox? Is the action from a CommandButton? If so, what is your CommandButton code and name of all objects in this association?

  3. #3
    VBAX Regular Shrout1's Avatar
    Joined
    Sep 2004
    Location
    Maryland, USA.
    Posts
    37
    Location
    Yes - I'm using a userform and a textbox.

    It isn't tied to a command button, it's tied to a textbox_Change routine. Basically, the value that is typed into the textbox is stored as a variable and then a sub is called up that checks if the other textboxes are filled.

    If the others have values then the program starts doing math - something that letters will mess up :-p. I was hoping there was an easy way to lock a textbox from letters, but I've never heard of one...

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Do a check as the numbers/text is entered
    MD

    eg

    Private Sub TextBox1_Change()
    If Not IsNumeric(TextBox1) Then MsgBox "Numbers Only"
    End Sub

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    This will only allow numbers 0-9:


    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii < 48 Or KeyAscii > 57 Then
    KeyAscii = 0
    Beep
    End If
    End Sub

    This will allow one decimal:


    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Dim x As Integer
    Dim y As String
    If KeyAscii < 46 Or KeyAscii > 57 Or KeyAscii = 47 Then
    KeyAscii = 0
    Beep
    End If
    If KeyAscii = 46 Then
    y = TextBox1.Text
    For x = 1 To Len(y)
    If Mid(y, x, 1) = "." Then KeyAscii = 0
    Next x
    End If
    End Sub

  6. #6
    VBAX Regular Shrout1's Avatar
    Joined
    Sep 2004
    Location
    Maryland, USA.
    Posts
    37
    Location
    Thanks! I'll give this 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
  •