ComboBox.Exit 事件 (Access)ComboBox.Exit event (Access)

Exit 事件會在控制項上的焦點移到相同表單或報表上的另一個控制項之前發生。The Exit event occurs just before a control loses the focus to another control on the same form or report.

語法Syntax

運算式結束取消expression.Exit (Cancel)

_運算式_代表**ComboBox** 物件的變數。expression A variable that represents a ComboBox object.

參數Parameters

名稱Name 必要/選用Required/Optional 資料類型Data type 描述Description
CancelCancel 必要Required IntegerInteger 設定為 True 可取消事件。Set to True to cancel the event.

註解Remarks

此事件不適用的核取方塊、 選項按鈕或切換按鈕選項群組中。This event does not apply to check boxes, option buttons, or toggle buttons in an option group. 它只適用於選項群組本身。It applies only to the option group itself.

若要發生此事件時執行巨集或事件程序,將 OnExit 屬性設定為巨集名稱或 [事件程序]。To run a macro or event procedure when this event occurs, set the OnExit property to the name of the macro or to [Event Procedure].

因為 Enter 事件發生於焦點移至的特定控制項之前,您可以使用 Enter 巨集或事件程序顯示指示;例如,您可以使用巨集或事件程序來顯示小型表單或訊息] 方塊中識別的控制項通常包含、 資料類型或提供如何使用控制項的指示。Because the Enter event occurs before the focus moves to a particular control, you can use an Enter macro or event procedure to display instructions; for example, you could use a macro or event procedure to display a small form or message box identifying the type of data the control typically contains, or giving instructions on how to use the control.

Exit 事件會在 LostFocus 事件之前發生。The Exit event occurs before the LostFocus event.

不同於 LostFocus 事件表單失去焦點時都不會發生 Exit 事件。Unlike the LostFocus event, the Exit event does not occur when a form loses the focus. 例如,假設您選取 [表單] 核取方塊,然後按一下 [報表。For example, suppose you select a check box on a form, and then click a report. 輸入GotFocus 事件發生時選取此核取方塊。The Enter and GotFocus events occur when you select the check box. 當您按一下 [報表會發生 LostFocus 事件。Only the LostFocus event occurs when you click the report. (因為將焦點移至不同的視窗) 不會發生 Exit 事件。The Exit event doesn't occur (because the focus is moving to a different window). 如果您選取一次以將其移至最上層的表單上核取方塊,會發生 GotFocus 事件,但不是 Enter 事件 (因為控制項有焦點時表單上次使用時間)。If you select the check box on the form again to bring it to the foreground, the GotFocus event occurs, but not the Enter event (because the control had the focus when the form was last active). 只有當您按一下表單上的另一個控制項時,會發生 Exit 事件。The Exit event occurs only when you click another control on the form.

如果您將焦點移至表單上控制項和控制項沒有焦點 離開 該表單上及表單上沒有焦點的控制項 LostFocus 事件之前發生 EnterGotFocus 事件控制項您移動至。If you move the focus to a control on a form, and that control doesn't have the focus on that form, the Exit and LostFocus events for the control that does have the focus on the form occur before the Enter and GotFocus events for the control you moved to.

如果您用滑鼠將焦點從主表單中的控制項移到該表單之子表單中的控制項 (子表單上的該控制項尚未取得焦點),則會發生下列事件:If you use the mouse to move the focus from a control on a main form to a control on a subform of that form (a control that doesn't already have the focus on the subform), the following events occur:

  • 結束 (如主表單上的控制項)Exit (for the control on the main form)
  • LostFocus (如主表單上的控制項)LostFocus (for the control on the main form)
  • 輸入 (適用於子表單控制項)Enter (for the subform control)
  • 結束 (適用於子表單中具有焦點的控制項)Exit (for the control on the subform that had the focus)
  • LostFocus (適用於子表單中具有焦點的控制項)LostFocus (for the control on the subform that had the focus)
  • 輸入 (適用於子表單中焦點的控制項)Enter (for the control on the subform that the focus moved to)
  • GotFocus (適用於子表單中焦點的控制項)GotFocus (for the control on the subform that the focus moved to)

如果您將移至子表單控制項先前具有焦點,其Enter事件和其GotFocus事件都不會發生,但在子表單控制項的Enter事件沒有發生。If the control that you move to on the subform previously had the focus, neither its Enter event nor its GotFocus event occurs, but the Enter event for the subform control does occur. 如果您子表單上控制項的焦點移至主表單上的控制項, 退出 和子表單控制項的 LostFocus 事件不會發生, Exit 事件子表單控制項和主表單上控制項的 輸入GotFocus 事件。If you move the focus from a control on a subform to a control on the main form, the Exit and LostFocus events for the control on the subform don't occur, just the Exit event for the subform control and the Enter and GotFocus events for the control on the main form.

注意

您通常會使用滑鼠或 tab 等按鍵將焦點移到另一個控制項。You often use the mouse or a key such as Tab to move the focus to another control. 此時除了本主題所討論的事件之外,還會引發滑鼠或鍵盤事件。This causes mouse or keyboard events to occur in addition to the events discussed in this topic.

範例Example

在下列範例中,兩個事件程序會附加至 [ LastName ] 文字方塊中。In the following example, two event procedures are attached to the LastName text box. Enter 事件程序會顯示指定的資料類型使用者可以在 [文字] 方塊中輸入訊息。The Enter event procedure displays a message specifying what type of data the user can enter in the text box. Exit 事件程序會顯示對話方塊要求使用者如果應儲存變更之前將焦點移動至另一個控制項。The Exit event procedure displays a dialog box asking the user if changes should be saved before the focus moves to another control. 如果使用者按一下 [取消] 按鈕, _Cancel_引數設為True (1),這將焦點移至 [] 文字方塊中,而不儲存變更。If the user clicks the Cancel button, the Cancel argument is set to True (1), which moves the focus to the text box without saving changes. 如果使用者選擇 [確定] 按鈕,儲存所做的變更,並將焦點移到另一個控制項。If the user chooses the OK button, the changes are saved, and the focus moves to another control.

若要嘗試此範例,請將下列事件程序新增至包含名為LastName] 文字方塊中的表單。To try the example, add the following event procedure to a form that contains a text box named LastName.

Private Sub LastName_Enter() 
 MsgBox "Enter your last name." 
End Sub 
 
Private Sub LastName_Exit(Cancel As Integer) 
 Dim strMsg As String 
 
 strMsg = "You entered '" & Me!LastName _ 
 & "' as your last name." & _ 
 vbCrLf & "Is this correct?" 
 If MsgBox(strMsg, vbYesNo) = vbNo Then 
 Cancel = True ' Cancel exit. 
 Else 
 Exit Sub ' Save changes and exit. 
 End If 
End Sub

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.