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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsNumeric Problem Returning FALSE for 1

Status
Not open for further replies.

GriffMG

Programmer
Mar 4, 2002
6,333
FR
I have just had a problem with IsNumeric()

I'm passing it a field from a record set, which contains just a 4 digit integer numeric such as 1 and the function is returning a False...

Has this got something to do with the value coming from a recordset?

Regards

Griff
Keep [Smile]ing
 
just a 4 digit integer numeric such as 1
I see only ONE digit ...
You may try to remove the spaces:
IsNumeric(Trim(yourVariable))

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PH

Yes, using a trim does certainly make it work properly.

I've done some tests and it only throws a wobbly with the value from a record set... if I feed it a string with leading, training or both leading and trailing spaces the function seems to cope nicely - but the recordset must be returning something I am not expecting... it is only getting one character (as measured by Len()) and it is always returning False!

Hmmm...

Regards

Griff
Keep [Smile]ing
 
Weird. What happens if you try a regular expression? I'm new to them, but I would think this'd only return true for a number with 1 to 4 digits.

Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "^\d{1,4}$"
If regEx(RecordSetVarEqualTo1) = True Then
Wscript.Echo "It works."
Else
Wscript.Echo "It doesn't work."
End If
 
If regEx.Test(RecordSetVarEqualTo1) Then
Wscript.Echo "It works."
Else
Wscript.Echo "It doesn't work."
End If

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
>...but the recordset must be returning something I am not expecting...
Definitely so, otherwise isnumeric() should already taken case of it even without trim(). Such as this.
[tt] isnumeric(" " & vbcr & vbtab & vbcrlf & " 1 ")[/tt]
will return by itself true!

Use escape to discover what it (figuratively rs("x").value below) is and it is important to check this.
[tt] wscript.echo escape(rs("x").value)[/tt]
 
Hi All,

What I have done to workaround the situation is to create a wrapper for CInt(), which is the function I really needed - because I need to do some very simple math with the recordset... previously I was trying to use IsNumeric() to determine if the value was numeric, hence my problem.

So now, I'm using error handling to manage the numbers:

Code:
Function SafeCint(mValue)
	ON ERROR resume next
	mRetVal = 0
	mRetVal = CInt(mValue)
        SafeCint= mRetVal
End Function

Curious problem, simple solution.

Learns something new each day I guess!

Regards

Griff
Keep [Smile]ing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top