Download presentation
Presentation is loading. Please wait.
Published byDonald Gilmore Modified over 9 years ago
1
S511 Session 9, IU-SLIS 1 DB Implementation: MS Access Macros & Expressions
2
S511 Session 9, IU-SLIS 2 Outline Expressions in Access Access Macros ► Macro Actions ► Functions ► Creating a Macro MS Access File for Lecture
3
S511 Session 9, IU-SLIS 3 MS Access Expressions: Intro What is an “expression” in MS Access? ► Combination of functions, operators, constants, & identifiers that evaluates to a single value Identifier name of field, control, or property (e.g., Forms![Orders]![OrderID]) Operator arithmetic (+ - * /), comparison (= ), logical (Not And Or), text (& +) ► e.g., =Sum([Purchase Price])*0.08 What does it do? ► Perform calculations ► Manipulate (e.g., combine, extract) text ► Check data values Expression Builder ► MS Access tool to help build expressions ► Can select from the lists of Built-in functions, operators, and constants. Identifiers Database object (table, query, form, report) Object elements (fields, controls, properties) MS Access 2007: Expression Builder
4
S511 Session 9, IU-SLIS 4 Access Expressions: Usage Where to use expressions ► Control Source property in Form or Report = DateAdd("yyyy",10,[Employees]![HireDate]) ► Default property in Form or Report = Date() ► Validation Rule property in Table or Form In ("Mr.","Mrs.","Ms.") ► Criteria in Query Between #1/1/1998# AND #12/31/1998 ► Calculated Field in Query Age: DateDiff(“yyyy”,[BirthDate],Date()) ► SQL Statements in Query or Macro ► Action Arguments and Conditions in Macro ► Arguments for VBA functions, statements, and methods
5
S511 Session 9, IU-SLIS 5 Access Expressions: Specifying Values Specify values in expressions using ► Contants (i.e, literal values) number, string, or dates; exactly as written e.g. 10, “Bloomington”, #1/1/1998# Access contants True, False, Null, “” (empty string) ► Functions Returns values of calculation/operation e.g. Date(), Sum([Sales]) ► Field, Control, Property Identifiers Syntax ObjectType![ObjectName]![Control/FieldName].PropertyName e.g. Forms![Orders]![OrderDate].DefaultValue ! operator indicates that what follows is a user-defined item.. operator indicates what follows is an item defined by MS Access. Only need to specify enough parts of an identifier to make it unique MS Access 2007: Expression Syntax
6
S511 Session 9, IU-SLIS 6 Access Expressions: Functions FunctionDescriptionExample Date()Returns system date Date() 10/16/2007 DatePart(interval, date)Extracts part of a date DatePart(“yyyy”,Date()) 2007 DateDiff(interval, date1,date2)Computes difference between two dates DateDiff(“d”,#9/11/2007#,Date()) 35 DateAdd(interval, number,date)Returns the date with a time interval added DateAdd(“m”,2,Date()) 12/16/2007 Iif(expr, trueval, falseval)Returns a truth value of an expressionIIf([Due Date]<Date(),"OVERDUE", IIf([Due Date]=Date(),"Due","Not Yet Due")) InStr(start, string1, string2)Returns a position of string1 in string2 Instr(1,” “,“Kiduk Yang”) 6 Left(string, length)Returns a substring from the left Left(“Kiduk Yang”, 5) “Kiduk” Right(string, length)Returns a substring from the right Right(“Kiduk Yang”, 4) “Yang” Mid(string, start,length)Returns a substring from start Mid(“Kiduk Yang”, 7) “Yang” Len(string)Returns the number of characters in a string Len(“Kiduk Yang”) 10 Switch(expr1,val1,expr2,val2,..)Returns the value associated w/ first true exprSwitch([City]=“Mexico”,”Spanish”, [City]=“Seoul”, “Korean”) Nz(variant, value)Converts the Null value of a variant to a valueNz([UnitsInStock],0) + Nz(UnitsInOrder,0) DLookup(expr, domain, criteria)Returns the field value of a specific recordDLookup(“[LastName]”,”EMPLOYEE”,”[EmpID]=1”) DSum(expr, domain, criteria)Sum the field values of a specific record setDSum(“[Price]”,”SALES”,”Month([SaleDate])=10”) MS Access 2007: Functions MS Access 2007: Operators
7
S511 Session 9, IU-SLIS 7 Access Expressions: Examples =IIf(IsNull([Initial]), [FName]&“ ”&[LName], [FName]&“ ”&[Initial]&“ ”&[LName]) ► Display “Firstname Lastname” if no initial, “Firstname Initial Lastname” otherwise =[Sales]/Sum([Sales]) ► Display the proprotion of current sales =DLookup(“[Name]”, “qryEmployees”, “[EmpD]=Forms![frmEmployees][EmpID]”) ► Display Name in qryEmployees, whose EmployeeID matches that entered in the active form =DSum(“[OrderAmount]”, “Orders”, “[CustomerID]=‘Doe’”) ► Display the total order amount of the cutomer “Doe” Year([OrderDate])=Year(Now()) AND Month([OrderDate])=Month(Now()) ► Query Criteria: where order date is in current year and month Areacode: Mid([Phone], 2, 3) ► Query Field: display 3 characters starting with the 2 nd character in [Phone] = Nz([UnitsInStock],0) + Nz(UnitsInOrder,0) ► Form Control Source: show total number of units SELECT ALL from [Employees] WHERE [LastName]=“Doe” ► SQL: return Employees rows with LastName = “Doe”
8
S511 Session 9, IU-SLIS 8 Programming in MS Access Modules ► An organized collection of Visual Basic for Application (VBA) code ► More flexible and powerful than macros, but less secure Macros ► A named set of actions that perform operations/tasks e.g. open/close a form, print a report, set value of a control ► Tool to automate tasks and extend functionalities without programming Macro actions/functions = subset of commands available in VBA ► Command Button Wizard To perform Common Tasks ► Macro Builder Build a list of actions to perform by selecting from list of actions
9
S511 Session 9, IU-SLIS 9 Access Macros: Elements Event ► The reason the macro “fires” or runs. i.e., the trigger for a macro execution e.g., a form control gaining/losing focus, mouse click/movement Action ► What the macro does e.g. load/close/delete object, set field/control value Action Arguments (properties) ► What action applies to e.g. frmEmployee Conditions ► Conditions that must be true for the macro to run e.g. IsNull(Forms![frmEmployee]![Status])
10
S511 Session 9, IU-SLIS 10 Access Macros: Events Event is the reason the macro “fires” or runs. ► A macro’s execution should be triggered by a specific event ► Successful macro use depends on attaching to appropriate events good macro design. ► Example After Update vs. Lost Focus event loss of focus can occur without data entry or edit for data entry trigger, use “After Update” event Associating Events and Macros ► Set the appropriate Event Property of the control to the name of the macro that will perform desired actions.
11
S511 Session 9, IU-SLIS 11 Access Macros: Grouping Simple/Embedded Macro ► A single macro ► Actions are executed sequentially from top to bottom Macro Group ► Related macros grouped together in a macro object ► Individual macros in a macro group are referenced by MacroGroupName.MacroName e.g. mcrOpenCloseGRP.CloseImOpen
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.