Create an expression Access 2007
Author: mety Labels:: Create an expression Access 2007An expression is a combination of some or all of the following: built-in or user-defined functions (Function procedure: A procedure that returns a value and that can be used in an expression. You declare a function with the Function statement and end it with the End Function statement.), identifiers (identifier (expressions): An element of an expression that refers to the value of a field, control, or property. For example, Forms![Orders]![OrderID] is an identifier that refers to the value in the OrderID control on the Orders form.), operators (operator: A sign or symbol that specifies the type of calculation to perform within an expression. There are mathematical, comparison, logical, and reference operators.), and constants (constant: A value that is not calculated and, therefore, does not change. For example, the number 210, and the text "Quarterly Earnings" are constants. An expression, or a value resulting from an expression, is not a constant.). Each expression evaluates to a single value. For example, the following expression contains all four of these elements: =Sum([Purchase Price])*0.08 In this example, Sum() is a built-in function, [Purchase Price] is an identifier, * is a mathematical operator, and 0.08 is a constant. This expression can be used in a text box in a form footer or report footer to calculate sales tax for a group of items. Expressions can be much more complex or much simpler than this example. For example, this Boolean (Boolean: An expression that can be evaluated as either true (nonzero) or false (0). You can use the keywords True and False to supply the values of -1 and 0, respectively. The field data type Yes/No is Boolean and has the value of -1 for Yes) expression consists of just an operator and a constant: >0 This expression returns True when it is compared to a number that is greater than 0, and returns False when it is compared to a number that is less than 0. You can use this expression in the Validation Rule property of a control or table field to ensure that only positive values are entered. In Access, expressions are used in many places to perform calculations, manipulate characters, or test data. Tables, queries, forms, reports, and macros all have properties that accept an expression. For example, you can use expressions in the Control Source and Default Value properties for a control. You can also use expressions in the Validation Rule property for a table field. In addition, when you write Microsoft Visual Basic for Applications (VBA) code for an event procedure or for a module, you often use expressions that are similar to those that you use in an Access object, such as a table or query. The following sections describe the most common ways in which you can use expressions. Use expressions in form and report controlsWhen you use an expression as the data source for a control, you create a calculated control. For example, suppose that you have an order form that displays multiple order records, and you want to create a subtotal in the form footer that sums all of the line items on the form.To calculate the subtotal, place a text box control in the form footer, and then set the ControlSource property of the text box to the following expression: =Sum([table_field]) In this case, table_field is the name of the field that contains your subtotal values. That field can reside in a table or a query. The Sum function calculates the total for a set of values from your record source. The following procedure shows how to enter an expression in a text box control. Enter an expression in a text box control
Use expressions as query criteriaYou can use an expression to define criteria in a query. Access then returns only those rows that match the criteria. For example, suppose that you want to see all the orders whose shipped date occurred in the year 2004. To enter the criteria, you type the following expression in the Criteria cell for the Date/Time column in your query. This example uses a Date/Time column called ShippedDate. To define a date range, enter your criteria in this manner:Between #1/1/2004# And #12/31/2004# The ShippedDate column will look similar to the following. For each record in the Orders table, if the value in the ShippedDate column falls in the date range that you specify, the record is included in the query output. Note that in the expression, you enclose the dates with pound signs (#). Access treats a value enclosed in pound signs as a Date/Time data type. Treating those values as date/time data enables you to perform calculations on those values, such as subtracting one date from another. Enter criteria in the query design grid
Use expressions to create calculated fields in a queryYou can use an expression to create a calculated field in a query. For example, suppose that you are designing a query, and you want to display the year in which an order was shipped. To create the calculated field, you enter the following expression in a blank cell in the Field row in your query:Shipped Date: Format([ShippedDate],"yyyy") In this case, the expression uses the Format function to extract the year from the values — the year is contained in a field called ShippedDate. The Format function retrieves the shipped date from that field, uses the "yyyy" argument to format the date to show only the four digits of the year, and places the resulting value in a new column. Prefacing the expression with the text Shipped Date: names the new column Shipped Date. This name is often called an alias (alias (SQL): An alternative name for a table or field in expressions. Often used to shorten the table or field name for subsequent references in code, to prevent possible ambiguous references, or to provide a more descriptive name in query output.). If you do not supply an alias, Access will create one, such as Expr1. Create a calculated field in query Design view
Use expressions in the Validation Rule property of a table fieldExpressions are also useful for validating data as it is entered into the database. You validate data by entering an expression in the Validation Rule property of a field in a table. For example, suppose that you have a table called Inventory with a field called Units On Hand, and you want to set a rule that forces users to enter a value greater than or equal to zero. In other words, inventory can never be a negative number. You can do this by using the following expression as a rule in the Validation Rule property:>=0 Enter a validation rule for a field in a table
Find links to more information about creating data validation rules in the See Also section. Use expressions in the Validation Rule property of a controlIn addition to table fields, controls also have a Validation Rule property that can accept an expression. For example, suppose that you use a form to enter the date range for a report, and you want to ensure that the beginning date isn't earlier than 1/1/2004. You can set the Validation Rule and ValidationText properties for the text box where you enter the beginning date to the following.
If you try to enter a date earlier than 1/1/2004, a message appears. After you click OK, Access returns you to the text box. Tip Setting a validation rule for a table field enforces the rule for all users of the table; alternatively, setting a validation rule for a control on a form enforces the rule only for users of that form. Setting validation rules separately for table fields and for controls on forms can be useful if you want to establish different validation rules for different users. Enter a validation rule for a control
Find links to more information about creating data validation rules in the See Also section. Use expressions to set default values for a table fieldYou can use an expression to store a default value for a field in a table. For example, suppose that you want to automatically insert the date and time into a field called OrderDate when you add a new record. To do this, you can enter the following expression in the Default Value property:Now() Enter a default value for a field in a table
Find links to more information about setting default values in the See Also section. Use expressions to set default values for controlsAnother common place to use an expression is in the Default Value property of a control. The Default Value property of a control behaves similarly to the Default Value property of a field in a table. For example, to use the current date as the default value for a text box, you can use the following expression:Date() This expression uses the Date function to return the current date, but not the time. If you bind the text box to a table field, and the field has a default value, the control's default value takes precedence over the table field. It often makes better sense to set the Default Value property for the field in the table. That way, if you base a number of controls for different forms on the same table field, the same default value will apply to each control, helping ensure consistent data entry on each form. Enter a default value for a control
Use expressions to carry out macro actionsIn some cases, you may want to carry out an action or series of actions in a macro only if a particular condition is true. For example, suppose that you want a macro action to run only when the value in a text box equals 10. To set this rule, you use an expression to define the condition in the Condition column of the macro.In this example, assume the text box is named "Items." Your expression that sets the condition is [Items]=10. Enter a condition for a macro action
Use expressions to group and sort data in reportsYou use the Group, Sort, and Total pane to define grouping levels and sorting orders for the data in a report. The pane replaces the Sorting and Grouping dialog box that appeared in earlier versions of Access. You can display and use the Group, Sort, and Total pane only when you open a report in Design view — the pane appears in the same tab as your report.The following figure illustrates the pane as it appears when it first opens: Grouping is the process of combining columns that contain duplicate values. For example, suppose that your database contains sales information for offices in different cities, and that one of the reports in your database is named "Sales by City." The query that provides the data for that report groups the data by your city values. This type of grouping can make your information easier to read and understand. In contrast, sorting is the process of imposing a sort order on the rows (the records) in your query results. For example, you can sort records by their primary key values (or another set of values in another field) in either ascending or descending order, or you can sort the records by one or more characters in a specified order, such as alphabetical order. Add grouping and sorting to a report
Add an expression to an existing group or sort
Use the Expression Builder to create expressionsYou can use the Expression Builder to help build expressions. The Expression Builder provides easy access to the names of the fields and controls in your database, and to many of the built-in functions that are available to you when you write expressions.Understanding the Expression BuilderYou can think of the Expression Builder as a way to look up and insert components of an expression that you might have trouble remembering, such as identifier names (for example, fields, tables, forms, and queries), and function names and their arguments.You can use the Expression Builder to create a new expression, or you can select from prebuilt expressions, including expressions for displaying page numbers, the current date, and the current date and time. You can start the Expression Builder from most of the places in Microsoft Office Access 2007 where you would write expressions manually, such as the Control Source property of a control or the Validation Rule property of a table field. As a rule, if you see the Build button , you can click it to start the Expression Builder. Expression box The upper section of the builder contains an expression box where you construct your expression. You can type your expression in the box manually, or you can select elements from the three columns in the lower section of the Expression Builder and then add them to your expression box. To add an element, double-click it, and then click Paste. Operator buttons The middle section of the Expression Builder displays buttons for inserting common arithmetical and logical operators into your expression. To insert an operator in the expression box, click the appropriate button. To display a longer list of operators that you can use in expressions, click the Operators folder in the lower-left column that contains expression elements, and then click the category that you want in the middle column. The right column then lists all the operators in the selected category. To insert an operator, double-click it. Expression elements The lower section contains three columns:
To construct your expression, you can type text in the expression box and paste elements from the other areas in the Expression Builder. For example, you can click in the lower-left column to see any of the objects in your database, in addition to the available functions, constants, operators, and common expressions. When you click an item in the left column, the contents of the other columns change accordingly. For example, when you double-click Tables in the left column and then click the name of a table, the middle column lists the fields in that table. When you double-click Functions in the left column and then click Built-In Functions, the middle column lists all the function categories, and the right column lists all the functions for those categories. When you double-click a function to insert it into your expression, the function and the text that indicates the needed arguments for that function appear as placeholder text in the expression box. For example, if you double-click the IIf function, the Expression Builder adds the following to the expression box: IIf («expr», «truepart», «falsepart») You must replace the text «expr», «truepart», and «falsepart»with the actual argument values. You can do this directly in the expression box by clicking each placeholder, and then either typing the argument or selecting the argument by using the three expression element lists. If you double-click two or more functions in succession, for example, the IIf function and then the Round function, the Expression Builder adds both functions to the expression box, separated by the placeholder «Expr»: IIf («expr», «truepart», «falsepart») «Expr» Round («number», «precision») In addition to replacing the placeholders for the function arguments, You must replace the placeholder «Expr» with an operator before the overall expression will be valid. When you paste an identifier (the name of a table field or control) into your expression, the Expression Builder inserts only the parts of the identifier that are required in the current context. For example, if you start the Expression Builder from the property sheet of a form called Customers, and you then paste an identifier for the Visible property of the form into your expression, the Expression Builder pastes only the property name Visible. If you use this expression outside the context of the form, you must include the full identifier: Forms![Customers].Visible. Start the Expression Builder from a table
Start the Expression Builder from a form or report
Start the Expression Builder from a query
Table of operatorsAn operator is a sign or symbol that specifies the type of calculation to perform within an expression. There are mathematical, comparison, logical, and reference operators. Access supports a variety of operators, including arithmetic operators such as +, -, multiply (*), and divide (/), in addition to comparison operators for comparing values, text operators for concatenating text, and logical operators for determining true or false values. This article provides details about using these operators. Arithmetic operatorsYou use the arithmetic operators to calculate a value from two or more numbers or to change the sign of a number from positive to negative or vice versa.
Comparison operatorsYou use the comparison operators to compare values and return a result that is True, False, or Null.
Note In all cases, if either the first value or the second value is null, the result is then also null. Because null represents an unknown value, the result of any comparison with a null value is also unknown. Logical operatorsYou use the logical operators to combine two Boolean values and return a true, false, or null result. Logical operators are also referred to as Boolean operators.
Concatenation operatorsYou use the concatenation operators to combine two text values into one.
Special operatorsYou use the special operators to return a True or False result as described in the following table.
|