Hi all,

I have the following code which works fine as a test:

If Not Text1.Text = 5 Then
Text2.Text = ""

But instead of 5 I need it to be any positive number. I can exclude a blank
field and negative numbers, but I can't figure out how to exclude letters or
other characters.

Can anyone tell me what a simple solution would be?

Thanks,

Andy

Re: Selecting 'any number' by Jan

Jan
Thu Jan 08 06:53:26 CST 2004

"Andy" <andy@joininthechant.com>'s wild thoughts were
released on Thu, 8 Jan 2004 12:30:47 -0000 bearing the
following fruit:

>Hi all,
>
>I have the following code which works fine as a test:
>
>If Not Text1.Text = 5 Then
>Text2.Text = ""
>
>But instead of 5 I need it to be any positive number.

A simple (perhaps too simple for your needs) is;

If val(text1.text) <=0 then

J

>I can exclude a blank
>field and negative numbers, but I can't figure out how to exclude letters or
>other characters.
>
>Can anyone tell me what a simple solution would be?
>
>Thanks,
>
>Andy
>


--
There was the ship carrying red paint that collided with another carrying blue paint.
Both crews were marooned.

[Abolish the TV Licence - http://www.tvlicensing.biz/]


Re: Selecting 'any number' by Andy

Andy
Thu Jan 08 07:45:40 CST 2004

Jan, that's perfect!

Cheers,

Andy



"Jan Hyde" <StellaDrinker@REMOVE.ME.uboot.com> wrote in message
news:5pkqvvso8t8tl44o1lndp3f630ivegoda1@4ax.com...
> "Andy" <andy@joininthechant.com>'s wild thoughts were
> released on Thu, 8 Jan 2004 12:30:47 -0000 bearing the
> following fruit:
>
> >Hi all,
> >
> >I have the following code which works fine as a test:
> >
> >If Not Text1.Text = 5 Then
> >Text2.Text = ""
> >
> >But instead of 5 I need it to be any positive number.
>
> A simple (perhaps too simple for your needs) is;
>
> If val(text1.text) <=0 then
>
> J
>
> >I can exclude a blank
> >field and negative numbers, but I can't figure out how to exclude letters
or
> >other characters.
> >
> >Can anyone tell me what a simple solution would be?
> >
> >Thanks,
> >
> >Andy
> >
>
>
> --
> There was the ship carrying red paint that collided with another carrying
blue paint.
> Both crews were marooned.
>
> [Abolish the TV Licence - http://www.tvlicensing.biz/]
>



Re: Selecting 'any number' by Rick

Rick
Thu Jan 08 08:08:12 CST 2004

> I have the following code which works fine as a test:
>
> If Not Text1.Text = 5 Then
> Text2.Text = ""
>
> But instead of 5 I need it to be any positive number. I can exclude a
blank
> field and negative numbers, but I can't figure out how to exclude letters
or
> other characters.
>
> Can anyone tell me what a simple solution would be?

Here are a couple of functions that I've posted in the past. The first one
provides the functionality you asked about (the second one is for possible
future use). Simply use it like this...

If IsDigitsOnly(Text1.Text) Then
' The number consists only of digits
' so do what you want here
End If

Rick - MVP

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Len(Value) > 0 And _
Not Value Like "*[!0-9]*"
End Function

Function IsNumber(ByVal Value As String) As Boolean
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function



Re: Selecting 'any number' by Syed

Syed
Thu Jan 08 09:57:40 CST 2004


"Andy" <andy@joininthechant.com> wrote in message
news:btjiho$np7$1@thorium.cix.co.uk...
> Hi all,
>
> I have the following code which works fine as a test:
>
> If Not Text1.Text = 5 Then
> Text2.Text = ""
>
> But instead of 5 I need it to be any positive number. I can exclude a
blank
> field and negative numbers, but I can't figure out how to exclude letters
or
> other characters.

Let me tell you what I understood from your question. You want to check the
text field for positive numbers. If it does not contain any such numbers you
want to do something; let's say clear its contents.
For this purpose try this.

If IsNumeric(Text1.Text) = False Or Not CDbl(Text1.Text) > 0 Then
Text1.Text=""

Hope this helps!
--
Syed Zeeshan Haider.
http://szh.20m.com/


-----------------------------------
Allah says to Mankind:
"Then which of the favours of your Lord will ye deny?"



Re: Selecting 'any number' by Rick

Rick
Thu Jan 08 10:52:04 CST 2004

> For this purpose try this.
>
> If IsNumeric(Text1.Text) = False Or Not CDbl(Text1.Text) > 0 Then
> Text1.Text=""

From a previous post of mine...

I usually try and steer people away from using IsNumeric to "proof"
supposedly numeric text. Consider this (also see note at end of post):

ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")

Most people would not expect THAT to return True. IsNumeric has some "flaws"
in what it considers a proper number and what most programmers are looking
for.

I had a short tip published by Pinnacle Publishing in their Visual Basic
Developer magazine that covered some of these flaws. Originally, the tip was
free to view but is now viewable only by subscribers.. Basically, it said
that IsNumeric returned True for things like -- currency symbols being
located in front or in back of the number as shown in my example (also
applies to plus, minus and blanks too); numbers surrounded by parentheses as
shown in my example (some people use these to mark negative numbers);
numbers containing any number of commas before a decimal point as shown in
my example; numbers in scientific notation (a number followed by an upper or
lower case "D" or "E", followed by a number equal to or less than 305 -- the
maximum power of 10 in VB); and Octal/Hexadecimal numbers (&H for
Hexadecimal, &O or just & in front of the number for Octal).

NOTE:
======
In the above example and in the referenced tip, I refer to $ signs and
commas and dots -- these were meant to refer to your currency, thousands
separator and decimal point symbols as defined in your local settings --
substitute your local regional symbols for these if appropriate.

As for your question about checking numbers, here are two functions that I
have posted in the past for similar questions..... one is for digits only
and the other is for "regular" numbers:

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Len(Value) > 0 And _
Not Value Like "*[!0-9]*"
End Function

Function IsNumber(ByVal Value As String) As Boolean
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Rick - MVP



Re: Selecting 'any number' by Larry

Larry
Thu Jan 08 11:17:49 CST 2004

"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote

> Function IsNumber(ByVal Value As String) As Boolean
> ' Leave the next statement out if you don't
> ' want to provide for plus/minus signs
> If Value Like "[+-]*" Then Value = Mid$(Value, 2)
> IsNumber = Not Value Like "*[!0-9.]*" And _
> Not Value Like "*.*.*" And _
> Len(Value) > 0 And Value <> "." And _
> Value <> vbNullString
> End Function


Just out of curiosity, does this pass muster?

If Value Like "[+-]*" Then Value = Mid$(Value, 2)
Test = Replace(Value, ".", "", 1, 1)
IsNumber = (Not Test Like "![0-9]*") And Len(Test)

What was the significance for using both Len > 0 and
Value <> vbNullString ?

LFS




Re: Selecting 'any number' by Rick

Rick
Thu Jan 08 11:26:10 CST 2004

> Just out of curiosity, does this pass muster?
>
> If Value Like "[+-]*" Then Value = Mid$(Value, 2)
> Test = Replace(Value, ".", "", 1, 1)
> IsNumber = (Not Test Like "![0-9]*") And Len(Test)
>
> What was the significance for using both Len > 0 and
> Value <> vbNullString ?

Yes, your modification works fine. The problem was that I grabbed an older
copy of my code to post as an answer. I used to post it in the way I did in
my answer to Andy and someone else pointed out the same thing you did back
then. I changed it in subsequent posts; but since I pick up old posts of
mine by searching my Sent folder using keywords that I remember, I ended up
opening an older email without thinking. What I posted won't hurt (it works
fine), but the "double test" is unnecessary as you have pointed out.

Rick - MVP



Re: Selecting 'any number' by Syed

Syed
Thu Jan 08 23:49:57 CST 2004

"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:uZAh9eg1DHA.2700@TK2MSFTNGP11.phx.gbl...
> > For this purpose try this.
> >
> > If IsNumeric(Text1.Text) = False Or Not CDbl(Text1.Text) > 0 Then
> > Text1.Text=""
>
> From a previous post of mine...
>
> I usually try and steer people away from using IsNumeric to "proof"
> supposedly numeric text. Consider this (also see note at end of post):
>
> ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")
>
> Most people would not expect THAT to return True. IsNumeric has some
"flaws"
> in what it considers a proper number and what most programmers are looking
> for.

Thanks Rick! Mistakes are necessary for learning. That's what I have done
(unintentionally).

Thanks again!
--
Syed Zeeshan Haider.
http://szh.20m.com/


-----------------------------------
Allah says to Mankind:
"Then which of the favours of your Lord will ye deny?"



Re: Selecting 'any number' by Andy

Andy
Fri Jan 09 06:41:50 CST 2004

Rick, as this was a bulletproofing exercise the 'hidden' flaws in IsNumeric
are fascinating. I'll confess my programming knowledge is too basic at the
moment to fully understand your example (in terms of figuring out what it
does), but it'll be worth looking into.

Cheers,

Andy



"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:uZAh9eg1DHA.2700@TK2MSFTNGP11.phx.gbl...
> > For this purpose try this.
> >
> > If IsNumeric(Text1.Text) = False Or Not CDbl(Text1.Text) > 0 Then
> > Text1.Text=""
>
> From a previous post of mine...
>
> I usually try and steer people away from using IsNumeric to "proof"
> supposedly numeric text. Consider this (also see note at end of post):
>
> ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")
>
> Most people would not expect THAT to return True. IsNumeric has some
"flaws"
> in what it considers a proper number and what most programmers are looking
> for.
>
> I had a short tip published by Pinnacle Publishing in their Visual Basic
> Developer magazine that covered some of these flaws. Originally, the tip
was
> free to view but is now viewable only by subscribers.. Basically, it said
> that IsNumeric returned True for things like -- currency symbols being
> located in front or in back of the number as shown in my example (also
> applies to plus, minus and blanks too); numbers surrounded by parentheses
as
> shown in my example (some people use these to mark negative numbers);
> numbers containing any number of commas before a decimal point as shown in
> my example; numbers in scientific notation (a number followed by an upper
or
> lower case "D" or "E", followed by a number equal to or less than 305 --
the
> maximum power of 10 in VB); and Octal/Hexadecimal numbers (&H for
> Hexadecimal, &O or just & in front of the number for Octal).
>
> NOTE:
> ======
> In the above example and in the referenced tip, I refer to $ signs and
> commas and dots -- these were meant to refer to your currency, thousands
> separator and decimal point symbols as defined in your local settings --
> substitute your local regional symbols for these if appropriate.
>
> As for your question about checking numbers, here are two functions that I
> have posted in the past for similar questions..... one is for digits only
> and the other is for "regular" numbers:
>
> Function IsDigitsOnly(Value As String) As Boolean
> IsDigitsOnly = Len(Value) > 0 And _
> Not Value Like "*[!0-9]*"
> End Function
>
> Function IsNumber(ByVal Value As String) As Boolean
> ' Leave the next statement out if you don't
> ' want to provide for plus/minus signs
> If Value Like "[+-]*" Then Value = Mid$(Value, 2)
> IsNumber = Not Value Like "*[!0-9.]*" And _
> Not Value Like "*.*.*" And _
> Len(Value) > 0 And Value <> "." And _
> Value <> vbNullString
> End Function
>
> Rick - MVP
>
>



Re: Selecting 'any number' by Pásztor,

Pásztor,
Fri Jan 09 08:28:57 CST 2004

Syed Zeeshan Haider wrote:
>
> If IsNumeric(Text1.Text) = False Or Not CDbl(Text1.Text) > 0 Then
> Text1.Text=""
>

Please note that this construct will not protect from raising error for bad
input, because VB always evaluates the full expression (no shurtcutting).
Actually I guess that IsNumeric is nothing else than a call of CDbl()
wrapped with error handling.
(The same holds also if Rick's IsNumber function is used instead.)

--
PZ