Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for IsNumeric but let the "-" (Hyphen) stay

Status
Not open for further replies.

rtshort

IS-IT--Management
Feb 28, 2001
878
US
I need to check to make sure an entry "IsNumeric" but if a "-" Hyphen is included to seperate the years that will be ok. Like "01-03" is ok but if someone makes a mistake and enters "01-0w" (with a letter) I want to catch it and "Exit Sub" before it's entered in the database. That column in the database is "numeric" so I need to catch it when the "Enter" button is clicked to avoid the SQL Error.

I of course have tried "IsNumeric(NewName)" but obviously that won't work because of the "-"

What do I need to do? I'm brain dead today.

Rob
Just my $.02.
 
Have you tried isdate()...


Craig

"I feel sorry for people who don't drink. When they wake up in the morning, that's as good as they're going to feel all day."
~Frank Sinatra
 
you could try IsNumeric(Replace("01-0w", "-", "")
 
If Not IsNumeric(Replace(NewName, "-", vbNullString)) Then Exit Sub


Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Dang, robtech, you beat me to it! [Smile]

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
LOL...well get quicker on the draw next time Andy..L8R
 
IsDate() is the proper function to use for this IF it is done in the Validate event. You need to still validate if the entry is a VALID date so the IsNumeric is just extra load.

If you are only using Month-Year as in the example, just add "-01" to the end:

If IsDate(Text1.Text & "-01")

If in the KeyPress/Change events, then of course not.

 
CCLINT,

I didn't use IsDate because I understood that "01-03" was meant to refer to a range of years, not a date part, or am I'm misunderstanding the original post?



Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
I think isDate(textbox.text) would just return TRUE if textbox.text was a recognised date
 
Isdate() will return true when passed June 23,2003 or 23 June. So it wouldn't actually limit input to numeric and "-" as requested. Do you really need to limit as stated above or only assure input is date?
[pc2]

Sam
 
I just need to insure that the information entered is a range of years as AndyWatt suggested. I just want a range of 2 digit years and a hyphen to be the only thing allowed.



Rob
Just my $.02.
 
Ok. Now I understand 01-03 means 2001-2003.

This can be easily validated like this:

?Text1.Text LIKE "[8-9,0-1][0-9]-[8-9,0-1][0-9]"
Or
?Text1.Text LIKE "##-##"

Or a variation of it, depending what you want...


'limit the year entry between 80 and 19 meaninng 1980 to 2019. The user can enter one two digit year, or two two digit years divided by a hyphen only.
If Not IsNumeric(Text1.Text) Then
If Not (Text1.Text LIKE "[8-9,0-1][0-9]-[8-9,0-1][0-9]") Then
'Or just:
'If Not Text1.Text LIKE "##-##" Then
'Error here
End If
Else
'Remove this portion if the user MUST enter a From/To range
'If Not (Text1.Text LIKE "[8-9,0-1][0-9]" Then
' 'Error here
'End If
End If

 
another way would be to have the user pick from a combobox hence restricting their entry
 
Hi rtShort,

How about validating user input right when he is typing? Not allowing him to key in chars thats not of interest to you and your program?

Take a look.

Private Function IsValidCharacter(KeyAscii As Integer) As Boolean

Const Numbers$ = "0123456789-."

IsValidCharacter = True
If KeyAscii <> 8 Then
If (InStr(Numbers, Chr$(KeyAscii)) = 0) Then
IsValidCharacter = False
Exit Function
End If
End If

End Function


and in the textboxes keypress event,

Private Sub txtFields_KeyPress(Index As Integer, KeyAscii As Integer)

If Not IsValidCharacter(KeyAscii) Then KeyAscii = 0

End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top