ACC: Writing Functions Called from Events or ExpressionsLast reviewed: June 8, 1997Article ID: Q97514 |
The information in this article applies to:
SUMMARYModerate: Requires basic macro, coding, and interoperability skills. Functions can be used in a variety of places in Microsoft Access. How you write your functions depends on where the functions are going to be called from. This article assumes that you are familiar with Access Basic and with creating applications for Microsoft Access using the programming tools provided with Microsoft Access.
MORE INFORMATIONThere are two main styles for writing Access Basic functions:
The examples below use the Proper() function to illustrate the differences between the two function styles. Proper() converts the first letter of a word to uppercase and the other letters to lowercase.
Calling a Function from an Event PropertyThe Proper() function can be written so it can be called from an event, such as the AfterUpdate property of a control on a form. In this example we will call it ProperAU() as a reminder that it should be called from the AfterUpdate property. Enter the following function in a module:
Function ProperAU(Field As Control) Field=UCase(Left(Field,1)) & LCase(Mid(Field,2)) End FunctionNOTE: The result of the calculation updates the field that was passed as a parameter.
Example
Calling a Function from an ExpressionThe Proper() function can be written so it can be called from an expression, or calculated control. In this example we will call it ProperCC() as a reminder that it should be used in calculations. Enter the following function in a module:
Function ProperCC(Field) ProperCC=UCase(Left(Field,1)) & LCase(Mid(Field,2)) End FunctionNOTE: The result of the calculation is assigned to the function. This way, it can be used in an expression or calculated control.
Example
Determining the Type of Function You Need
Where used Function style ----------------------------------------------------------- AfterUpdate, BeforeUpdate, and so on Event RunCode macro action Event Calculated controls on forms and reports Expression Calculated fields in a query Expression SetValue macro action expression Expression Default values in a table or form Expression Called from another function or sub Expression |
Keywords : kbprg PgmOthr
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |