?
Solved

Input box

Posted on 2003-03-18
11
Medium Priority
?
174 Views
Last Modified: 2011-11-27
how to write code so that user only allowed to type number  the textbox inside  the inputbox, thank you
0
Comment
Question by:indra_putra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +5
11 Comments
 

Expert Comment

by:bhnv9
ID: 8157801
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not (KeyAscii > 48 And KeyAscii < 57) Then
        KeyAscii = 0
    End If
End Sub
0
 

Expert Comment

by:tat83
ID: 8157802
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
 

Expert Comment

by:bhnv9
ID: 8157812
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:tat83
ID: 8157818
hi bhnv9
indra_putra is asking for INPUT BOX NOT TEXT BOX
please take note the different between text box and input box
0
 
LVL 1

Expert Comment

by:hmadhur
ID: 8157824
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
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8157827
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
 
LVL 1

Expert Comment

by:hazgod
ID: 8158360
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
 
LVL 11

Accepted Solution

by:
Steiner earned 200 total points
ID: 8158955
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
 
LVL 11

Expert Comment

by:supunr
ID: 8163112

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
 
LVL 11

Expert Comment

by:supunr
ID: 8163177
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
 

Author Comment

by:indra_putra
ID: 8163866
thank you, and also thanks for all who have tried to help me
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question