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

Dirty事件發生於指定控制項的內容變更時。The Dirty event occurs when the contents of the specified control changes.

語法Syntax

運算式dirty取消expression.Dirty (Cancel)

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

參數Parameters

名稱Name 必要/選用Required/Optional 資料類型Data type 描述Description
CancelCancel 必要Required IntegerInteger 設定決定 Dirty 事件發生於。The setting determines if the Dirty event occurs. _Cancel_引數設定為True (1) 會取消Dirty事件。Setting the Cancel argument to True (1) cancels the Dirty event.

註解Remarks

此事件的範例包括直接在文字方塊或下拉式方塊中輸入字元,或來使用巨集或 Visual Basic 變更控制項的 Text 屬性設定。Examples of this event include entering a character directly in the text box or combo box or changing the control's Text property setting by using a macro or Visual Basic.

使用巨集或 Visual Basic 中修改表單內的記錄不會觸發此事件。Modifying a record within a form by using a macro or Visual Basic doesn't trigger this event. 您必須將資料直接輸入記錄或設定控制項的 Text 屬性。You must type the data directly into the record or set the control's Text property.

此事件僅適用於結合表單,並不適用於未結合表單或報表。This event applies only to bound forms, not an unbound form or report.

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

Dirty事件發生時執行巨集或事件程序,您可以判斷是否能變更記錄。By running a macro or event procedure when a Dirty event occurs, you can determine if the record can be changed. 您也可以顯示一則訊息,並要求編輯 」 權限。You can also display a message and ask for edit permission.

使用鍵盤變更記錄中的資料會引發鍵盤事件發生類似Dirty事件的控制項事件。Changing the data in a record by using the keyboard causes keyboard events to occur in addition to control events like the Dirty event. 例如,如果您移到一個新的記錄,並在記錄的文字方塊中輸入一個 ANSI 字元,就會依次發生下列事件:For example, if you move to a new record and type an ANSI character in a text box in the record, the following events occur in this order:

KeyDownKeyPressBeforeInsert變更KeyUpKeyDownKeyPressBeforeInsertDirtyKeyUp

一筆記錄的BeforeUpdateAfterUpdate事件發生在記錄中輸入新的或變更資料並移至另一筆記錄 (或 [記錄] 功能表上選擇 [儲存記錄),因此Dirty之後記錄的事件。The BeforeUpdate and AfterUpdate events for a record occur after you enter the new or changed data in the record and move to another record (or choose Save Record on the Records menu), and therefore after the Dirty event for the record.

取消Dirty事件將會使會復原目前記錄的變更。Canceling the Dirty event will cause the changes to the current record to be rolled back. 它是相當於按下 Esc 鍵。It is equivalent to pressing the Esc key.

範例Example

下列範例會啟用 [ btnUndo ] 按鈕時變更資料。The following example enables the btnUndo button when data is changed. UndoEdits () 副程式會呼叫文字方塊控制項的Dirty事件。The UndoEdits( ) subroutine is called from the Dirty event of text box controls. 按一下 [已啟用的btnUndo ] 按鈕,則會使用OldValue屬性還原控制項的原始值。Clicking the enabled btnUndo button restores the original value of the control by using the OldValue property.

Private Sub Form_Dirty() 
 If Me.Dirty Then 
 Me!btnUndo.Enabled = True ' Enable button. 
 Else 
 Me!btnUndo.Enabled = False ' Disable button. 
 End If 
End Sub 
 
Sub btnUndo_Click() 
 Dim ctlC As Control 
 ' For each control. 
 For Each ctlC in Me.Controls 
 If ctlC.ControlType = acTextBox Then 
 ' Restore Old Value. 
 ctlC.Value = ctlC.OldValue 
 End If 
 Next ctlC 
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.