Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
Ranges
Old
  (#1)
dfg
Guest
 
Posts: n/a
Default Ranges - 06-04-2007, 08:50 AM

Hello. I am trying to figure out how I can find out if a character
field is within a certain range. For example, I have a textbox and I
would like to check to see if the data entered is between 01 to 05. I
don't want to convert them to numbers. I need the leading zero.

I was thinking of using a Case statement to compare if it is within the
proper range. I just can't seem to figure out how to check to see if it
is outside the range. An array perhaps?

Any thoughts or pointers?

Thanks.

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Ranges
Old
  (#2)
Scott Zielinski
Guest
 
Posts: n/a
Default Re: Ranges - 06-04-2007, 08:50 AM

dfg wrote:
> Hello. I am trying to figure out how I can find out if a character
> field is within a certain range. For example, I have a textbox and I
> would like to check to see if the data entered is between 01 to 05. I
> don't want to convert them to numbers. I need the leading zero.
>
> I was thinking of using a Case statement to compare if it is within the
> proper range. I just can't seem to figure out how to check to see if it
> is outside the range. An array perhaps?
>
> Any thoughts or pointers?
>
> Thanks.
>


You can test with the Case Statements - simply include a 'Case Else'
statement, and use that for your 'Out of Range' routine.

Scott C. Zielinski

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Ranges
Old
  (#3)
Mark Mulvany
Guest
 
Posts: n/a
Default Re: Ranges - 06-04-2007, 08:50 AM


"Scott Zielinski" <EMAIL REMOVED> wrote in message
news:FsudnQhY7sIkrF2iRVn-EMAIL REMOVED...
> dfg wrote:
> > Hello. I am trying to figure out how I can find out if a character
> > field is within a certain range. For example, I have a textbox and I
> > would like to check to see if the data entered is between 01 to 05. I
> > don't want to convert them to numbers. I need the leading zero.
> >
> > I was thinking of using a Case statement to compare if it is within the
> > proper range. I just can't seem to figure out how to check to see if it
> > is outside the range. An array perhaps?
> >
> > Any thoughts or pointers?
> >
> > Thanks.
> >

>
> You can test with the Case Statements - simply include a 'Case Else'
> statement, and use that for your 'Out of Range' routine.
>
> Scott C. Zielinski
>


If your entry is numbers all you have to do is use "If" to compare it: "If
MyNumber < 5 Then..." it is outside the range.

A text box entry might need to be first converted to numeric entry for you
to test it. That can be done with "Val" by ***igning to a numeric variable
with "=Val( [String Var])". For example:-

Dim MyNumber as Integer

MyNumber = Val(TextBox1.Text) '***ign contents of box to integer variable

If MyNumber < 5 Then
MsgBox("Sorry that number was too high! Please try again.")
End If

A useful thing is "IsNumeric" for testing whether the contents of a variable
are numeric. (A variant variable takes either a number or a string). "If
IsNumeric(MyNumber) Then..."

I am not positive about it but one way this kind of thing might also be done
is by using the "Like Operator". This enables you to set a pattern and match
the user's entry against the pattern: see Help for "Like Operator" and its
syntax. I know it copes with ranges of letters in the alphabet. I suspect it
would work because to the Like Operator numbers are characters.

Just ideas.


Mark Mulvany










   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Ranges
Old
  (#4)
Richard Heathfield
Guest
 
Posts: n/a
Default Re: Ranges - 06-04-2007, 08:50 AM

Mark Mulvany wrote:

>> dfg wrote:
>> > [...] I don't want to convert them to numbers. [...]
>> >

<snip>
>
> If your entry is numbers all you have to do is use "If" to compare it:
> "If MyNumber < 5 Then..." it is outside the range.
>
> A text box entry might need to be first converted to numeric entry for you
> to test it.


He specifically says that he /doesn't/ want to convert to numbers.

--
Richard Heathfield : EMAIL REMOVED
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Ranges
Old
  (#5)
J French
Guest
 
Posts: n/a
Default Re: Ranges - 06-04-2007, 08:50 AM

On Sun, 23 Nov 2003 03:10:40 GMT, dfg <EMAIL REMOVED> wrote:

>Hello. I am trying to figure out how I can find out if a character
>field is within a certain range. For example, I have a textbox and I
>would like to check to see if the data entered is between 01 to 05. I
>don't want to convert them to numbers. I need the leading zero.
>
>I was thinking of using a Case statement to compare if it is within the
>proper range. I just can't seem to figure out how to check to see if it
>is outside the range. An array perhaps?
>
>Any thoughts or pointers?


If InStr( "12345", N$ ) Then ...
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Ranges
Old
  (#6)
James Rogers
Guest
 
Posts: n/a
Default Re: Ranges - 06-04-2007, 08:50 AM

"Mark Mulvany" <EMAIL REMOVED> wrote in
news:bpq13r$t8j$EMAIL REMOVED:

>
> "Scott Zielinski" <EMAIL REMOVED> wrote in message
> news:FsudnQhY7sIkrF2iRVn-EMAIL REMOVED...
>> dfg wrote:
>> > Hello. I am trying to figure out how I can find out if a character
>> > field is within a certain range. For example, I have a textbox and
>> > I would like to check to see if the data entered is between 01 to
>> > 05. I don't want to convert them to numbers. I need the leading
>> > zero.
>> >
>> > I was thinking of using a Case statement to compare if it is within
>> > the proper range. I just can't seem to figure out how to check to
>> > see if it is outside the range. An array perhaps?
>> >
>> > Any thoughts or pointers?


A simple solution is to create a function taking your input string as a
parameter and returning a boolean (true or false) value.

An example (in Ada) of such a function would be:

function In_Range (Item : String) return Boolean is
subtype Small_String is String(1..2);
type Value_List is array(1..5) of Small_String;
Good_Values : constant Value_List := ("01", "02",
"03", "04",
"05");
Result : Boolean := False;
begin
for Index in Good_Values'Range loop
if Item = Good_Values(Index) then
Result := True;
end if;
end loop;
return Result;
end In_Range;

When performing the test for the value from the text box simply call
the function:

if In_Range(TextBox_Value) then
-- Do whatever you should do
else
-- Handle out of range condition
end if;

Jim Rogers
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Ranges
Old
  (#7)
Rick Rothstein
Guest
 
Posts: n/a
Default Re: Ranges - 06-04-2007, 08:50 AM

> Hello. I am trying to figure out how I can find out if a character
> field is within a certain range. For example, I have a textbox and I
> would like to check to see if the data entered is between 01 to 05. I
> don't want to convert them to numbers. I need the leading zero.


In other words, your user **must** type in his/her entry **with** the
leading zero in order for it to be acceptable to you (that is, a user entry
of 3 would be unacceptable whereas an entry of 03 would be fine), right?

If Text1.Text >= "01" And Text1.Text <= "05" Then
MsgBox "Your entry is OK"
Else
MsgBox "The entry is invalid!"
End If

Rick - MVP


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Ranges
Old
  (#8)
Rick Rothstein
Guest
 
Posts: n/a
Default Re: Ranges - 06-04-2007, 08:50 AM

> > Hello. I am trying to figure out how I can find out if a character
> > field is within a certain range. For example, I have a textbox and I
> > would like to check to see if the data entered is between 01 to 05. I
> > don't want to convert them to numbers. I need the leading zero.

>
> In other words, your user **must** type in his/her entry **with** the
> leading zero in order for it to be acceptable to you (that is, a user

entry
> of 3 would be unacceptable whereas an entry of 03 would be fine), right?
>
> If Text1.Text >= "01" And Text1.Text <= "05" Then
> MsgBox "Your entry is OK"
> Else
> MsgBox "The entry is invalid!"
> End If


Mark raised the idea of using the Like operator. This is how you would do
the above using it instead of testing the range as I did above...

If Text1.Text Like "0[1-5]" Then
MsgBox "Your entry is OK"
Else
MsgBox "The entry is invalid!"
End If


Rick - MVP


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Ranges
Old
  (#9)
Rick Rothstein
Guest
 
Posts: n/a
Default Re: Ranges - 06-04-2007, 08:50 AM

> A useful thing is "IsNumeric" for testing whether the contents of a
variable
> are numeric. (A variant variable takes either a number or a string). "If
> IsNumeric(MyNumber) Then..."


IsNumeric is not nearly as useful for parsing numbers as you are indicating.
Here is a posting that I have offered in the past on this function. (The
alternate functions at the end don't really relate to the question at hand,
but I've left them in case anyone who hasn't seen my posting before needs
the kind of functionality they provide.)

Rick - MVP

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

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 = 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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Ranges
Old
  (#10)
Randy Birch
Guest
 
Posts: n/a
Default Re: Ranges - 06-04-2007, 08:50 AM

Rick ...

I've read this response a number of times, and understand the message you
attempting to present, however I believe the example

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

.... and the statement "Most people would not expect THAT to return True." is
not quite correct. Your statement ***umes the developer viewing the
complexity and formatting of the expression would expect a False result.

One of the MSDN definitions for IsNumeric indicates it will return True if
the value p***ed is a string _and_ that string can be successfully converted
to a double. This is the case for the above ...

Print CDbl("($1,23,,3.4,,,5,,E67$)")

Thus, given the MSDN inputbox entry example:

Do
anyNumber = InputBox("Enter a number")
Loop Until IsNumeric(anyNumber)

.... the loop would properly terminate when the string above was entered.

While your example may be representative of what may, upon first
examination, be perceived as a string that should fail the IsNumeric test, I
believe the MSDN example of using :

ReturnValue = IsNumeric("45 some text here")

remains a better (and clearer) example showing the possibility of misusing
IsNumeric against a string containing a numeric value where the user may
interpret that any variable that p***es a Val() test should p*** an
IsNumeric() test.

My intention is not to represent IsNumeric as being a better test than any
other available methods for determining whether a value is a valid number,
just that the complexity of a valid string does not negate the benefit of
using IsNumeric as judiciously as any other function would be used.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top