Number only in a textbox

C

clinton.lam

Has anyone done this?

I'm trying to make a textbox called, Year, and it should be the
following:
- Allow Null Values
- Must be 4-digits (numbers), no less or not greater.


I tried using this logic:
If Len(Item.UserProperties("Year").Value) > 0 And
Len(Item.UserProperties("Year").Value) < 4 And
IsNumeric(Item.UserProperties("Year") = False Then
msgbox("Invalid. Must be 4-digits and no letters")
End If


For some reason it doesn't go through the IsNumeric() part. So I tried

searching for a Key event method and it's seems like Outlook doesn't
have such a thing... or I could be wrong.


Also, I've tried using a Number field, but it keeps default the value
with 0 which I can't get rid of.


Any suggestions? Any thoughts would be much appreciated.
 
S

Sue Mosher [MVP-Outlook]

Well, for one thing, you need < 5, not 4, and another close parenthesis:

IsNumeric(Item.UserProperties("Year")) = False

That kind of complex If block is too hard to debug for my taste. The way you have it written, all three tests are conducted for every value. Instead, you should only need to conduct all three for entries with 1 to 4 characters. I'd organize it like this:

strYear = Item.UserProperties("Year")
blnIsBad = False
If Len(strYear) > 4 Then
blnIsBad = True
ElseIf Len(strYear) > 0 Then
If Not IsNumeric(strYear) Then
blnIsBad = True
End If
End If
If blnIsBad = True Then
msgbox "Invalid. Must be 4-digits and no letters"
End If

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
N

nabeyaki-udon

Yes, this looks easier. I'll give this a try.
Well, for one thing, you need < 5, not 4, and another close parenthesis:

IsNumeric(Item.UserProperties("Year")) = False

That kind of complex If block is too hard to debug for my taste. The way you have it written, all three tests are conducted for every value. Instead, you should only need to conduct all three for entries with 1 to 4 characters. I'd organize it like this:

strYear = Item.UserProperties("Year")
blnIsBad = False
If Len(strYear) > 4 Then
blnIsBad = True
ElseIf Len(strYear) > 0 Then
If Not IsNumeric(strYear) Then
blnIsBad = True
End If
End If
If blnIsBad = True Then
msgbox "Invalid. Must be 4-digits and no letters"
End If

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
N

nabeyaki-udon

Okay. So I did some tweaking to what you gave me and it seems to be
work correctly... At least I think it does.

strYear = Item.UserProperties("Year").Value
blnIsYear = False

If Len(strYear) > 0 and Len(StrYear) < 4 Then
blnIsYear = True
ElseIf Len(strYear) > 0 Then
If Not IsNumeric(strYear) Then
blnIsYear = True
End If
End If

If blnIsYear = True Then
msbox("Year must be 4-digits and no letters")
End If

Thanks for your input, Sue.
 
S

Sue Mosher [MVP-Outlook]

You might want to rename your Boolean flag to something like blnIsNotAYear to make the code more readable.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
N

nabeyaki-udon

Also, thanks helping me out with the logic. I didn't know you could do
nested if's in VBScript.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top