UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> User-defined function for Validation    
 
   
re444
post Feb 19 2009, 08:42 AM
Post #1

UtterAccess Member
Posts: 38



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.
Go to the top of the page
 
+
Alan_G
post Feb 19 2009, 09:21 AM
Post #2

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,884
From: Devon UK



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 ?
Go to the top of the page
 
+
re444
post Feb 19 2009, 03:14 PM
Post #3

UtterAccess Member
Posts: 38



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?
Go to the top of the page
 
+
Alan_G
post Feb 19 2009, 03:57 PM
Post #4

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,884
From: Devon UK



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 ?
Go to the top of the page
 
+
re444
post Feb 20 2009, 05:11 PM
Post #5

UtterAccess Member
Posts: 38



Your code is an improvement.
I'm calling the function from Validation Rule section of the control like: IsDateTrue([Control_Name])

thanks
Go to the top of the page
 
+
Alan_G
post Feb 20 2009, 05:30 PM
Post #6

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,884
From: Devon UK



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.
Go to the top of the page
 
+
re444
post Feb 21 2009, 04:59 AM
Post #7

UtterAccess Member
Posts: 38



It works when placed in Before Update event of the control.

thanks for your time!
Go to the top of the page
 
+
Alan_G
post Feb 21 2009, 08:34 AM
Post #8

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,884
From: Devon UK



You're very welcome (IMG:http://www.utteraccess.com/forum/style_emoticons/default/wink.gif)
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 23rd May 2013 - 11:41 AM