even and odd numbers

M

Mansoor

How should I write the following:

IIf([Number] is even,0,1)
Thanks in advance mansoor
 
S

Stefan Hoffmann

hi,

How should I write the following:

IIf([Number] is even,0,1)
Either use bit-masking or modulo-division:

CLng([Number]) And 1 = 1

CLng([Number]) Mod 2 = 1

Both are testing for odd numbers. Depending of you numbers data type you
don't need the CLng() conversion.


mfG
--> stefan <--
 
J

John Spencer

I think you will have to include parentheses to ensure the proper order of
evaluation in the statement.
(CLng([Number]) And 1) = 1

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Stefan said:
hi,

How should I write the following:

IIf([Number] is even,0,1)
Either use bit-masking or modulo-division:

CLng([Number]) And 1 = 1

CLng([Number]) Mod 2 = 1

Both are testing for odd numbers. Depending of you numbers data type you
don't need the CLng() conversion.


mfG
--> stefan <--
 
J

John W. Vinson

How should I write the following:

IIf([Number] is even,0,1)
Thanks in advance mansoor

You don't need an IIF at all: just use the MOD operator, which returns the
remainder after a divison. [Number] MOD 2 will be equal to 0 if Number is
even, 1 if it is odd.
 
M

Mansoor

Thank you Mr Spencer
but I need this condition

IIf([Number] is even,0,1)
as a criteria to the following column
[FromNo] Mod 2

John Spencer said:
I think you will have to include parentheses to ensure the proper order of
evaluation in the statement.
(CLng([Number]) And 1) = 1

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Stefan said:
hi,

How should I write the following:

IIf([Number] is even,0,1)
Either use bit-masking or modulo-division:

CLng([Number]) And 1 = 1

CLng([Number]) Mod 2 = 1

Both are testing for odd numbers. Depending of you numbers data type you
don't need the CLng() conversion.


mfG
--> stefan <--
.
 
J

John Spencer

You might want to wrap that in an abs function so it can handle negative
integers also.

Abs([Number]) MOD 2
or
Abs([Number] MOD 2)

If number is -3 then the original expression returns -1. Of course, if you
are just testing for zero, then there is no problem with the original expression.


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
How should I write the following:

IIf([Number] is even,0,1)
Thanks in advance mansoor

You don't need an IIF at all: just use the MOD operator, which returns the
remainder after a divison. [Number] MOD 2 will be equal to 0 if Number is
even, 1 if it is odd.
 
J

John Spencer

You can use either one of these expression to get the desired result

IIF(([Number] Mod 2) = 0,0,1)

Or

Abs([Number] Mod 2)

They both will return 0 or 1 if there is a number value in the number field.
There is a slight difference it the number field is NULL. The first
expression will return 1, the second will return Null.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
Thank you Mr Spencer
but I need this condition

IIf([Number] is even,0,1)
as a criteria to the following column
[FromNo] Mod 2

John Spencer said:
I think you will have to include parentheses to ensure the proper order of
evaluation in the statement.
(CLng([Number]) And 1) = 1

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Stefan said:
hi,

On 13.02.2010 09:55, Mansoor wrote:
How should I write the following:

IIf([Number] is even,0,1)
Either use bit-masking or modulo-division:

CLng([Number]) And 1 = 1

CLng([Number]) Mod 2 = 1

Both are testing for odd numbers. Depending of you numbers data type you
don't need the CLng() conversion.


mfG
--> stefan <--
.
 
M

Mansoor

Thank you very much Mr Spencer
both solutions work perfectly
Mansoor

John Spencer said:
You can use either one of these expression to get the desired result

IIF(([Number] Mod 2) = 0,0,1)

Or

Abs([Number] Mod 2)

They both will return 0 or 1 if there is a number value in the number field.
There is a slight difference it the number field is NULL. The first
expression will return 1, the second will return Null.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
Thank you Mr Spencer
but I need this condition

IIf([Number] is even,0,1)
as a criteria to the following column
[FromNo] Mod 2

John Spencer said:
I think you will have to include parentheses to ensure the proper order of
evaluation in the statement.
(CLng([Number]) And 1) = 1

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Stefan Hoffmann wrote:
hi,

On 13.02.2010 09:55, Mansoor wrote:
How should I write the following:

IIf([Number] is even,0,1)
Either use bit-masking or modulo-division:

CLng([Number]) And 1 = 1

CLng([Number]) Mod 2 = 1

Both are testing for odd numbers. Depending of you numbers data type you
don't need the CLng() conversion.


mfG
--> stefan <--
.
.
 

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