Introduction to Expressions |
|
|
Practical Learning: Basic Expressions
|
VBA and Expressions |
Introduction |
As we will see in this and later chapters, the various functions available in Microsoft Access make it a valuable application to create good functional databases. Nevertheless, Microsoft Access is not a programming environment but a good platform to visually create databases. As your experience grows, you will face some demands that become difficult to achieve. For this reason, Microsoft Access ships with a
computer language called Visual Basic For Applications or VBA. Although in this book we will not explore computer programming, we will take a glimpse at what some of the code lines look like. |
Automatically Generated Code |
You may recall that when we created subforms, we selected only a few fields from the data source. This was purposely done so the user can have the handiest pieces of information about the data that the current form is related to. While the user is performing data entry, he may want to have more details about the data that is displaying. You can achieve this by calling the originating form. To implement this functionality, you can create a command button and let Microsoft Access write code that would take you straight to the related record only. |
Practical Learning: Generating VBA Code
|
Procedures and Functions |
Introduction to Procedures and Functions |
A procedure is a task that must be performed aside of a table, a form, or a report, to complement it. It is like an operation or an expression with the first difference that someone else created it and you can just use it. Microsoft Access ships with various procedures to perform different tasks. There are so many of them that we will review only those that are most regularly used. ProcedureName() End This syntax is very simplistic but indicates that the minimum piece of information a procedure needs is a name. The name allows you to refer to this procedure in other parts of the database. In reality, a procedure is meant to perform a task. This task would be defined or described in the body of the procedure. In our simple syntax, the body of the procedure would start just under its name and would stop just above the End word. The person who creates a procedure also decides what the procedure can do. Following our simple syntax, if we wanted a procedure that can open Solitaire, it could appear as follows: ProcedureExample() Open Solitaire End
Once a procedure has been created, it can be used. Using a procedure is referred to as calling it. To call a simple procedure like the above
ProcedureExample, you would just type its name. ReturnType FunctionName() End
The main difference, as you may see here is that 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, a sub procedure, or 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. |
Because most, if not all, of the built-in procedures you will use in Microsoft Access are functions, from now on, we will use only the word “function” but this refers to both types of procedures. |
Procedures, Functions, and their Arguments |
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 an argument. While a certain function may need one argument, another function would need many arguments. The number and types of arguments of a procedure depend on its goal. ReturnValue FunctionName(Argument) End 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 argument by 12.58, it would appear almost as follows: Decimal FunctionName(Argument) Argument * 12.58 End While one function may use only one argument, another function may use more than one, depending on its goal. When a function uses more than one argument, a comma separates them. A possible syntax would be: ReturnValue FunctionName(Argument1, Argument2, Argument_n) End If you were creating a function that adds its two arguments, it would appear almost as follows: NaturalNumber AddTwoNumbers(Argument1, Argument2) Argument1 + Argument2 End 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 arguments, it is called differently than a function that does not take any argument. We saw already how you can call a function that does not take any argument and assign it to a field using its Control Source. Here is an example:
If a function is taking one argument, when calling it, you must provide a value for the argument, otherwise the function would not work (when you display the form or report, Microsoft Access would display an
error).
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. Here is an example:
|
Default Arguments |
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. |
The Expression Builder |
There are three main ways you create expressions. If you know, based on experience, what the expression is made of, you can just write it in the Control Source of the field that will hold the expression. This technique is faster but prone to error. Creating an expression is a technique of giving a "made-up" value to a field. The field that would hold such an expression is referred to as unbound because it is not directly linked to any field of a table. Since the new value that constitutes the expression is in fact "assigned" to the expression-field, you must use the assignment operator which is "=". Therefore, the first symbol you will use in an expression is =.
Without being particularly explicit, the syntax of the function allows you to know the number and order of arguments of a function. To get more information about the function, you
can check its documentation in the help files.
If you double-click the name of a function in the right list, if the function takes one or more arguments, a placeholder would be created for each argument:
You have the responsibility to supply an appropriate value for each placeholder. To do that, you have two alternatives. If you know the value that should be used in a placeholder, you can just type it. Alternatively, you can use the operator buttons or the built-in functions to fill a placeholder. We should point out that not all functions are available in the list of functions of the Expression Builder but, as long as you know that a Visual Basic function is supported and you know its syntax, you can use it. |
Introduction to Built-In Functions |
Overview |
To ease your job with database implementation, Microsoft Access ships with many functions. These functions have been tested and are highly reliable, so much that in this book, we will use only the built-in functions. Because there are so many of them, they are classified in categories and you are not likely to use all or even most of them. |
Page Break References |
In the previous lesson, we mentioned that a form could be divided in various sections separated by a page break. Although such sections are created in the Design View of a form, Microsoft Access does not inherently provide a way to access them. You have two alternatives. You can use a macro or VBA. For one thing we will not use macros in this book. For another, we will afford to write one line of code to apply the functionality we need. |
Conversion Functions |
The first action you should take when dealing with the value retrieved from a field is to convert it to the appropriate type. There are various conversion functions adapted to the different possible kinds of values. The general syntax of the conversion functions is: ReturnType = FunctionName(Expression)
The expression could be of any kind. For example, it could be a string or value the user would have entered in a form. It could also be the result of a calculation performed on another field or function. The conversion function would take such a value, string, or expression and attempt to convert it. If the conversion is successful, the function would return a new value that is of the type specified by the ReturnType in our syntax. |
|
Practical Learning: Converting Data Values
|
Logical Functions |
Introduction |
In the strict sense, because Microsoft Access is not a traditional programming environment, it does not provide a feature called conditional or control statement, which is used to check a condition. Instead, it provides functions you can use to check that a condition is true or false. There are various functions that can be used to create an expression as complex as an algorithm would allow. It is important to note that, although most of these functions perform conditional checking, they do not return the same type of value. For this reason, you should choose the right function. Most of these functions will also be used in combination with other functions as necessary. |
Immediate If |
The most regularly used function to perform conditional statements is called IIf. The Immediate If function, IIf(), needs three pieces of information in order to accomplish its assignment. The syntax used is: IIf(Condition, WhatToDoIfConditionIsTrue, WhatToDoIfConditionIsFalse)
The first action this function performs is to evaluate a condition. This condition could involve an operation or the result of an operation. If the
Condition is true, then the function would execute the first expression, which is identified in our syntax as
WhatToDoIfConditionIsTrue. =IIf([cboGender]="M","Boy","Girl")
|
Practical Learning: Using IIf
|
Field Emptiness |
The conversion functions we reviewed above are effective if they are supplied a value to convert. In some of your expressions, you will provide a data field as argument to use by a function. Even if you decide to convert a field to the appropriate value, you may need to first check whether the field is empty. This can be taken care of by calling the IsEmpty() function. Its syntax is: IsEmpty(Value) This function checks its argument, which could be a field or an expression. If it is a field and the field is empty, this function returns True. If the field is not empty, the IsEmpty() function returns False. |
Practical Learning: Checking For Empty Field
|
Nullity Checking |
Another problem you may encounter when involving a field in an operation is to know whether the field has never contained a value. This operation is sometimes confused with that of checking whether a field is empty. Here is the difference (it is important to understand this because it is used in many other environments, including Microsoft Visual Basic and Microsoft SQL Server):
To check whether a field (or a value) is null, you can call the IsNull() function. Its syntax is: IsNull(Value) Also used on fields, the IsNull() function checks the state of a field (remember, this function does not check whether a field is empty or not; it checks if the field has ever contained a value). If the field it null, this function returns True. If the field is not null, this function returns False. |
Numerical Checking |
When the word operation is stated, most people think of a numeric value, an operator, and another numeric value. Such values must be valid in order for the operation to succeed. It certainly would not make sense to multiply a DateHired by a HourlySalary. Therefore, before involving a value or field to an operation, you should check that it contains a valid number. To perform this checking, Microsoft Access provides the IsNumeric() function. Its syntax is: IsNumeric(Value) The IsNumeric() function returns True if its argument is a valid number. Otherwise, it returns False. |
Practical Learning: Checking for Numeric Value
|
Non-Zero Checking |
We saw that, before performing any operation on the value held by a field, you should first convert it to the appropriate value. The conversion functions we mentioned assume that the field contains a value. Unfortunately, it is not always the case. Microsoft Access provides the Nz() function that can be used to check whether a field contains a value, whether the field is empty, or is Null. The syntax of this function is: Nz(Value, ValueIfNull) The first argument, Value is required. The second argument is optional. The pseudo-code for this function is: |
If the field contains a value Return that value Otherwise Return 0
In other words, this function considers the value provided as the Value argument. This could be an expression but it is usually used on a field that is involved in a calculation. If the
Value argument or the passed field is empty, this function returns 0. If the field contains a value, this function
returns it. The beauty of this function is that it provides a valuable safeguard for an operation. |
Practical Learning: Basic Expressions
|
MOUS Topics |
S20 | Use a Calculated Control on a form |
S31 | Create a calculated field |
|
||
Previous | Copyright © 2002-2016, FunctionX, Inc. | Next |
|