Form’s Events ISYS 562. Form Events Opening & closing form:Open, Close, Load, Unload Moving to and...

Post on 20-Dec-2015

225 views 1 download

Tags:

Transcript of Form’s Events ISYS 562. Form Events Opening & closing form:Open, Close, Load, Unload Moving to and...

Form’s Events

ISYS 562

Form Events

• Opening & closing form:Open, Close, Load, Unload• Moving to and from the form: Activate, deactivate• Moving from control to control: Enter, Exit, Getfocus,

Lostfocus• Pressing keys: KeyDown, KeyUp, KeyPress• Clicking mouse: Click, DblCLick, MouseUp, MouseWheel• Changing data: dirty, BeforeUpdate, AfterUpdate, Undo• Adding records: BeforeInsert, AfterInsert• Deleting records: Delete, BeforeDelConfirm,

AfterDelConfirm• Filtering records: Filter, ApplyFilter

Order of events for controls on forms

Moving the focus to a control• When you move the focus to a control on a form— for example, by

opening a form that has one or more active controls or by moving to another control on the same form— the Enter and GotFocus events occur in this order: Enter → GotFocus

• If you are opening a form, these events occur after the events associated with opening the form (such as Open, Activate, and Current), as follows:– Open (form) → Activate (form) → Current (form) → Enter (control) → GotFocus (control)

• When the focus leaves a control on a form— for example, when you close a form that has one or more active controls or move to another control on the same form— the Exit and LostFocus events occur in this order: Exit → LostFocus

• If you are closing a form, the Exit and LostFocus events occur before the events associated with closing the form (such as Unload, Deactivate, and Close), as follows:– Exit (control) → LostFocus (control) → Unload (form) → Deactivate

(form) → Close (form)

Changing and updating data in a control• When you enter or change data in a control on a form and then move the

focus to another control, the BeforeUpdate and AfterUpdate events occur:– BeforeUpdate → AfterUpdate

• The Exit and LostFocus events for the changed control occur after the BeforeUpdate and AfterUpdate events:

– BeforeUpdate → AfterUpdate → Exit → LostFocus

• When you change the text in a text box or in the text box portion of a combo box, the Change event occurs. This event occurs whenever the contents of the control change, but before you move to a different control or record (and thus, before the BeforeUpdate and AfterUpdate events occur). The following sequence of events occurs for each key you press in a text box or in the text box portion of a combo box:

– KeyDown → KeyPress → Dirty → Change → KeyUp

• The NotInList event occurs after you enter a value in a combo box that isn't in the combo box list and then attempt to move to another control or record. It occurs after the keyboard events and the Change events for the combo box, but before any other control or form events. If the LimitToList property of the combo box is set to Yes, the Error event for the form occurs immediately after the NotInList event:

• KeyDown → KeyPress → Dirty → Change → KeyUp → NotInList → Error

Order of events for records on forms

• Events occur for records on forms when you move the focus to a record, update data in a record, delete an existing record or records, or create a new record.

Moving the focus to records and updating data in records

• When you move the focus to an existing record on a form, enter or change data in the record, and then move the focus to another record, the following sequence of events occurs for the form:

– Current (form) → BeforeUpdate (form) → AfterUpdate (form) → Current (form)

• When you leave the record whose data has changed, but before you enter the next record, the Exit and LostFocus events occur for the control with the focus. These events occur after the BeforeUpdate and AfterUpdate events for the form, as follows: BeforeUpdate (form) → AfterUpdate (form) → Exit (control) → LostFocus (control) → RecordExit (form) → Current (form)

• As you move the focus among the controls on a form, events occur for each control. For example, the following sequences of events occur when you:

• Open a form and change data in a control: Current (form) → Enter (control) → GotFocus (control) → BeforeUpdate (control) → AfterUpdate (control)

• Move the focus to another control: Exit (control1) → LostFocus (control1) → Enter (control2) → GotFocus (control2)

• Move the focus to another record: BeforeUpdate (form) → AfterUpdate (form) → Exit (control2) → LostFocus (control2) → RecordExit (form) → Current (form)

Changing and updating data in a control

• When you enter or change data in a control on a form and then move the focus to another control, the BeforeUpdate and AfterUpdate events occur.

• The Exit and LostFocus events for the changed control occur after the BeforeUpdate and AfterUpdate events:– BeforeUpdate → AfterUpdate → Exit → LostFocus

Deleting records

• When you delete a record, the following events occur for the form, and Microsoft Access displays a dialog box asking you to confirm the deletion:– Delete → BeforeDelConfirm → AfterDelConfirm

• If you cancel the Delete event, the BeforeDelConfirm and AfterDelConfirm events don't occur and the dialog box isn't displayed.

Creating a new record

• When you move the focus to a new (blank) record on a form and then create a new record by typing in a control, the following sequence of events occurs:– Current (form) → Enter (control) → GotFocus

(control) → BeforeInsert (form) → AfterInsert (form)

• The BeforeUpdate and AfterUpdate events for the controls on the form and for the new record occur after the BeforeInsert event and before the AfterInsert event.

Keyboard events

• When you press and release a key while a control on a form has the focus (or use the SendKeys action to send a keystroke), the following sequence of events occurs:– KeyDown → KeyPress → KeyUp

Mouse Events

• When you press and release a mouse button while the mouse pointer is on a control on a form, the following sequence of events occurs for the control:– MouseDown → MouseUp → Click

• If a control has the focus and you click another control to move the focus to this second control, the following sequences of events occur:

– First control: Exit → LostFocus– Second control: Enter → GotFocus → MouseDown → MouseUp → Click

VB Modules

• Standard modules:– Standard modules are separated database objects

containing one or many procedures. They most often contains utilities functions that are useful in many different circumstances.

