Full Version: User-defined function for Validation
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
re444
I have one almost global, Validation Rule for my date fields and I'd like to write a user-defined function for this purpose just one time; but it seems that Access does not allow such a work! Is this true and if yes, what is the solution?

thanks,
Reza.
Alan_G
Hi

You can most definitely write your own Public Function that can be called from anywhere in your db...what's the validation rule you want to use ?
re444
It's just a simple rule that restricts the date values to be in current year(as a practice):

Public Function IsDateTrue(ctl As Control) As Boolean
If Year(ctl.Value) = Year(Date) Then
IsDateTrue = True
Else
IsDateTrue = False
End If
End Function

While used in a form, when the entry violates the rule, custom massage is displayed but when I correct it, this massage appears again(no matter what the content is)! And in a table, the function is not accessible from the builder.
Is the form of the function true, anyway?
Alan_G
Hi

You can't call functions (or code in general) from a table.......

You can change the function to make it check for a valid date as well as check for current year to something like

CODE
Public Function IsDateTrue(ctl As Control) As Boolean
If IsDate(ctl.Value) Then
    IsDateTrue = Year(ctl.Value) = Year(Date)
Else
    IsDateTrue = False
End If
End Function


How are you calling the function, and from what form event ?
re444
Your code is an improvement.
I'm calling the function from Validation Rule section of the control like: IsDateTrue([Control_Name])

thanks
Alan_G
I'd use one the Event Procedures of the control...........

If you wanted to force a valid entry then you could use the Before Update event setting the Cancel argument equal to IsDateTrue.
re444
It works when placed in Before Update event of the control.

thanks for your time!
Alan_G
You're very welcome wink.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.