how do i see if the number in the text box is between 2 and 8 (the number entered will be a whole number)
Printable View
how do i see if the number in the text box is between 2 and 8 (the number entered will be a whole number)
Code:Private Sub Command1_Click()
If Val(Text1) >= 2 And Val(Text1) <= 8 Then MsgBox "OK"
End Sub
Code:If number < 9 and number >1 then
'number is between 8 and 2
Ens If
Would this meet your needs?
Hope this helps.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
Micahel Woolsey
thanks to all
if asc(text1.text)>=2 and asc(text1.text)<=8 then
'ok for now..
end if
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"
Here's a more robust function:
[Edited by r0ach on 11-16-2000 at 01:52 AM]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
it would be better if VB could do real integer division :)
Code:If Int((num - 2) / 7) = 1 Then Msgbox "In range!"
This looks like fun. I also want a go:
ShrogCode:Select Case Val(txtNumber.Text)
Case 2 to 8
'It's in range
Case Else
'Not in range
End Select
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.
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
Love you too - but don't tell my wife!
Here's to really confusing coding (hehehe)
P.
Kedaman, Paul:
Isn't an integer division this > \ instead of / ???
Just thought it was...
I've not come across that. Have you an example?
P.
Yes it is.
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.Code:'These two line are the same
X = Y \ Z
X = Int(Y / Z)
Shrog
Not exactly. :rolleyes:Quote:
Originally posted by Shrog
Code:' These two line are the same
X = Y \ Z
X = Int(Y / Z)
Code:X = 3.6 \ 4 ' X = 1
X = Int(3.6 / 4) ' X = 0
Duh. I thought the operator was ">\".
Retires from the arena in shame...
Cheers,
P.
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
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.
this is of course assuming that the variable is Declared an integer.Code:If X = 3 or X = 4 or X = 5 or X = 6 or X = 7 then
'Do Code
Else
'Do Whatever
End if
Sorry couldn't resist!!
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
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!!! :D