[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

Exclude alpha entries in text box?

I'm working on a program for class (novice programmer here) - I'm entering in and out times to calculate hours worked.  What I'm doing right now is having a MsgBox pop up when one of the boxes in the array looses focus if the input is a negative value.  But I want to prevent the user from being able to enter alpha entries in the txt boxes.  

I had the idea of excluding the ASCII values of characters other than numeric, but I'm not sure how to do it.

Any suggestions?
0
KarenMS
Asked:
KarenMS
1 Solution
 
JacamarCommented:
Private sub Text1_Change
if Text1.text < 0 then
Call msgbox ("Value must be positive", vbinformation
Text1.text = ""
end if
end sub
0
 
KarenMSAuthor Commented:
will the msgbox show up if the input is alpha?  I want to prevent the user from entering anything other than a number
0
 
AzraSoundCommented:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 Then
        KeyAscii = 0
    End If
End Sub


That will allow only numbers and the delete/backspace key.  If you need support for commas or decimals, you can include them as well.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JacamarCommented:
or

Private sub Text1_Change
If Not IsNumeric(Text1.Text) Then
    BoolAccept = False
End If


If IsNumeric(Text1.Text) Then
   BoolAccept = True
End If

If IsNumeric(Text1.Text) Then
   If Text1 <= 0 Then
       BoolAccept = False
   End If
End If

if BoolAccept = False then
Call msgbox("blah blah", vbinformation)
Text1.text = ""
end if

end sub
0
 
trkcorpCommented:
Azrasound has the most common method, that's the way I filter characters.  You could also switch from a text box to a masked edit box, it will do this for you.  And lastly, you can validate a time with the IsDate function like this:
Dim y As String
y = "10:00"
If IsDate(y) Then
   MsgBox "Time is good."
Else
   MsgBox "Time is bad!"
End If
0
 
supunrCommented:
just add the following code to KeyPress Event.  This will stop all keys but the numbers and ".".  Also don't forget other keys such as enter, delete, backspace and escape keys.

Private Sub Text1_KeyPress(KeyAscii As Integer)
     Select case KeyAscii
             Case vbKeyDelete, vbKeyReturn, vbKeyBack, vbKeyEscape: ' Do Nothing
             Case vbKey0 To vbKey9: ' Do Nothing
                Case vbkeydecimal: ' "."
          Case Else
               KeyAscii = 0    ' disable the key
               Beep     ' Optional
     End Select  
End Sub

Good LUck!!!
0
 
KarenMSAuthor Commented:
Thanks!  These are all wonderful, and UNDERSTANDABLE.  I won't have a chance to try them out until I get home tonight.  Again, thank you everybody.  I couldn't find anything in the MSDN online library, and I only have the student verson of VB 6.0 - so I don't have a help file.  You are all life-savers. :o)
0
 
KarenMSAuthor Commented:
Informative, and the code example will help big time.  This is exactly what I was looking for.
0
 
JacamarCommented:
This example allows the "n" key to be pressed for some reason.  Can you explain that?
0
 
JacamarCommented:
this example for some reason takes in the "n" key to be KeyAscii = 110, which is also the value of vbKeydecimal.  However, it also allows the decimal to be pressed.....................very strange.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now