Results 1 to 6 of 6

Thread: allow numbers only in text box

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 1999
    Location
    Philippines
    Posts
    7
    Where in my code can i insert a msgbox to tell the user that only numbers are allowed? I put the code in keypress but its not working. Where do you think should i put this code?

    Option Explicit
    Private Declare Function GetWindowLong& Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long)
    Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
    Private Const ES_NUMBER = &H2000&
    Private Const GWL_STYLE = (-16)

    Private Sub AccsNo_Keypress()
    Dim tmpValue&
    Dim fAlignment&
    Dim ret&
    Dim flag
    fAlignment& = ES_NUMBER
    tmpValue& = GetWindowLong&(AccsNo, GWL_STYLE)
    If flag Then
    tmpValue& = tmpValue& Or ES_NUMBER
    Else
    tmpValue& = tmpValue& And (Not ES_NUMBER)
    End If
    ret& = SetWindowLong&(AccsNo, GWL_STYLE, tmpValue& Or fAlignment&)
    End Sub


    I badly need a reply soon...

    shei

  2. #2
    Guest
    Weird...try putting it into the LostFocus sub, with the text box.setfocus.

    or simple

    Private Sub Text.LostFocus()

    if not(num(text)) then
    MsgBox "Field must be numeric"
    text.setfocus
    end if

    End Sub

    or Use a masked edit box, and set type to integer or whatever.

    Hope it helps

  3. #3
    New Member
    Join Date
    Feb 2000
    Location
    West Launceston
    Posts
    15
    you can try an isnumeric statement
    I hope this is right its been a while since I've used this statement, and I don't have vb installed on this computer so I don't know if this works, but it should

    Private Sub Text1_Change()
    If IsNumeric(Text1.Text) = False Then
    MsgBox "only numbers are supported"
    End If
    End Sub

  4. #4
    Lively Member
    Join Date
    Mar 2000
    Location
    Germany
    Posts
    84
    Hi,

    I've got a little example here which I used recently.
    Hope it helps you ...

    Nina

    -----------------------------------------------------------
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    'Ignores all Keys that are not numbers or the backspace key
    If (KeyAscii < 48 Or KeyAscii > 57) Then
    If (Not (KeyAscii = vbKeyBack)) Then
    KeyAscii = 0
    ' You might want to add a messagebox here which tells the user that he pressed an invalid key
    End If
    End If
    End Sub

    Private Sub Text1_Validate(KeepFocus As Boolean)
    'Checks if the value of the textbox is
    If Val(Text1) <= 0 Then
    KeepFocus = True
    MsgBox "Number has to be greater than 0", vbOKOnly, "Entry not valid"
    Exit Sub
    End If

    'Checks if no entry was made
    If Text1.Text = "" Then
    KeepFocus = True
    MsgBox "Enter Positve Integer", , "Entry not valid"
    End If
    End Sub

  5. #5
    Member
    Join Date
    Jan 2000
    Location
    Singapore
    Posts
    59
    Hai,
    If you want to restrict only for numeric values even sometimes the IsNumeric function is also not so useful.Since if you use this function you can enter + and - signs also.

    If you want restrict for only numeric values you can use asc(chr) function.

    thanks
    karun

  6. #6
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    You can simply use KeyPress event of the textbox to restrict it to numbers:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not IsNumeric(Chr(KeyAscii)) Then
            KeyAscii = 0
        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