Odd or Even Test

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Access 2003 on Windows XP Pro

In my past computing life I have used the "Mod" aritmetic function to build
an odd or even integer test tool. The Modular function, as I know it,
discards the fractional (Non-integer) portion of a real number. A simple
division followed by a multiplication after applying the mod function will
generate Odd or Even determinable results. That I can do.

I have searched for a Mod Function or Odd-Even test function, but have
failed to uncover anything. Is there an alternate tool or method that does
this? Since the data type is not text, I do not want to use string
manipulation tools as the long route to the answer. Way too much code there.

Any guidance, or directions would be greatly appreciated. Thanks as always.
 
YOu are not looking in the right place. Try looking in the VB Editor help.
VBA does have a mod function:
2 mod 2 returns 0
3 mod 2 returns 1
 
VBA has Mod, but it is an operator, not a function ...

Public Function IsEven(ByVal varNumber As Variant) As Variant

If IsNull(varNumber) Then
IsEven = Null
Else
IsEven = ((varNumber Mod 2) = 0)
End If

End Function
 
Thanks for the detail. Just what I needed.

Brendan Reynolds said:
VBA has Mod, but it is an operator, not a function ...

Public Function IsEven(ByVal varNumber As Variant) As Variant

If IsNull(varNumber) Then
IsEven = Null
Else
IsEven = ((varNumber Mod 2) = 0)
End If

End Function
 
As in the past Duane, few words, precision in detail, clarity in example.

I truly am appreciative. Thanks.
 
I have searched for a Mod Function or Odd-Even test function, but have
failed to uncover anything. Is there an alternate tool or method that
does this?

You have seen examples of the Mod operator. Without testing, I would guess
that the And operator would be faster; it's certainly cleaner in the sense
that most programmers maintaining your code afterwards would recognise more
easily what you are trying to do:

' check for parity...
If (MyValue And 1) = 1 Then
' it's odd

Else
' it's even

End If


B Wishes


Tim F
 
Hi,


Nope. Counter example:

? (-1) AND 1
1


The operator MOD is, in my opinion, more explicit, as per the intentions,
than logic involving bits.


Vanderghast, Access MVP
 
Forget that counter example, I am wrong, it worked, definitively.

------------------------------
 
Back
Top