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

Is The Result a Whole Number????

Status
Not open for further replies.

wisey

IS-IT--Management
Jul 5, 2000
14
0
0
I know this is a stupid question, but, I have to test that the input to a textbox is evenly divisible by 8 and if its not clear the text box and display a warning that it isn't. I'm sure I did this years ago, but can't remember how. Please can someone help??
 
Dim lngNum As Long
Dim lngErr as long
On Error Resume next
lngNum = text1.Text
lngerr = Err.Number
On Error Goto 0
If lngNumber <> 0 then
' lngNum mod 8 = 0 will not work because of rounding and truncation
If (lngNum \ 8) * 8 = lngNum then
....multiple of 8
End if
End if Compare Code (Text)
Generate Sort in VB or VBScript
 
If you have VB6, this type of thing can be put into the Validate event of the control. Make sure your TextBox's CausesValidation property is set to True (the default) then try this code:
Code:
Private Sub TextBox_Validate(Cancel as Boolean)
     If TextBox.Text <> (TextBox.Text / 8) * 8 Then
          MsgBox &quot;You must enter a number divisible by 8&quot;, vbExclamation
          Cancel = True
     End If
End Sub
The validate event fires when the control loses it's focus. If validation fails then focus goes back to the control.
 
If TextBox.Text <> (TextBox.Text / 8) * 8 Then

Above will throw an exception if the number is not numeric.

'This does not give an error message.
Private Sub Command1_Click()
If (8.33333 / 8) * 8 <> 8.33333 Then ' / is ordinary divide
MsgBox &quot;Fail&quot;
End If
End Sub

'This does give error message
Private Sub Command1_Click()
If (8.33333 \ 8) * 8 <> 8.33333 Then ' Whole number division
MsgBox &quot;Fail&quot;
End If
End Sub Compare Code (Text)
Generate Sort in VB or VBScript
 
Code:
Private Sub Command1_Click()
    If (Not IsNumeric(Text1.Text)) Then
        MsgBox &quot;Non-Numeric Value entered&quot;, vbOKOnly, &quot;Invalid Entry&quot;
        Check1 = False
        Exit Sub
    End If

    If (Text1.Text / 8) * 8 = 8 Then ' Whole number division
        Check1 = 1
     Else
        MsgBox &quot;Value not divisible by 8&quot;, vbOKOnly, &quot;Invalid Entry&quot;
        Check1 = False
    End If
End Sub

At least trap the non-numeric input. I added a Check box to provide feedback that the process was dooing SOMEthing!
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Michael

This only checks that number is 8
If (Text1.Text / 8) * 8 = 8 Then ' Whole number division

The / does reguilar division. &quot;/ 8) * 8&quot; is always = 1.

That is why I used \ in
If (lngNum \ 8) * 8 = lngNum then
Compare Code (Text)
Generate Sort in VB or VBScript
 
Oops - sorry

Code:
Private Sub Command1_Click()
    If (Not IsNumeric(Text1.Text)) Then
        MsgBox &quot;Non-Numeric Value entered&quot;, vbOKOnly, &quot;Invalid Entry&quot;
        Check1 = False
        Exit Sub
    End If

    If (Text1.Text / 8) * 8 = Text1.Text Then ' Whole number division
        Check1 = 1
     Else
        MsgBox &quot;Value not divisible by 8&quot;, vbOKOnly, &quot;Invalid Entry&quot;
        Check1 = False
    End If
End Sub
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Hi,
I realize the question's been answered, but I'm just curious if there is some difficulty with the Mod operator that makes it inadvisable in this instance?

i.e. to check if a number is evenly divisible by 8, we just type:
If intNum Mod 8 = 0 Then
msgbox &quot;Evenly divisible by 8&quot;
Else
msgbox &quot;Not evenly divisible by 8&quot;
End If

Thanks :)
 
intNum = 8 * 7.267
? intMum Mod 8
0
of course, this is somewhat contrived, but the principal can arise for those who are not careful.

Also, consider this:

? ASCII Mod 8
0

(Here, ASCII is not a defind variable (type))

Or, consider the next

? &quot;ASCII&quot; Mod 8

which results in Error(13), Type Mismatch. If there is a variable involved, you MAY get some type cast operation which leaves the value in an unexpected type - and the result is therefore not correct. Of course, no &quot;PROFESSIONAL&quot; programmer would EVER be in this situation, we ALL use Option Explicit in EVERY module, and we NEVER use variant data typing ... so the above scenarios do not apply to you, and the modulo operation is appropiate. In fact, I usually do just use mod, but then I TRY to act professionally.


MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Katerine,

The question asked about checking the value from a textbox, and that will be a string. So you have to do some work first of all to ensure that the contents of that string can genuinely be represented as an integer (or long), before carrying out the test you propose. Which is basically what all the code presented in the prior solutions is trying, directly or indirectly, to achieve

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top