メイン コンテンツへスキップ
サポート
Microsoft アカウントでサインイン
サインインまたはアカウントを作成してください。
こんにちは、
別のアカウントを選択してください。
複数のアカウントがあります
サインインに使用するアカウントを選択してください。

注: 最新のヘルプ コンテンツをできるだけ早く、お客様がお使いの言語で提供したいと考えております。このページは、自動翻訳によって翻訳されているため、文章校正のエラーや不正確な情報が含まれている可能性があります。私たちの目的は、このコンテンツがお客様の役に立つようにすることです。お客様にとって役立つ情報であったかどうかを、このページの下部でお知らせください。簡単に参照できるように、こちらに英語の記事があります。

イベント プロシージャに OnError Access フォームのプロパティを設定した場合にその手順については、ODBC エラーの説明を取得することはできずも、特定の ODBC エラーをトラップことはできません。ODBC エラーが発生した場合、エラーのイベント プロシージャに渡される情報は、エラー メッセージに対応するように一般的なエラー、3146 などの数を: ODBC 呼び出しが失敗します。

原因

ODBC エラー メッセージは通常、2 つのコンポーネントで構成されます。最初のコンポーネントは、エラー 3146、説明します。

ODBC 呼び出しが失敗しました。

エラー番号となど、説明を取得できる、2 番目のコンポーネントで、サーバーに固有のエラー情報が含まれています。

