Odd or Even Test

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.
 
G

Guest

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
 
B

Brendan Reynolds

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
 
G

Guest

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
 
G

Guest

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

I truly am appreciative. Thanks.
 
T

Tim Ferguson

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
 
M

Michel Walsh

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
 
M

Michel Walsh

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

------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top