Logical Operations Introduction An operator is referred to as logical if it can be used to compare two values. The result of the comparison produces a True or False value. The IS Operator One of the logical operations you can perform on a field is to check whether it is NULL or not. To support this operation, Microsoft Access provides an operator named IS. The formula to use it is: something IS NULL The something value can be the name of a field. An example is: txtRadius IS NULL In this case, the database engine would check the status (not the value) of a control named txtRadius. If the txtRadius control is NULL, the expression produces a True value. The NOT Operator To negate an expression or the existence of a value, you can use the NOT operator. This operator is primarily used to reverse an IS NULL expression. For example, we have learned that False is the opposite of True. In the same way, True is the opposite of False. If you want to compare a value as not being True, the NOT TRUE expression would produce the same result as the False value. For the same reason, the expression Not False is the same as True. In the same way, you can use the NOT operator to negate an expression. Consider the following expression: txtRadius IS NOT NULL This time, the database engine would check the status of a control named txtRadius. If the control holds a value, the comparison would produce a False result (note this very importantly) but if the control doesn't hold a value, then the comparison would produce a True result. Logical Functions Introduction To support logic on actions that can be performed on the values of a database, besides the operators we saw above, Microsoft Access provides various functions. In the strict sense, because Microsoft Access is not a 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. To let you create a conditional statement, Microsoft Access provides a function named IIf. The Immediate If function, IIf(), needs three pieces of information in order to accomplish its assignment. The formula to follow is: IIf(Condition, WhatToDoIfConditionIsTrue, WhatToDoIfConditionIsFalse) As Boolean The first action this function performs is to evaluate a condition. The condition can 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. The Condition may lead to only one of two results, True or False, Yes or Not, 0 or not 0. It can be resumed as follows: IIf(condition, ifTrue, ifFalse) As Boolean Here is an example: =IIf([chkIsMarried]=True,"Is Married","Single") A Condition could also lead to more than one value but you are interested in only two outcomes. For example, the condition may compare the number of days a clothing item has been displayed to a certain number using a comparison operator. Here is an example: IIf(txtDaysInStore > 15, 0.20, "") Another Condition can result in more than two values. For example, imagine a clothing store wants to apply different discount rates to items based on the number of days the items have been displayed in the store. To apply such a scenario, you can call an IIf() function inside another. This is referred to as nesting. To nest an IIf() function in another, in place of the ifFalse placeholder, call another instance of IIf(). This would be done as follows: IIf(condition, ifTrue, IIf(condition-else, if-else-true, if-else-false)) As Boolean In the same way, you can nest an IIf() function in any other IIf() function. All you have to do is to replace an IIf() call with an if-false expression. Practical Learning: Using IIf
Value Checking Functions Non-Zero Checking When studying numeric types, we reviewed different functions that could be used to convert a value or an expression to the desired type. We saw CByte, CInt, CSng, CDbl, and CCur. Before performing any operation on the value held by a field, you should first convert it to the appropriate type. The conversion functions we mentioned assume that the field contains a value. Unfortunately, this 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) AS Appropriate Type 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 by the Value argument. This could be an expression or the name of a field (or control) 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. The second argument is optional and it would be used as the return value if the Value argument were null. Practical Learning: Using the Nz() Function
Field Emptiness A field or control is empty if it currently holds no value. To let you get this information, Microsoft Access provides a a function named IsEmpty. Its syntax is: IsEmpty(Value) As Boolean 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. Nullity Checking A problem you may encounter when involving a field in an operation is 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 let you check whether a field (or a value or a control) is null, Microsoft Access provides a function named IsNull. Its syntax is: IsNull(value) As Boolean Also used on fields, the IsNull() function checks the state of a field (remember, this functions does not check whether a field is empty or not; it checks whether the field has never contained a value). If the field it null, this function returns True. If the field is not null, this function returns False. Numerical Checking Before involving a value, a field, or a control to an arithmetic operation, you should check that it contains a valid number. To let you perform this checking, Microsoft Access provides a function named IsNumeric. Its syntax is: IsNumeric(value) As Boolean The IsNumeric() function returns True if its argument is a valid number. Otherwise, it returns False. Practical Learning: Checking a Numeric Value
|