Odd/Even Validation Rule between Two Fields

BGlover

New member
Local time
Today, 09:22
Joined
May 3, 2013
Messages
4
Hi,
I am building a scoring system for an Archery Tournament and am trying to add some extra validation between two fields.
I want to check the contents of one field against another.
So if Field A has an even value, then Field B must also be even, and if Field A has an Odd value, Field B must be odd.
Would it also be possible to add an exception such that if Field B = 0 then the validation rule does not apply?
I am only a very occasional user of Access, and don't have a very good grasp of the code structure in it, but am prepared to try and learn.
Many Thanks and feel free to ask for more detail,
Ben
 
Welcome to the forum.

You could have some code in the Before Update Event of Field B, along the line of;

Code:
[COLOR="DarkGreen"]'Check if Field B hold a Zero Value[/COLOR]
If Me.FieldB = 0 Then
     Exit Sub
End If

[COLOR="DarkGreen"]'Check if both fields are even[/COLOR]
If Int(Me.FieldA/2) - Me.FieldA/2 = 0 And Int(Me.FieldB/2) - Me.FieldB/2 = 0
     Exit Sub    [COLOR="DarkGreen"]'Both Fields Even[/COLOR]
Else
     MsgBox "Both Fields Must be Even, please check the value entered in Field B"
     Cancel = True
     Me.FieldB.SetFocus
     Exit Sub
End If

[COLOR="DarkGreen"]'Check if both fields are odd[/COLOR]
If Abs(Int(Me.FieldA/2) - Me.FieldA/2) = 0.5 And Abs(Int(Me.FieldB/2) - Me.FieldB/2 = 0.5)
     Exit Sub    [COLOR="DarkGreen"]'Both Fields Odd[/COLOR]
Else
     MsgBox "Both Fields Must be Odd, please check the value entered in Field B"
     Cancel = True
     Me.FieldB.SetFocus
     Exit Sub
End If

Warning: This is air code and may need some tweaking :o
 
Since x MOD 2= 0 for any even x we can do:

Code:
If Me.FieldB <> 0 Then
    If Me.FieldA Mod 2 <> Me.FieldB Mod 2 Then
        MsgBox "Fields must be either both even or both odd"
        'add some action here
    End If
End If

Chris
 
Thanks stopher, much neater. I knew there'd be a more economic way to do that :D
 
Thank You both of you, that has done the trick brilliantly, and taught me a little about the syntax used.
Ben G
 

Users who are viewing this thread

Back
Top Bottom