Input box

how to write code so that user only allowed to type number  the textbox inside  the inputbox, thank you
indra_putraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bhnv9Commented:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not (KeyAscii > 48 And KeyAscii < 57) Then
        KeyAscii = 0
    End If
End Sub
0
tat83Commented:
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.
0
bhnv9Commented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

tat83Commented:
hi bhnv9
indra_putra is asking for INPUT BOX NOT TEXT BOX
please take note the different between text box and input box
0
hmadhurCommented:
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
0
CimperialiCommented:
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
0
hazgodCommented:
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
0
SteinerCommented:
If you don't need to check whether there are numbers at runtime but when the user clicked Ok, then use a simlpe loop:

Sub Test()
   Dim Txt As String
   
   Do
      Txt = InputBox("Please enter a number!")
   Loop Until IsNumeric(Txt) Or Txt = ""
   
   If Txt <> "" Then
      MsgBox "It's a number!"
   Else
      MsgBox "You entered nothing. Aborting.."
   End If
End Sub

VB will show the inputbox again and again and... until the user enters data which VB can convert to a number. If the user enters nothing or presses cancel then txt is "" and the program continues.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
supunrCommented:

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!
0
supunrCommented:
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!
0
indra_putraAuthor Commented:
thank you, and also thanks for all who have tried to help me
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.