Once any errors are resolved and everything is correct, save the module. When done, go to the menu and select Debug, Compile to compile the code and see if there are any errors. In the code, GetMax=pvValue1 means that the value returned for the GetMax function will be the value pvValue1, which is the first parameter.Įnter the function in Figure 1 into the module. The return value is assigned to the function name. For example, GetMax(5,1) will return a value of 5, GetMax(Date(),#) will give today’s date, and GetMax(“abc”,”def”) will return “def.” Not only will the GetMax function work for numbers and dates, it will also work for strings. Finally, the data type of the return value is specified “As Variant.” Variant provides flexibility for different types of data and to handle a null result. In this case, there are two arguments containing the values to be compared: pvValue1 and pvValue2. After this are parentheses, which can be empty or contain arguments (parameters). The first line after the option statements begins with “Function GetMax” to declare a function and define the name. Functions and Subs are defined below the Option statements.įigure 1 shows a function called GetMax. Otherwise, it could create problems (run-time errors) when the code is used, such as in a function or query. That way, if your code contains any misspelled variables, the error will be identified when the code is compiled. “Option Explicit” can be added to require that all variable names be defined. This one tells the code how to compare string data. At the top of the new module, it will say “Option Compare Database.” Option statements are instructions that apply to the whole module. Press Alt+F11 to go to the Visual Basic Editor. If neither has a value, the result will be null. If one of the pair is missing, then the other should be returned. Let’s write a function to return the maximum value from a set of two numbers or dates. The logic for writing a function is the same as for creating a formula. A function can be used in place of a value in an expression for a field in a query, a control source on a form or report, and more. Access already has built-in functions such as SUM, which shows the result of adding several numbers, and COUNT, which returns how many values there are.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |