次の方法で共有


Before Change マクロ イベント

適用先: Access 2013、Office 2013

The Before Change event occurs when a record changes, but before the change is committed.

注:

Before Change イベントは、データ マクロでのみ使用できます。

注釈

Use the Before Change event to perform any actions that you want to occur before a record is changed. The Before Change is commonly used to perform validation and to raise custom error messages.

Updated("フィールド名") 関数を使用すると、フィールドが変更されているかどうかを判断できます。 次のコード例は、 If ステートメントを使用して、PaidInFull フィールドが変更されているかどうかを判断する方法を示しています。

    If  Updated("PaidInFull")   Then 
     
        /* Perform actions based on changes to the field.   */ 
     
    End If 

Use the IsInsert property to determine whether the Before Change event was triggered by a new record being created or a change to an existing record. They IsInsert property contains True if the event was triggered by a new record, False if the event was triggered by a change to en existing record.

The following code example shows the syntax for using the IsInsert property.

    If   [IsInsert] = True   Then 
     
       /*  Actions for validating a new record go here.       */ 
     
    Else 
     
       /* Actions for processing a changed record go here.    */ 
     
    End If

次の構文を使用して、フィールド内の以前の値にアクセスできます。

    [Old].[Field Name]

たとえば、QuantityInStock フィールド内の以前の値にアクセスするには、次の構文を使用します。

    [Old].[QuantityInStock]

The previous values are deleted permanently when the Before Change event ends.

You can cancel the Before Change event by using the RaiseError action. エラーが発生すると、Before Change イベント内の変更は破棄されます。

Before Change イベントで使用できるマクロ コマンドは次のとおりです。

コマンドの種類

コマンド

プログラム フロー

Comment マクロ ステートメント

プログラム フロー

Group マクロ ステートメント

プログラム フロー

If...Then...Else マクロ ブロック

データ ブロック

LookupRecord マクロ アクション

データ アクション

ClearMacroError マクロ アクション

データ アクション

OnError マクロ アクション

データ アクション

RaiseError マクロ アクション

データ アクション

SetField マクロ アクション

データ アクション

SetLocalVar マクロ アクション

データ アクション

StopMacro マクロ アクション

To create a Data Macro that captures the Before Change event, use the following steps:

  1. Open the table for which you want to capture the Before Change event.

  2. [ テーブル] タブの [ イベント前] で、[ 変更前] をクリックします。

空のデータ マクロがマクロ デザイナーに表示されます。

次のコード例では 、[変更前 ] イベントを使用して [状態] フィールドを検証します。 An error is raised if an inappropriate value is contained in the Resolution field.

 
/* Check to ensure that if the bug is resloved that the user has selected a resolution      */ 
If   [Status]="3 - Resolved" And IsNull([Resolution])   Then 
 
     RaiseError 
             Error Number   1 
        Error Description   You must select a resolution. 
End If 
 
/* Check to ensure that if a bug is closed that the user has selected a resolution first     */ 
If   [Status]="4 - Closed" And IsNull([Resolution])   Then 
 
      RaiseError 
             Error Number   2 
        Error Description   An issue must be resolved before it can be closed. 
End If 
 
If   [Status]<>"3 - Resolved" And [Status]<>"4 - Closed"   Then 
 
      SetField 
             Name   Resolution 
            Value   =Null 
End If 
 

この例をマクロ デザイナーで表示するには、次の手順に従います。

  1. Open the table for which you want to capture the Before Change event.

  2. [ テーブル] タブの [ イベント前] で、[ 変更前] をクリックします。

  3. 次のコード例のコードを選択し、 Ctrl キーを押しながら C キー を押してクリップボードにコピーします。

  4. マクロ デザイナー ウィンドウをアクティブにし、 Ctrl キーを押しながら V キーを押します。

<DataMacros xmlns="http://schemas.microsoft.com/office/accessservices/2009/04/application"> 
  <DataMacro Event="BeforeChange"> 
    <Statements> 
      <Comment>Check to ensure that if the bug is resloved that the user has selected a resolution </Comment> 
      <ConditionalBlock> 
        <If> 
          <Condition>[Status]="3 - Resolved" And IsNull([Resolution])</Condition> 
          <Statements> 
            <Action Name="RaiseError"> 
              <Argument Name="Number">1</Argument> 
              <Argument Name="Description">You must select a resolution.</Argument> 
            </Action> 
          </Statements> 
        </If> 
      </ConditionalBlock> 
      <Comment>Check to ensure that if a bug is closed that the user has selected a resolution first </Comment> 
      <ConditionalBlock> 
        <If> 
          <Condition>[Status]="4 - Closed" And IsNull([Resolution])</Condition> 
          <Statements> 
            <Action Name="RaiseError"> 
              <Argument Name="Number">2</Argument> 
              <Argument Name="Description">An issue must be resolved before it can be closed.</Argument> 
            </Action> 
          </Statements> 
        </If> 
      </ConditionalBlock> 
      <ConditionalBlock> 
        <If> 
          <Condition>[Status]&lt;&gt;"3 - Resolved" And [Status]&lt;&gt;"4 - Closed"</Condition> 
          <Statements> 
            <Action Name="SetField"> 
              <Argument Name="Field">Resolution</Argument> 
              <Argument Name="Value">Null</Argument> 
            </Action> 
          </Statements> 
        </If> 
      </ConditionalBlock> 
    </Statements> 
  </DataMacro> 
</DataMacros>

次の例は、" RaiseError" アクションを使用して Before Change データ マクロ イベントを取り消す方法を示します。 AssignedTo フィールドが更新されると、 LookupRecord データ ブロックを使用して、割り当てられた技術者が未解決サービス リクエストに現在割り当てられているかどうかが確認されます。 これが true の場合、変更前イベントは取り消され、レコードは更新されません。

サンプル コードの提供元:Microsoft Access 2010 Programmer's Reference

    /* Get the name of the technician  */
    Look Up A Record In tblTechnicians
        Where Condition =[tblTechnicians].[ID]=[tblServiceRequests].[AssignedTo]
    SetLocalVar
        Name TechName
        Expression [tblTechnicians].[FirstName] & " " & [tblTechnicians].[LastName]
    /* End LookUpRecord  */
    
    If Updated("AssignedTo") Then
        Look Up A Record In tblServiceRequests
            Where Condition SR.[AssignedTo]=tblServiceRequests[AssignedTo] And 
                SR.[ID]<>tblServiceRequests.[ID] And IsNull(SR.[ActualCompletionDate])
            Alias SR
            RaiseError
                Error Number 1234
                Error Description ="Cannot assign a request to the specified technician: " & [TechName]
    
    End If