Respond to Data Entry Events Callahan Chapter 4 Understanding Form and Control Events Developer s Goal make data entry easy, fast, complete, accurate Many form- and control-level events occur as user works within your application Events provide an opportunity for your application to respond to user action Table on Callahan page 68 Event Demonstrator database and webcast Covered in this chapter/webcast Form Events BeforeUpdate, KeyDown Control Events Enter, Exit, KeyDown, KeyPress, KeyUp, AfterUpdate 1
Performing Actions as the User Moves in a Form Several events occur as user navigates controls on a form we experimented with them in the Event Demonstrator database Enter and Exit only occur when a user navigates to another control on the same form user clicks another control on the same form Exit(1 st control) LostFocus(1 st control) Enter (2 nd control) GotFocus(2 nd control) Enter and Exit don t occur when a user navigates to another control on a different form user clicks a control on a different form LostFocus(control) Deactivate(form 1) Activate(form 2) GotFocus(control) Changing a Control s Property Setting Change the Background Color of a Combo Box to indicate that was the active control Private Sub Combo51_Enter() Me.AllowEdits= True Combo51.BackColor = 16777215 'change background to white Private Sub Combo51_Exit(Cancel As Integer) Me.AllowEdits= False Combo51.BackColor = 12632256 'change background to gray If the control is on same form whose VB code is executing Me.controlname.property = value If the control is on another open form Forms!formname!controlname.property = value 2
Performing Actions as the User Changes Data AfterUpdateevent (control) occurs after a control s data has been updated, and just before the control is exited Have the Dear field fill automatically whenever the First Name field is changed but only when the Dear field is blank (don t overwrite an existing Dear value) Private Sub FirstName_AfterUpdate() 'Copy the FirstNamevalue to the Dear control If IsNull(Dear) Then Dear = FirstName Responding to Keyboard Events KeyPress event occurs when a user presses and releases a key or key combination that corresponds to an ANSI code while a form or control has the focus ignores function keys, navigation keys, SHIFT, CTRL, ALT KeyUp event occurs when a user releases a key while a form or control has the focus KeyDown event occurs when a user presses a key while a form or control has the focus typically used to trigger code when a user presses function keys (F1-F12) navigation keys (Home, End, Page Up, Page Down,,,,, Tab) combinations of keys involving SHIFT, CTRL, ALT numeric keypad top-row number keys Event Demonstrator database 3
Using KeyDown KeyCode an integer that indicates which key was pressed Shift an integer that indicates the status of Shift, Control, and Alt keys 0 = none 1= Shift 2= Ctrl 4= Alt any other value is a sum indicating multiple keys held (eg: 3=[Ctrl]+[ Shift]) Define [Ctrl]+[1] as keyboard shortcut to automatically enter City, State, Country for New York contacts Private Sub City_KeyDown(KeyCode As Integer, Shift As Integer) ' If key is Ctrl+1, enter the values for New York If KeyCode = 49 And Shift = 2 Then 'Ctrl+1was pressed City = "New York" StateOrProvince = "NY" Country = "USA" Make the Keyboard Handler Work Anywhere on the Form By default, only the active control receives keystrokes KeyPreview property a form property that determines whether the form receives keyboard events before passing them to the active control used here so the [Ctrl]+[1] shortcut could work anywhere on the form, not just the City field Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) ' If key is Ctrl+1, enter the values for New York If KeyCode = 49 And Shift = 2 Then 'Ctrl+1was pressed City = "New York" StateOrProvince = "NY" Country = "USA" 4
Practice Time Open Artie s List database frmrestaurant What happens when a user presses [PageUp] or [PageDown]? Suppose we want to disable [PageUp] and [PageDown] no matter which control the user is in how learn codes for [PageUp] and [PageDown]? Help System Access Object Events Form KeyDown event how can you make your VBA code ignore a keystroke? build this code, then test that [PageDown][PageUp] are now ignored Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) ' Ignore PageUp/PageDn keys If KeyCode = vbkeypageup Or KeyCode= vbkeypagedownthen KeyCode= 0 Validating Data Access provides many opportunities to validate data Data type (field) eg: Access rejects the value abc in a Number field Input Mask property (field) PostalCode, WorkPhone, MobilePhone, FaxNumber fields used them Required property (field) Access won t store the record if a Required field is left Null required each record have a LastName and FirstName Validation Rule property (field-level or record-level) used a record-level Validation Rule to require a work or mobile phone number ([WorkPhone] & [MobilePhone]) Is Not Null Before Update event (form) the most versatile way to validate form data fires just before a new or modified record is saved used to execute VBA code to check the form s data, then either allow the save or cancel the save & require corrective action 5
Using Form_BeforeUpdate Private Sub Form_BeforeUpdate(CancelAs Integer) ' if the user entered an address, check for a postal code Dim strmessage As String Dim intoptions As Integer variables = named storage locations in memory Dim bytchoiceas Byte If Not IsNull(Address) And IsNull(PostalCode) Then strmessage = "You didn't enter a postal code. Save anyway?" intoptions = vbquestion+ vbokcancel bytchoice= MsgBox(strMessage, intoptions) If bytchoice = vbcancelthen PostalCode.SetFocus Cancel = True ' go back to PostalCode ' cancel saving the record Dueling Message Boxes Message Box statement (Callahan 2) displays a message in a modal dialog box code execution halts until after the message box is closed syntax MsgBox message[, icontype][, title] example MsgBox "This is a Message", vbcritical, "Title Goes Here" Message Box function displays message in a modal dialog box, waits for user to click a button, returns an integer indicating which button was clicked code execution halts until after the message box is closed syntax MsgBox(prompt[, buttons][, title][, helpfile, context]) example intanswer= MsgBox("Are You Sure?", vbyesno+ vbquestion, "Confirm File Deletion") Demo: MsgBox Function in Immediate Window and Help 6