form’s events isys 562. form events opening & closing form:open, close, load, unload moving to...

40
Form’s Events ISYS 562

Post on 20-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

Form’s Events

ISYS 562

Page 2: 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

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

Page 3: 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

Order of events for controls on forms

Page 4: 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

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)

Page 5: 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

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

Page 6: 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

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.

Page 7: 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

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)

Page 8: 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

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

Page 9: 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

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.

Page 10: 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

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.

Page 11: 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

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

Page 12: 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

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

Page 13: 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

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

Page 14: 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

VB Editor

Page 15: 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

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

Page 16: 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

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

Page 17: 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

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

Page 18: 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

Form’s Filter Event

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

Page 19: 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

Form’s Filtering Properties

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

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

Page 20: 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

Using Filter Properties

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

Private Sub Command17_Click()Me.FilterOn = FalseEnd Sub

Page 21: 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

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

Page 22: 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

Label

• Format– Caption, Visible

• Data– SmartTag

• Event

• Other– Name– ControlTip text

Page 23: 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

TextBox

• Format– Format, Visible

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

• Event– OnDirty, BeforeUpdate, AfterUpdate

Page 24: 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

Creating a Bound Form Using Design View

• Form: Record source

• Textbox: – Control source– Format– Inputmask

• Buttons: Move Next, Move Previous

Page 25: 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

SmartTag with a TextBox

• Name

• Email address

Page 26: 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

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

Page 27: 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

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

Page 28: 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

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

Page 29: 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

VBA Constants

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

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

Page 30: 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

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

Page 31: 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

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

Page 32: 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

Option Group

• Option Group wizard

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

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

• Else

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

Page 33: 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

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

Page 34: 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

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

Page 35: 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

Option Box (Radio Button)

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

Page 36: 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

ListBox

• Format:– Column count

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

• Listbox’s value:– MsgBox (List23)

Page 37: 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

Access Macros

ISYS 562

Page 38: 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

Macro’s Actions

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

RunMacro• SendObject• SetValue

Page 39: 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

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

Page 40: 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

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