Results 1 to 22 of 22

Thread: See if a number is between 2 and 8

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    how do i see if the number in the text box is between 2 and 8 (the number entered will be a whole number)
    NXSupport - Your one-stop source for computer help

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Private Sub Command1_Click()
     If Val(Text1) >= 2 And Val(Text1) <= 8 Then MsgBox "OK"
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Code:
    If number < 9 and number >1 then 
    'number is between 8 and 2
    Ens If
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Addicted Member Michael Woolsey's Avatar
    Join Date
    Nov 2000
    Location
    Calgary, Alberta, Canada.
    Posts
    243
    Would this meet your needs?

    Code:
    If CInt(Text1.Text) >= 2 and CInt(Text1.Text) <= 8 Then
      Msgbox "The number is between 2 and 8."
    Else
      Msgbox "The number is NOT between 2 and 8."
    End If
    Hope this helps.
    Micahel Woolsey
    Application/Web Developer

    Visual Basic 6.0 SP5
    Active Server Pages
    Oracle 9i
    - I'm going to live forever, or die trying!

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    thanks to all
    NXSupport - Your one-stop source for computer help

  6. #6
    Member
    Join Date
    Oct 2000
    Posts
    47

    more way

    if asc(text1.text)>=2 and asc(text1.text)<=8 then
    'ok for now..
    end if
    DealMan

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    dealman:
    the correct syntax would be this.

    If Asc(Text1) >= 50 And Asc(Text1) <= 56 Then MsgBox "OK"

    Vlatko:
    the correct syntax would be this.

    Dim number As Integer
    number = Val(Text1)
    If number < 9 And number > 1 Then MsgBox "OK"
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Function for different values

    Here's a more robust function:
    Code:
    Private Function IsInRange(ByVal num as Integer, ByVal min as Integer, ByVal max as Integer) as Boolean
    'Set a default return value
        IsInRange = False
        If (num => min) and (num <= max) Then
            IsInRange = True
        End If
    End Function
    'Usage:
        If IsInRange(Val(Text1), 2, 8) Then
            MsgBox "OK"
        Else
            MsgBox "Not OK"
        End If
    'Or check a number between 5 and 25
        If IsInRange(Val(Text1), 5, 25) Then
            MsgBox "OK"
        Else
            MsgBox "Not OK"
        End If
    [Edited by r0ach on 11-16-2000 at 01:52 AM]

    r0ach™
    Don't forget to rate the post

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    keeping it simple

    it would be better if VB could do real integer division
    Code:
    If Int((num - 2) / 7) = 1 Then Msgbox "In range!"
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186

    Talking Me too!

    This looks like fun. I also want a go:
    Code:
      Select Case Val(txtNumber.Text)
      Case 2 to 8
        'It's in range
      Case Else
        'Not in range
      End Select
    Shrog

  11. #11
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    kedaman,

    for Integers between 2 and 8 inclusively, Int((num -2)/7) equates to zero, not 1!!!

    Being really picky, the only integers between 2 and 8 are 3, 4, 5, 6 and 7. 2 and 8 are on the boundaries!

    Cheers,

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    love you paul! I think it's even better when you can leave out the equal operator! thanks for noticing my bug
    Code:
    if Int((x - 3) / 5) then
     'out of bounds
    else
     'inside bounds
    end if
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    Love you too - but don't tell my wife!

    Here's to really confusing coding (hehehe)

    P.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  14. #14
    Guest
    Kedaman, Paul:

    Isn't an integer division this > \ instead of / ???

    Just thought it was...

  15. #15
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    I've not come across that. Have you an example?

    P.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  16. #16
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186

    Thumbs up

    Yes it is.

    Code:
      'These two line are the same
      X = Y \ Z
      X = Int(Y / Z)
    People tend to miss it though, so I find that I have to put comments in my coding to point out that I am using "\" instead of "/". Most of the time it's simpler to just use the Int function, to avoid confusion.

    Shrog

  17. #17
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Originally posted by Shrog
    Code:
    ' These two line are the same
    X = Y \ Z
    X = Int(Y / Z)
    Not exactly.
    Code:
    X = 3.6 \ 4 ' X = 1
    X = Int(3.6 / 4) ' X = 0

  18. #18
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    Duh. I thought the operator was ">\".

    Retires from the arena in shame...

    Cheers,

    P.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  19. #19
    Junior Member
    Join Date
    Nov 2000
    Posts
    16
    hope this works


    Exit Sub 'you will remove this line in Lesson A
    Dim intX As Integer, intDem As Integer, intRep As Integer
    Dim intInd As Integer, intTotal As Integer
    Dim strFont As String, sngSize As Single
    Dim strPS1 As String * 3, strPS2 As String * 3, strPS3 As String * 3
    Dim strPS4 As String * 3, strPS5 As String * 4

    'accumulate totals
    For intX = 0 To 3
    intDem = intDem + Val(lblDem(intX).Caption)
    intRep = intRep + Val(lblRep(intX).Caption)
    intInd = intInd + Val(lblInd(intX).Caption)
    Next intX
    intTotal = intDem + intRep + intInd

    strFont = Printer.Font 'save current printer settings
    sngSize = Printer.FontSize
    Printer.Font = "courier new" 'change printer settings
    Printer.FontSize = 10 'print title and headings
    Printer.Print Tab(30); "PAO Information - 1999"
    Printer.Print
    Printer.Print Tab(5); "Party"; Tab(20); "18-35"; Tab(30); "36-50"; _
    Tab(40); "51-65"; Tab(50); "Over 65"; Tab(60); "Total"
    'align democrat numbers and print
    RSet strPS1 = Format(lblDem(0).Caption, "general number")
    RSet strPS2 = Format(lblDem(1).Caption, "general number")
    RSet strPS3 = Format(lblDem(2).Caption, "general number")
    RSet strPS4 = Format(lblDem(3).Caption, "general number")
    RSet strPS5 = Format(intDem, "general number")
    Printer.Print Tab(5); "Democrat"; Tab(22); strPS1; Tab(32); strPS2; _
    Tab(42); strPS3; Tab(54); strPS4; Tab(61); strPS5
    'align republican numbers and print
    RSet strPS1 = Format(lblRep(0).Caption, "general number")
    RSet strPS2 = Format(lblRep(1).Caption, "general number")
    RSet strPS3 = Format(lblRep(2).Caption, "general number")
    RSet strPS4 = Format(lblRep(3).Caption, "general number")
    RSet strPS5 = Format(intRep, "general number")
    Printer.Print Tab(5); "Republican"; Tab(22); strPS1; Tab(32); strPS2; _
    Tab(42); strPS3; Tab(54); strPS4; Tab(61); strPS5
    'align independent numbers and print
    RSet strPS1 = Format(lblInd(0).Caption, "general number")
    RSet strPS2 = Format(lblInd(1).Caption, "general number")
    RSet strPS3 = Format(lblInd(2).Caption, "general number")
    RSet strPS4 = Format(lblInd(3).Caption, "general number")
    RSet strPS5 = Format(intInd, "general number")
    Printer.Print Tab(5); "Independent"; Tab(22); strPS1; Tab(32); strPS2; _
    Tab(42); strPS3; Tab(54); strPS4; Tab(61); strPS5
    Printer.Print 'print two blank lines
    Printer.Print
    'print grand total
    RSet strPS5 = Format(intTotal, "general number")
    Printer.Print Tab(41); "Total respondents"; Tab(61); strPS5
    Printer.Print 'print a blank line
    Printer.Print Tab(5); "End of report" 'print message
    Printer.EndDoc 'send report to printer
    Printer.Font = strFont
    Printer.FontSize = sngSize
    End Sub

    Private Sub Form_Load()
    frmPao.Top = (Screen.Height - frmPao.Height) / 2
    frmPao.Left = (Screen.Width - frmPao.Width) / 2
    lstAge.AddItem "18 - 35"
    lstAge.AddItem "36 - 50"
    lstAge.AddItem "51 - 65"
    lstAge.AddItem "Over 65"

    End Sub


  20. #20
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Exclamation ???

    Ok...WHat the hell was that?... SOme sort of Voting ballot??
    I thought this was the 'Every possible way to determine if a number is between 2 and 8' Board.

    BTW.. everyone forgot the worst (or really the first way you every try it when you learn VB) way.

    Code:
    If X = 3 or X = 4 or X = 5 or X = 6 or X = 7 then
       'Do Code
    Else
       'Do Whatever
    End if
    this is of course assuming that the variable is Declared an integer.

    Sorry couldn't resist!!

    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  21. #21
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Geoff_xrx, this is even more worse...
    Code:
    Dim X 'evil variant
    Dim InRange 'is it in the range
    
    'do a select case on x
    Select Case X
    Case 3
        'it's in the range
        InRange = True
    Case 4
        'it's in the range
        InRange = True
    Case 5
        'it's in the range
        InRange = True
    Case 6
        'it's in the range
        InRange = True
    Case 7
        'it's in the range
        InRange = True
    Case Else
        'not in the range
        InRange = False
    End Select
    
    'is it in the range?
    If InRange = True Then
        Msgbox "It's between 2 and 8"
    Else
        Msgbox "It's not between 2 and 8"
    End If
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  22. #22
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    Boy, This is the 2nd thread I've seen that just drags on about a simple thing!

    If the first reply (which in this case it did) seem to answer the question why add more replies???

    I know sometimes you guys have some none standard/generic method of doing things and yes they may be interesting and/or worth learning but this one is just ridiculous!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width