Jump to content

Welcome the ORIGINAL FileMaker Community

Take a moment to join us, no noise, all FileMaker...We Promise

SamEdwards

Finding records in number field that are odd or even

Recommended Posts

SamEdwards

I would like to write a script that performs a find that only returns records in a number field that are odd numbers. Is this possible?

Share this post


Link to post
Share on other sites
Techphan

Yes.  You can use the MOD function - - -> Mod (Number ; 2) will result in "0" for even number and "1" for odd numbers.

Share this post


Link to post
Share on other sites
SamEdwards

Thanks for this info. How would I incorporate this type of custom function in a script to perform a records find in a number formatted field?

Share this post


Link to post
Share on other sites
Techphan
Posted (edited)
1 hour ago, SamEdwards said:

Thanks for this info. How would I incorporate this type of custom function in a script to perform a records find in a number formatted field?

Let's assume that you have a field called NUMBER (actually it is YourTable::NUMBER) that you want to evaluate:

You would need to either create a calculated field in that table called ModNumber (calc field, number result) = Mod (YourTable::NUMBER ; 2) which results in "0" for even numbers and "1" for odd numbers. Mod is short for "modulus" which is just serial subtraction of "2" (in this specific case) from the beginning number repetitively until the remainder is either "0" or "1."   In this case your script can just run a simple search in the table for all the ModNumber values = 1:

Sample script v1

Enter Find Mode [ Pause: Off ]

Set Field [YourTable::ModNumber ; 1]

Perform Find [ ]

 

***or***

Instead of a ModNumber calculated field, just create a simple text field called FLAG (or any other name of your liking).  In this case your script will perform the Modulus calculation and set the FLAG field according ( the results are up to you and can be "Odd" or "Even,"  "0" or "1," or you can just place an entry in the field if your original meets the criteria of being odd ("True," for example).  Then your script just searches for the records with that value.

 

Sample script v2

Go to Record [First]

Loop

...If [ Mod (YourTable::NUMBER ; 2) = 1

......Set Field [YourTable::FLAG ; "True"]

...End If

...Go to Record [Next ; Exit after last: On]

End Loop

Enter Find Mode [ Pause: Off ]

Set Field [YourTable::FLAG ; "True"]

Perform Find [ ]

 

Version 1 will be faster, especially if you have a lot of records that a script would have to go through and evaluate each record sequentially.

Edited by Techphan
Grammar correction

Share this post


Link to post
Share on other sites
SamEdwards

Thank you, a wealth of info. Works perfectly!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×