次の方法で共有


CancelEvent マクロ アクション

適用先: Access 2013、Office 2013

" CancelEvent/イベントのキャンセル " アクションを使用して、このアクションが定義されたマクロが Access で実行される原因となったイベントを取り消すことができます。 " BeforeUpdate/更新前処理 "、" OnOpen/開く時 "、" OnUnload/読み込み解除時 "、" OnPrint/印刷時 " などのイベント プロパティに、このマクロ名を設定します。

設定

"CancelEvent/イベントのキャンセル" アクションには、引数はありません。

注釈

In a form, you typically use the CancelEvent action in a validation macro with the BeforeUpdate event property. When a user enters data in a control or record, Access runs the macro before adding the data to the database. If the data fails the validation conditions in the macro, the CancelEvent action cancels the update process before it starts.

Often, you use this action with the MessageBox action to indicate that the data has failed the validation conditions and to provide helpful information about the kind of data that should be entered.

The following events can be canceled by the CancelEvent action.

ApplyFilter

Dirty

MouseDown

BeforeDelConfirm

Exit

NoData

BeforeInsert

Filter

Open

BeforeUpdate

Format

Print

DblClick

KeyPress

Unload

Delete

注:

You can use the CancelEvent action with the MouseDown event only to cancel the event that occurs when you right-click an object.

If a control's OnDblClick event property setting specifies a macro containing the CancelEvent action, the action cancels the DblClick event.

取り消し可能なイベントでは、イベントのマクロが実行された後に、イベントの既定の動作 (イベント発生時の通常の動作) が実行されます。 したがって、既定の動作を取り消すことができます。 たとえば、テキスト ボックスでカーソル位置にある単語をダブルクリックすると、通常は、その単語が選択されます。 DblClick イベントのマクロで、この既定の動作を取り消し、テキスト ボックスのデータに関する情報を表示するフォームを開くなどの別のアクションを実行することができます。 取り消し不可能なイベントでは、イベントのマクロが実行される前に、既定の動作が実行されます。

注:

If a form's OnUnload event property specifies a macro that carries out a CancelEvent action, you won't be able to close the form. You must either correct the condition that caused the CancelEvent action to be carried out or open the macro and delete the CancelEvent action. If the form is a modal form, you won't be able to open the macro.

To carry out the CancelEvent action in a Visual Basic for Applications (VBA) module, use the CancelEvent method of the DoCmd object.

マクロによるデータの入力検査

次の入力検査マクロでは、"仕入先" フォームで入力された郵便番号を確認します。 このマクロでは、" StopMacro/マクロの中止 "、" MessageBox/メッセージボックス "、" CancelEvent/イベントのキャンセル "、および " GoToControl/コントロールの移動 " の各アクションの使い方を示します。 条件式では、フォームのレコードに入力された都道府県と郵便番号を確認します。 都道府県に対して郵便番号が正しく入力されていない場合、マクロはメッセージ ボックスを表示して、レコードの保存を取り消します。 It then returns you to the Postal Code control, where you can correct the error. このマクロは "仕入先" フォームの "BeforeUpdate/更新前処理" プロパティに設定します。

条件

アクション

引数: 設定値

コメント

IsNull([CountryRegion])

StopMacro

If CountryRegion is Null, postal code can't be validated.

[都道府県] In ("東京都","埼玉県","千葉県") And Len([郵便番号]) <> 7

MessageBox

"Message/メッセージ": 郵便番号は 7 文字である必要があります。 "Beep/警告音": はい Type/メッセージの種類: 情報 Title/メッセージ タイトル: 郵便番号エラー

郵便番号が 7 文字でない場合にメッセージを表示します。

...

CancelEvent

イベントを取り消します。

GoToControl

"Control Name/コントロール名": 郵便番号

[都道府県] In ("山梨県","長野県") And Len([郵便番号]) <> 7

MessageBox

"Message/メッセージ": 郵便番号は 7 文字である必要があります。 "Beep/警告音": はい Type/メッセージの種類: 情報 Title/メッセージ タイトル: 郵便番号エラー

郵便番号が 7 文字でない場合にメッセージを表示します。

...

CancelEvent

イベントを取り消します。

GoToControl

"Control Name/コントロール名": 郵便番号

([都道府県] = "広島県") And ([郵便番号] Not Like "7[2-3][0-9][0-9][0-9][0-9][0-9]")

MessageBox

メッセージ: 郵便番号が無効です。 カナダのコードの例: H1J 1C3 ビープ音: はい 型: 情報 タイトル: 郵便番号エラー

郵便番号がカナダに適していない場合は、メッセージを表示します。 (カナダのコードの例: H1J 1C3)

...

CancelEvent

イベントを取り消します。