?
Solved

Exclude alpha entries in text box?

Posted on 2003-03-04
10
Medium Priority
?
304 Views
Last Modified: 2012-06-27
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
Comment
Question by:KarenMS
[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
10 Comments
 
LVL 2

Expert Comment

by:Jacamar
ID: 8066441
Private sub Text1_Change
if Text1.text < 0 then
Call msgbox ("Value must be positive", vbinformation
Text1.text = ""
end if
end sub
0
 

Author Comment

by:KarenMS
ID: 8066461
will the msgbox show up if the input is alpha?  I want to prevent the user from entering anything other than a number
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 8066468
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:Jacamar
ID: 8066476
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
 
LVL 4

Expert Comment

by:trkcorp
ID: 8068050
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
 
LVL 11

Accepted Solution

by:
supunr earned 200 total points
ID: 8068203
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
 

Author Comment

by:KarenMS
ID: 8068268
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
 

Author Comment

by:KarenMS
ID: 8068373
Informative, and the code example will help big time.  This is exactly what I was looking for.
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8068682
This example allows the "n" key to be pressed for some reason.  Can you explain that?
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8068701
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

741 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