Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] - نسخه متنی

Alison Balter

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



What Are the Section and Control Events, and When Do You Use Them?


Sections have only five events: Click, DblClick, MouseDown, MouseMove, and MouseUp. These events rarely play significant roles in your application.

Each control type has its own set of events to which it responds. Many events are common to most controls, but others are specific to certain controls. Furthermore, some controls respond to very few events. The following sections cover all the Control events and the controls they apply to.

BeforeUpdate


The BeforeUpdate event applies to text boxes, option groups, combo boxes, list boxes, and bound object frames. It occurs before changed data in the control updates. The following code example is found in the BeforeUpdate event of the txtProjectTotalBillingEstimate control on the frmProjects form in the sample database:

Private Sub CustomerID_BeforeUpdate(Cancel As Integer)
'If project total billings are less than or equal to zero
'display a message to the user and cancel the update
If Me.txtProjectTotalBillingEstimate <= 0 Then
MsgBox "Project Total Billings Must Be Greater Than " & _
"or Equal to Zero", vbCritical, "Canceling Update"
Cancel = True
End If
End Sub

This code tests whether the value of the CustomerID control is less than or equal to zero. If it is, the code displays a message box, and the Update event is canceled.

AfterUpdate


The AfterUpdate event applies to text boxes, option groups, combo boxes, list boxes, and bound object frames. It occurs after changed data in the control updates. The following code example is from the AfterUpdate event of the txtBeginDate control on the frmPrintInvoice form found in the time and billing database:

Private Sub txtBeginDate_AfterUpdate()
'Requery the subforms when the begin
'date changes
Me.fsubPrintInvoiceTime.Requery
Me.fsubPrintInvoiceExpenses.Requery
End Sub

This code re-queries both the fsubPrintInvoiceTime subform and the fsubPrintInvoiceExpenses subform when the txtBeginDate control updates. This ensures that the subforms display the time and expenses appropriate for the selected date range.

Updated


The Updated event applies to a bound object frame only. It occurs when the object linking and embedding (OLE) object's data is modified.

Change


The Change event applies to text and combo boxes and takes place when data in the control changes. For a text box, this event occurs when the user types a character; for a combo box, it happens when a user types a character or selects a value from the list. You use this event when you want to trap for something happening on a character-by-character basis.

NotInList


The NotInList event applies only to a combo box and happens when a user enters a value in the text box portion of the combo box that's not in the combo box list. By using this event, you can allow the user to add a new value to the combo box list. For this event to be triggered, the LimitToList property must be set to Yes. Here's an example from the time and billing application's frmPayments form:

Private Sub cboPaymentMethodID_NotInList _
'If payment method is not in the list,
'ask user if they want to add it
If MsgBox("Payment Method Not Found, Add?", _
vbYesNo + vbQuestion, _
"Please Respond") = vbYes Then
'If they respond yes, open the frmPaymentMethods form
'in Add mode, passing in the new payment method
DoCmd.OpenForm "frmPaymentMethods", _
Datamode:=acFormAdd, _
WindowMode:=acDialog, _
OpenArgs:=NewData
'If form is still loaded, unload it
If IsLoaded("frmPaymentMethods") Then
Response = acDataErrAdded
DoCmd.Close acForm, "frmPaymentMethods"
'If the user cancels the add, redisplay the existing options
Else
Response = acDataErrContinue
End If
Else
'If the user responds no, redisplay the existing options
Response = acDataErrContinue
End If
End Sub

This code executes when a user enters a payment method that's not in the cboPaymentMethodID combo box. It asks the user if he wants to add the entry. If he responds yes, the frmPaymentMethods form displays. Otherwise, the user must select another entry from the combo box. The NotInList event is covered in more detail later in the "Handling the NotInList Event" section.

Enter


The Enter event applies to text boxes, option groups, combo boxes, list boxes, command buttons, object frames, and subforms. It occurs

before a control gets focus from another control on the same form and