[Microsoft][ODBC SQL Server ドライバー][SQL Server] < サーバー固有のエラー メッセージ > (#< エラー番号 >)

イベント プロシージャにフォームのOnErrorプロパティを設定した場合、エラーの最初のコンポーネントの数をトラップすることができますが、2 番目のコンポーネントの数をトラップすることはできません。ODBC エラーの 2 番目の部分でサーバーに固有の情報が表示されます画面に完了した後、コードを実行して指定しない限り、次の行で [イベント プロシージャ。

応答 = acDataErrContinue

解決策

注: Microsoft は、黙示保証するだけでプログラミングの例を示します。これが含まれているが、商品や特定の目的の責任に限定されます。この記事では、例示されているプログラミング言語とを作成して、プロシージャをデバッグするために使用するツールに精通していることを前提としています。Microsoft サポート エンジニアが、特定のプロシージャの機能を説明することができますが、機能の追加や、特定の要件を満たしているプロシージャを作成する次の例は変更されません。

アプリケーション手順については、フォームに基づくRecordsetCloneを更新するデータ アクセス オブジェクト (DAO) を使用している Microsoft Visual Basic を作成することができます。これにより、表示されるエラー メッセージをトラップすることができます。

DAO には、ODBC エラーの 2 番目の部分でサーバーに固有の情報をトラップするために使用できるエラーのコレクションにはが含まれています。ODBC エラーが発生して、エラーコレクションの最初の要素の最初のコンポーネントが保存されている 2 番目の要素の 2 番目のコンポーネントが保存されています。

この記事の例では、ODBC の特定のエラーをトラップするのにエラーイベントではなくただしイベントを使用します。フォームのただしイベントが発生したときに、特定の ODBC エラーをトラップする関数を作成するには、次の手順を実行します。

  1. 空のデスクトップ データベースを作成します。

  2. Microsoft SQL Server で AdventureWorks サンプル データベースの dbo_Accounts テーブルへのリンクです。

  3. 単票形式のレイアウト、[アカウント] テーブルに基づく新しいフォームを作成するのには、フォーム ウィザードを使用します。

  4. フォームを frmAccounts として保存します。

  5. 新しいモジュールを作成し、その行がない場合に宣言] セクションで、次の行を入力します。

    Option Explicit

  6. 入力するか、モジュール、次の手順に貼り付けます。

    Public Function SaveRecODBC(SRO_form As Form) As Boolean
    ' ***************************************************************
    ' Function: SaveRecODBC
    ' Purpose: Updates a form based on a linked ODBC table
    ' and traps any ODBC errors.
    ' Arguments: SRO_Form, which refers to the form.
    ' Returns: True if successful or False if an error occurs.
    ' ***************************************************************
    On Error GoTo SaveRecODBCErr
    Dim fld As Field, ctl As Control
    Dim errStored As Error
    Dim rc As DAO.Recordset
    
    ' Check to see if the record has changed.
    If SRO_form.Dirty Then
        Set rc = SRO_form.Recordset.Clone
        If SRO_form.NewRecord Then
            rc.AddNew
            For Each ctl In SRO_form.Controls
                ' Check to see if it is the type of control
                ' that has a ControlSource.
                If ctl.ControlType = acTextBox Or _
                    ctl.ControlType = acComboBox Or _
                    ctl.ControlType = acListBox Or _
                    ctl.ControlType = acCheckBox Then
                    ' Verify that a value exists in the ControlSource.
                    If ctl.Properties("ControlSource") <> "" Then
                        ' Loop through the fields collection in the
                        ' RecordsetClone. If you find a field name
                        ' that matches the ControlSource, update the
                        ' field. If not, skip the field. This is
                        ' necessary to account for calculated controls.
                        For Each fld In rc.Fields
                            ' Find the field and verify
                            ' that it is not Null.
                            ' If it is Null, don't add it.
                            If fld.Name = ctl.Properties("ControlSource") _
                            And Not IsNull(ctl) Then
                                fld.Value = ctl
                                ' Exit the For loop
                                ' if you have a match.
                                Exit For
                            End If
                        Next fld
                    End If ' End If ctl.Properties("ControlSource")
                End If ' End If ctl.controltype
            Next ctl
            rc.Update
        Else
            ' This is not a new record.
            ' Set the bookmark to synchronize the record in the
            ' RecordsetClone with the record in the form.
            rc.Bookmark = SRO_form.Bookmark
            rc.Edit
            For Each ctl In SRO_form.Controls
                ' Check to see if it is the type of control
                ' that has a ControlSource.
                If ctl.ControlType = acTextBox Or _
                    ctl.ControlType = acComboBox Or _
                    ctl.ControlType = acListBox Or _
                    ctl.ControlType = acCheckBox Then
                    ' Verify that a value exists in the
                    ' ControlSource.
                    If ctl.Properties("ControlSource") <> "" Then
                        ' Loop through the fields collection in the
                        ' RecordsetClone. If you find a field name
                        ' that matches the ControlSource, update the
                        ' field. If not, skip the field. This is
                        ' necessary to account for calcualted controls.
                        For Each fld In rc.Fields
                            ' Find the field and make sure that the
                            ' value has changed. If it has not
                            ' changed, do not perform the update.
                            If fld.Name = ctl.Properties("ControlSource") _
                                And fld.Value <> ctl And _
                                Not IsNull(fld.Value <> ctl) Then
                                fld.Value = ctl
                                ' Exit the For loop if you have a match.
                                Exit For
                            End If
                        Next fld
                    End If ' End If ctl.Properties("ControlSource")
                End If ' End If ctl.controltype
            Next ctl
            rc.Update
        End If ' End If SRO_form.NewRecord
    End If ' End If SRO_form.Dirty
    ' If function has executed successfully to this point then
    ' set its value to True and exit.
    SaveRecODBC = True
    
    Exit_SaveRecODBCErr:
        Exit Function
    
    SaveRecODBCErr:
    ' The function failed because of an ODBC error.
    ' Below are a list of some of the known error numbers.
    ' If you are not receiving an error in this list,
    ' add that error to the Select Case statement.
    For Each errStored In DBEngine.Errors
        Select Case errStored.Number
            Case 3146 ' No action -- standard ODBC--Call failed error.
            Case 2627 ' Error caused by duplicate value in primary key.
                MsgBox "You tried to enter a duplicate value in the Primary Key."
            Case 3621 ' No action -- standard ODBC command aborted error.
            Case 547 ' Foreign key constraint error.
                MsgBox "You violated a foreign key constraint."
            Case Else ' An error not accounted for in the Select Case ' statement.
                On Error GoTo 0
                Resume
        End Select
    Next errStored
    SaveRecODBC = False
    Resume Exit_SaveRecODBCErr
    
    End Function
    
  7. 一意の名前を付けてモジュールを保存して、モジュール ウィンドウを閉じます。

  8. 次のイベント プロシージャをただしfrmAccounts フォームのプロパティを設定します。

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    ' If you can save the changes to the record undo the changes on the form.
    If SaveRecODBC(Me) Then Me.Undo
    ' If this is a new record go to the last record on the form.
    If Me.NewRecord Then
        RunCommand acCmdRecordsGoToLast
    Else
        ' If you can't update the record, cancel the BeforeUpdate event.
        Cancel = -1
    End If
    End Sub
    
  9. [デバッグ] メニューの [ < データベース名 > のコンパイル] をクリックします

  10. エラーが発生しない場合は、フォームを保存します。

  11. FrmAccounts フォームを開いて新しいレコードを追加するか、レコードを編集します。

    レコードを変更すると、別のレコードに移動するときにレコードを保存します。サーバーに固有のエラーを基にしたユーザー設定のメッセージが表示 ODBC エラーが発生する場合や、一般的な"ODBC--呼び出しに失敗しました"メッセージをトラップします。

ヘルプを表示

その他のオプションが必要ですか?

サブスクリプションの特典の参照、トレーニング コースの閲覧、デバイスのセキュリティ保護方法などについて説明します。

コミュニティは、質問をしたり質問の答えを得たり、フィードバックを提供したり、豊富な知識を持つ専門家の意見を聞いたりするのに役立ちます。

この情報は役に立ちましたか?

言語の品質にどの程度満足していますか?
どのような要因がお客様の操作性に影響しましたか?
[送信] を押すと、Microsoft の製品とサービスの改善にフィードバックが使用されます。 IT 管理者はこのデータを収集できます。 プライバシーに関する声明。

フィードバックをいただき、ありがとうございます。

×