– Create a standard module:• In the database window, click Modules and New.

• Form/Report modules: Containing procedures belong to a form/report– Create a form module:

• In the Form Design view, click the Code button

VB Editor

Event Procedure

• Create an event procedure:– Select control– Select control’s event

• Event procedure’s argument:Private Sub CITY_BeforeUpdate(Cancel As Integer)

End SubPrivate Sub CITY_KeyDown(KeyCode As Integer, Shift As Integer)

End SubPrivate Sub Command10_Click()

End Sub

Keep ComboBox (listBox) Synchronized with the Form

• Use Form’s OnCurrent event:– Example: A form to display employee table

with a comboBox of empID:• ComboBoxName = EmpID

Filtering Form Data• Filter By Selection button (Form View Toolbar or

Format Toolbar)– Example: While viewing employee record, click Race

field, and click Filter By Selection button.– Click the Remove Filter to remove the filter.

• Filter By Form button:– Click the button– Click the field and select a value (may select many

fields)

• Advanced Filter/Sort :– Format toolbar: Record/Filter– QBE window

Form’s Filter Event

• Triggered when a user chooses the Filter By Form or Advanced Filter/Sort command.

Form’s Filtering Properties

• Form’s Filter property:– Me.Filter = “sex = ‘male’”

• Form’s FilterOn property– True if we apply the filter

Using Filter Properties

Private Sub Command16_Click()Me.Filter = "race='" & Race & "'"Me.FilterOn = TrueEnd Sub

Private Sub Command17_Click()Me.FilterOn = FalseEnd Sub

Automatically Fill an Unbound Textbox with a Calculated Result

• Example:– Employee pays $50 for an event if

salary<50000– Otherwise, $100.

Private Sub Form_Current()List14 = EmpIDIf Salary <= 50000 Then Text24 = 50 Else Text24 = 100End IfEnd Sub

Label

• Format– Caption, Visible

• Data– SmartTag

• Event

• Other– Name– ControlTip text

TextBox

• Format– Format, Visible

• Data– Control source– Inputmask– Validation rule– Default value– Smart tags

• Event– OnDirty, BeforeUpdate, AfterUpdate

Creating a Bound Form Using Design View

• Form: Record source

• Textbox: – Control source– Format– Inputmask

• Buttons: Move Next, Move Previous

SmartTag with a TextBox

• Name

• Email address

Using Textbox’s Events• After entering annual income in an unbound box, Use

the BeforeUpdate to check input, then use the AfterUpdate event to display event ticket fee based on the following rules:– If income <= 50000 then

• Fee = 100

– Else• Fee = 200

Private Sub Text6_AfterUpdate() If Text6 <= 50000 Then Text8 = 100 Else Text8 = 200 End IfEnd SubPrivate Sub Text6_BeforeUpdate(Cancel As Integer) If Text6 <= 10000 Then MsgBox ("Must be greater than 10000") Cancel = True End IfEnd Sub

Required Field

• Use textbox’s validation rule:– Not Null

• Use textbox’s BeforeUpdate event:– If isNull(text2) then

• Msgbox(“Must enter a value”)• Cancel=true

– End if

Responding to Keyboard Events

• Events:– KeyPress:

• Normal characters– Letters, numbers

– KeyDown, KeyUp:• Handle the exact keys that are being pressed

including combinations that use special keys such as Shift, Alt, Ctrl

VBA Constants

• In VB Editor view:– View/Object Browser– Choose VBA

• Type constant to search– Color Constants– Key Code constants

KeyDown Event Arguments

• Textbox: City– Private Sub CITY_KeyDown(KeyCode As Integer,

Shift As Integer)– KeyCode: – Shift:

• = 1 if Shift key is pressed• = 2 if Control key is pressed• = 3 if Alt key is pressed

Example of Using KeyDown Event to Enter Data

Private Sub CITY_KeyDown(KeyCode As Integer, Shift As Integer)If Shift = 2 Then If KeyCode = vbKey1 Then CITY = "san francisco" RATING = "A" ElseIf KeyCode = vbKey2 Then CITY = "los angeles" RATING = "A" End IfEnd IfEnd Sub

Option Group

• Option Group wizard

• Testing Option Group selection:– If Frame18 = 1 Then

• Me.Filter = "sex='male'"

• Else

• Me.Filter = "sex='female'"– End If

Private Sub Frame18_AfterUpdate()If Frame18 = 1 Then Me.Filter = "sex='male'" Else Me.Filter = "sex='female'"End IfMe.FilterOn = TrueEnd Sub

CheckBox

• If checked, the checkbox has a value of -1

• If unckecked, it has a value of 0.

If Check17 = -1 Then MsgBox ("You check check17")Else MsgBox ("You uncheck chk17") End If

Option Box (Radio Button)

If Option19 = -1 Then MsgBox ("You check OptionBox")Else MsgBox ("You uncheck OptionBox")End If

ListBox

• Format:– Column count

• Data:– Control source– Row source type– Row source

• Listbox’s value:– MsgBox (List23)

Access Macros

ISYS 562

Macro’s Actions

• OpenForm, OpenReport• TransferDatabase, TransferSpreadsheet• OutputTo• CopyDatabaseFile, CopyObject• FindNext, FindRecord• RunCode, RunCommand, RunSQL, RunApp,

RunMacro• SendObject• SetValue

Macro Condition

• You can use any expression that evaluates to True/False or Yes/No in a macro condition. The macro will be executed if the condition evaluates to True .

• To add a condition:– At macro design view:

• View/Condition

Convert Macro to VB

• Tools/Macro/Convert macros to VB

• Each action is converted to an Access DoCmd command.

• To view DoCmd command:– Object Browser– Choose Access– Search DoCmd