Link to home
Start Free TrialLog in
Avatar of indra_putra
indra_putra

asked on

Input box

how to write code so that user only allowed to type number  the textbox inside  the inputbox, thank you
Avatar of bhnv9
bhnv9

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not (KeyAscii > 48 And KeyAscii < 57) Then
        KeyAscii = 0
    End If
End Sub
u mean the user can't even type a number to the input box
if so, i also dun know
but if u mean that the input box can accept numbers but a error message box pop up then i may be can help u.
Sorry, didn't realize you wrote inputbox. That code is for a textbox.
You could create your own inputbox as a small modal form and then you can use the textbox keyPress code.
hi bhnv9
indra_putra is asking for INPUT BOX NOT TEXT BOX
please take note the different between text box and input box
Hi Indra Putra :~)

Even if the user inputs number into the input box, you are bound to receive it as a string only. Because the input box has been designed to return a string value and nothing else. If you leave the textbox blank you will receive a blank string (""). So as far as I know you cant compell the user to enter only numbers in an inputbox.

You can check the value after the user enters something in the inputbox.

The other way is to create a dialog box with only a textbox in the form and then trap the keys.

Regards
Madhur
If you want to avoid user from imputting number in realtime, better you build your own inputbox (as modal form...)
But youcan also have a look here to see if this can be modified to suite your needing:
http://www.codeguru.com/vb/articles/2013.shtml
Private Sub Text1_KeyPress(KeyAscii As Integer)
   If Not (KeyAscii > 48 And KeyAscii < 57) Then
       text1.text = left$(text1.text, len(text1.text)-1)
   End If
End Sub

this will remove the last character they pressed if it was not a number
ASKER CERTIFIED SOLUTION
Avatar of Steiner
Steiner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        ' allow following keys
        Case vbKey0 To vbKey9:
        Case vbKeyDecimal:  ' if you want decimal numbers
        Case Asc("-"):  ' negative numbers
        Case vbKeyReturn, vbKeyEscape, vbKeyDelete, vbKeyBack:
        ' do not allow other keys
        Case Else
            KeyAscii = 0
    End Select
End Sub


Good Luck!
I guess you have to write your own InputBox form.  Try to implement the same.

create a form called frmInputBox
Add Label Control and TextBox into it.
add code below to the function

Option Explict

Public retVal as String

Private Sub Text1_KeyPress(KeyAscii As Integer)
   Select Case KeyAscii
       ' allow following keys
       Case vbKey0 To vbKey9:
       Case vbKeyDecimal:  ' if you want decimal numbers
       Case Asc("-"):  ' negative numbers
       Case vbKeyReturn, vbKeyEscape, vbKeyDelete, vbKeyBack:
       ' do not allow other keys
       Case Else
           KeyAscii = 0
   End Select
End Sub

Private Sub cmdOK_Click()
    retVal = Text1.Text
    Unload Me
End Sub

Private Sub cmdCancel_Click()
    retVal = ""
    Unload Me
End sub

' create a new module and add following code
Public Function Inputbox2(prompt As String, Optional title As String = "", Optional default As String = "")
    With frmInputbox
        .Caption = title
        .label1.Caption = prompt
        .Text1.Text = default
        .Show 1
        Inputbox2 = .retVal
    End With
End Function

' now you could call your InputBox
myStr = InputBox2("Enter Value:", "Value?", "0.00")

Good Luck!
Avatar of indra_putra

ASKER

thank you, and also thanks for all who have tried to help me