A function is a task that must be performed to produce a result on a table, a form, or a report. It is like an operation or an expression with the first difference that someone else created it and you can just use it. For example, instead of the addition operator "+", to add two values, you could use a function. In practicality, you cannot create a function in Microsoft Access. You can only use those that have been created and that exist already. These are referred to as built-in functions.
If you had to create a function (remember that we cannot create a function in Microsoft Access; the following sections are only hypothetical but illustrative of the subject of a function), a formula you would use is: FunctionName() This syntax is very simplistic but indicates that the minimum piece of information a function needs is a name. The name allows you to refer to this function in other parts of the database. The name of the function is followed by parentheses. As stated already, a function is meant to perform a task. This task would be defined or described in the body of the function. In our simple syntax, the body of the function would start just under its name after the parentheses and would stop just above the End word. The person who creates a function also decides what the function can do. Following our simple formula, if we wanted a function that can open Solitaire, it could appear as follows: FunctionExample() Once a function has been created, it can be used. Using a function is referred to as calling it. To call a simple function like the above FunctionExample, you would just type its name. A function produces a result. This is also stated that a function returns a value. Based on this, the result of a function can be provided for further use and assigned (passed) to a field or to another function. To display the result of a function in a field, you can access its Control Source property, use the assignment operator "=", type the name of the function, followed by its parentheses. This would be done as follows: ![]() The person who creates a function also decides what kind of value the function can return. For example, if you create a function that performs a calculation, the function may return a number. If you create another function that combines a first name and a last name, you can make the function return a string that represents a full name. When asked to perform its task, a function may need one or more values to work with. If a function needs a value, such a value is called a parameter. The parameter is provided in the parentheses of the function. The formula used to create such a function would be: ReturnValue FunctionName(Parameter) Once again, the body of the function would be used to define what the function does. For example, if you were writing a function that multiplies its parameter by 12.58, it would appear almost as follows: Decimal FunctionName(Parameter) While a certain function may need one parameter, another function would need many of them. The number and types of parameters of a function depend on its goal. When a function uses more than one parameter, a comma separates them in the parentheses. The syntax used is: ReturnValue FunctionName(Parameter1, Parameter2, Parameter_n) If you were creating a function that adds its two parameters, it would appear as follows: NaturalNumber AddTwoNumbers(Parameter1, Parameter2) Once a function has been created, it can be used in other parts of the database. Once again, using a function is referred to as calling it. If a function is taking one or more parameters, it is called differently than a function that does not take any parameter. We saw already how you could call a function that does not take any parameter and assign it to a field using its Control Source. If a function is taking one parameter, when calling it, you must provide a value for the parameter, otherwise the function would not work (when you display the form or report, Microsoft Access would display an error). When you call a function that takes a parameter, the parameter is called an argument. Therefore, when calling the function, we would say that the function takes one argument. In the same way, a function with more than one parameter must be called with its number of arguments. To call a function that takes an argument, type the name of the function followed by the opening parenthesis "(", followed by the value (or the field name) that will be the argument, followed by a closing parenthesis ")". The argument you pass can be a constant number. Here is an example: ![]() The value passed as argument can be the name of an existing field. The rule to respect is that, when Microsoft Access will be asked to perform the task(s) for the function, the argument must provide, or be ready to provide, a valid value. As done with the argument-less function, when calling this type of function, you can assign it to a field by using the assignment operator in its Control Source property. Here is an example: ![]() If the function is taking more than one argument, to call it, type the values for the arguments, in the exact order indicated, separated from each other by a comma. As for the other functions, the calling can be assigned to a field in its Control Source. All the arguments can be constant values, all of them can be the names of fields or objects, or some arguments can be passed as constants and others as names of fields. Here is an example: ![]()
We have mentioned that, when calling a function that takes an argument, you must supply a value for the argument. There is an exception. Depending on how the function was created, it may be configured to use its own value if you fail, forget, or choose not, to provide one. This is known as the default argument. Not all functions follow this rule and you would know either by checking the documentation of that function or through experience. If a function that takes one argument has a default value for it, then you do not have to supply a value when calling that function. Such an argument is considered optional. Whenever in doubt, you should provide your own value for the argument. That way, you would not only be on the safe side but also you would know with certainty what value the function had to deal with. If a function takes more than one argument, some argument(s) may have default values while some others do not. The arguments that have default values can be used and you do not have to supply them.
To assist you with writing expressions or calling a (built-in) function and reduce the likelihood of a mistake, Microsoft Access is equipped with a good functional dialog box named the Expression Builder. The Expression Builder is used to create an expression or call a function that would be used as the Control Source of a field.
To access the Expression Builder, open the Property Sheet for the control that will use the expression or function, and click its ellipsis button ![]() ![]() Like every regular dialog box, the Expression Builder starts on top with its title bar that displays its caption and its system Close button. Unlike a regular dialog box, the Expression Builder is resizable: you can enlarge, narrow, heighten, or shorten it, to a certain extent. Under the title bar, there is a label followed by a link: Calculated Control. If you click that link, a Help window would come up: ![]() Under the link, there is an example of an expression. The main upper area of the Expression Builder shows a rectangular text box with a white background. It is used to show the current expression when you have written it. If you already know what you want, you can directly type an expression, a function, or a combination of those. The right section of the Expression Builder displays a few buttons. After creating an expression, to submit it, you click OK. To abandon whatever you have done, yo can click Cancel or press Esc. To get help while using the Expression Builder, you can click Help. To show a reduced height of the Expression Builder, click the << Less button. The button would change to More >>: ![]() To show the whole dialog box, click More >>. Under the text box, there are three boxes. The left list displays some categories of items. Some items in the left list appear with a + button. To access an object, expand its node collection by double-clicking its corresponding button or clicking its + button. After you have expanded a node, a list would appear. In some cases, such as the Forms node, another list of categories may appear. To access an object of a collection, in the left list, you can click its node. This would fill the middle list with some items that would of course depend on what was selected in the left list. Here is example: ![]() The top node is the name of the form or report on which you are working. Under that name are the Functions node. To access a function, first expand the Functions node. To use one of the Microsoft Access built-in functions, in the left list, click Built-In Functions. The middle list would display categories of functions. If you see the function you want to use, you can use it. If the right list is too long and you know the type of the function you are looking for, you can click its category in the middle list and locate it in the right list. Once you see the function you want in the right list, you can double-click it. If it is a parameter-less function, its name and parentheses would be added to the expression area: ![]() If the function is configured to take arguments, its name and a placeholder for each argument would be added to the expression area: ![]() You must then replace each placeholder with the appropriate value or expression. To assist you with functions, in its bottom section, the Expression Builder shows the syntax of the function, including its name and the name(s) of the argument(s). To get more information about a function, click its link in the bottom section of the Expression Builder. A help window would display. Here is an example: ![]() Besides the built-in functions, if you had created a function in the current database, in the left list, click the name of the database, its function(s) would display in the middle list. Depending on the object that was clicked in the left list, the middle list can display the Windows controls that are part of, or are positioned on, the form or report. For example, if you click the name of a form in the left list, the middle list would display the names of all the controls on that form. To use one of the controls on the object, you can double-click the item in the middle list. When you do, the name of the control would appear in the expression area. Some items in the middle list hold their own list of items. To show that list, you must click an item in the middle list. For example, to access the properties of a control positioned on a form, in the left list, expand the Forms node and expand All Forms: ![]() Then, in the left list, click the name of a form. This would cause the middle list to display the controls of the selected form. To access the properties of the control, click its name in the middle list. The right list would show its properties: ![]() As mentioned already, after creating the expression, if you are satisfied with it, click OK.
| | |||||||||||||||||||
|
Tuesday, June 11, 2013
Introduction to Data Expressions
Related Posts
Subscribe to:
Post Comments (Atom)
EmoticonEmoticon