before the GotFocus event. Here's an example from the time and billing application's frmTimeCards form:

Private Sub fsubTimeCards_Enter()
'If the user clicks to enter time cards, and the EmployeeID
'is Null, display a message and set focus back to the
'cboEmployeeID combo box
If IsNull(Me.EmployeeID) Then
MsgBox "Enter employee before entering time or expenses."
Me.cboEmployeeID.SetFocus
End If
End Sub

When the user moves into the fsubTimeCards subform control, its Enter event tests whether the EmployeeID has been entered on the main form. If it hasn't, a message box displays, and focus is moved to the cboEmployeeID control on the main form.

Exit


The Exit event applies to text boxes, option groups, combo boxes, list boxes, command buttons, object frames, and subforms. It occurs just before the LostFocus event.

GotFocus


The GotFocus event applies to text boxes, toggle buttons, option buttons, check boxes, combo boxes, list boxes, and command buttons. It takes place when focus moves to a control in response to a user action or when the SetFocus, SelectObject, GoToRecord, GoToControl, or GoToPage method is issued in code. Controls can get focus only if they're visible and enabled.

LostFocus


The LostFocus event applies to text boxes, toggle buttons, option buttons, check boxes, combo boxes, list boxes, and command buttons. It occurs when focus moves away from a control in response to a user action, or when your code issues the SetFocus, SelectObject, GoToRecord, GoToControl, or GoToPage methods.

NOTE

The difference between GotFocus/LostFocus and Enter/Exit lies in when they occur. If focus is lost (moved to another form) or returned to the current form, the control's GotFocus and LostFocus events are triggered. The Enter and Exit events don't take place when the form loses or regains focus. Finally, it is important to note that none of these events takes place when the user makes menu selections or clicks toolbar buttons.

Click


The Click event applies to labels, text boxes, option groups, combo boxes, list boxes, command buttons, and object frames. It occurs when a user presses and then releases a mouse button over a control. Here's an example from the time and billing application's frmProjects form:

Private Sub cmdToggleView_Click()
'If the caption of the control is View Expenses,
'hide the Projects subform and show the Project Expenses subform
'Change caption of command button to View Hours
If Me.cmdToggleView.Caption = "&View Expenses" Then
Me.fsubProjects.Visible = False
Me.fsubProjectExpenses.Visible = True
Me.cmdToggleView.Caption = "&View Hours"
'If the caption of the control is View Hours,
'hide the Project Expenses subform and show the Project subform
'Change caption of command button to View Expenses
Else
Me.fsubProjectExpenses.Visible = False
Me.fsubProjects.Visible = True
Me.cmdToggleView.Caption = "&View Expenses"
End If
End Sub

This code checks the caption of the cmdToggleView command button. If the caption reads "&View Expenses" (with the ampersand indicating a hotkey), the fsubProjects subform is hidden, the fsubProjectExpenses subform is made visible, and the caption of the cmdToggleView command button is modified to read "&View Hours". Otherwise, the fsubProjectExpenses subform is hidden, the fsubProjects subform is made visible, and the caption of the cmdToggleView command button is modified to read "&View Expenses".

NOTE

The Click event is triggered when the user clicks the mouse over an object, as well as in the following situations:

  • When the user presses the spacebar while a command button has focus

  • When the user presses the Enter key, and a command button's Default property is set to Yes

  • When the user presses the Escape key, and a command button's Cancel property is set to Yes

  • When an accelerator key for a command button is used

DblClick


The DblClick event applies to labels, text boxes, option groups, combo boxes, list boxes, command buttons, and object frames. It occurs when a user presses and then releases the left mouse button twice over a control. Here's an example from the time and billing application's fsubTimeCards form:

Private Sub cboWorkCodeID_DblClick(Cancel As Integer)
Dim strWorkCode As String
On Error GoTo Err_cboWorkCodeID_DblClick
'If the cboWorkCodeID is Null, set the
'strWorkCode variable to a zero-length string
'otherwise set it to the text in the combo box
If IsNull(Me.cboWorkCodeID.Text) Then
strWorkCode = "
Else
strWorkCode = Me.cboWorkCodeID.Text
End If
'If the cboWorkCodeID is Null, set the
'Text property to a zero-length string
If IsNull(Me.cboWorkCodeID) Then
Me.cboWorkCodeID.Text = "
Else
'Otherwise, set the cboWorkCodeID
'combo box to Null
Me.cboWorkCodeID = Null
End If
'Open the frmWorkCodes form modally
DoCmd.OpenForm "frmWorkCodes", _
DataMode:=acFormAdd, _
WindowMode:=acDialog, _
OpenArgs:=strWorkCode
'After the form is closed, requery the combo box
Me.cboWorkCodeID.Requery
'Set the text of the combo box to the value added
Me.cboWorkCodeID.Text = strWorkCode
Exit_cboWorkCodeID_DblClick:
Exit Sub
Err_cboWorkCodeID_DblClick:
MsgBox Err.Description
Resume Exit_cboWorkCodeID_DblClick
End Sub

In this example, the code evaluates the cboWorkCodeID combo box control to see whether it's Null. If it is, the text of the combo box is set to a zero-length string. Otherwise, a long integer variable is set equal to the combo box value, and the combo box value is set to Null. The frmWorkCodes form is opened modally. When it's closed, the cboWorkCodeID combo box is re-queried. If the long integer variable doesn't contain a zero, the combo box value is set equal to the long integer value.

MouseDown


The MouseDown event applies to labels, text boxes, option groups, combo boxes, list boxes, command buttons, and object frames. It takes place when a user presses the mouse button over a control,

before the Click event fires.

MouseMove


The MouseMove event applies to labels, text boxes, option groups, combo boxes, list boxes, command buttons, and object frames. It occurs as a user moves the mouse over a control.

MouseUp


The MouseUp event applies to labels, text boxes, option groups, combo boxes, list boxes, command buttons, and object frames. It occurs when a user releases the mouse over a control,

before the Click event fires.

KeyDown


The KeyDown event applies to text boxes, toggle buttons, option buttons, check boxes, combo boxes, list boxes, and bound object frames. It happens when a user presses a key while within a control; the event occurs repeatedly until the user releases the key. You can cancel it by setting KeyCode equal to Zero.

KeyUp


The KeyUp event applies to text boxes, toggle buttons, option buttons, check boxes, combo boxes, list boxes, and bound object frames. It occurs when a user releases a key within a control. It occurs only once, no matter how long the user presses a key.

KeyPress


The KeyPress event applies to text boxes, toggle buttons, option buttons, check boxes, combo boxes, list boxes, and bound object frames. It occurs when a user presses and releases an ANSI key while the control has focus. You can cancel it by setting KeyCode equal to Zero.

Understanding the Sequence of Control Events


Just as Form events take place in a certain sequence when the form is opened, activated, and so on, Control events occur in a specific sequence. You need to understand this sequence to write the event code for a control.

What Happens When Focus Is Moved to or from a Control?

When focus is moved to a control, the following events occur:

Enter->GotFocus

If focus is moving to a control as the form is opened, the Form and Control events take place in the following sequence:

Open(form)->Activate(form)->Current(form)->Enter(control) GotFocus(control)

When focus leaves a control, the following events occur:

Exit->LostFocus

When focus leaves the control because the form is closing, the following events happen:

Exit(control)->LostFocus(control)->Unload(form)->Deactivate(form) Close(form)

What Happens When the Data in a Control Is Updated?

When you change data in a control and then move focus to another control, the following events occur:

BeforeUpdate->AfterUpdate->Exit->LostFocus

After every character that's typed in a text or combo box, the following events take place before focus is moved to another control:

KeyDown->KeyPress->Change->KeyUp

For a combo box, if the NotInList event is triggered, it occurs after the KeyUp event.


/ 544