Masuk dengan Microsoft
Masuk atau buat akun.
Halo,
Pilih akun lain.
Anda memiliki beberapa akun
Pilih akun yang ingin Anda gunakan untuk masuk.

Catatan: Kami ingin secepatnya menyediakan konten bantuan terbaru dalam bahasa Anda. Halaman ini diterjemahkan menggunakan mesin dan mungkin terdapat kesalahan tata bahasa atau masalah keakuratan. Kami bertujuan menyediakan konten yang bermanfaat untuk Anda. Dapatkah Anda memberi tahu kami apakah informasi ini bermanfaat untuk Anda di bagian bawah halaman ini? Berikut artikel dalam bahasa Inggris untuk referensi.

Jika Anda mengatur properti OnError formulir Access untuk prosedur kejadian, Anda tidak bisa mengambil Deskripsi kesalahan ODBC dalam prosedur tersebut, dan Anda tidak bisa menemukan kesalahan ODBC spesifik. Saat terjadi kesalahan ODBC, satu-satunya informasi yang diberikan kepada prosedur kejadian kesalahan adalah jumlah kesalahan umum, seperti 3146, yang berhubungan dengan pesan kesalahan: ODBC-panggilan gagal.

Penyebab

Pesan kesalahan ODBC biasanya terdiri dari dua komponen. Komponen pertama adalah kesalahan 3146, deskripsi yang adalah:

Gagal panggilan-ODBC

Informasi kesalahan server khusus yang dimuat dalam komponen kedua, dari mana Anda bisa mengambil nomor kesalahan dan deskripsi seperti:

[Microsoft] [ODBC SQL Server Driver] [SQL Server] < pesan kesalahan spesifik Server > (#< nomor kesalahan >)

Jika Anda menyetel properti OnError formulir ke prosedur kejadian, Anda bisa menemukan jumlah komponen pertama kesalahan, tapi Anda tidak bisa menemukan jumlah komponen kedua. Informasi server khusus dalam bagian kedua kesalahan ODBC muncul pada layar setelah kode yang telah selesai berjalan, kecuali jika Anda menyertakan garis berikut ini dalam acara prosedur:

Respons = acDataErrContinue

Resolusi

Catatan: Microsoft menyediakan contoh pemrograman ilustrasi saja, tanpa jaminan dinyatakan atau tersirat. Ini menyertakan, tetapi tidak terbatas pada, garansi atau kesesuaian untuk tujuan tertentu. Artikel ini mengasumsikan bahwa Anda sudah familiar dengan bahasa pemrograman yang muncul dan dengan alat yang digunakan untuk membuat dan debug prosedur. Insinyur dukungan Microsoft bisa membantu menjelaskan fungsionalitas prosedur tertentu, tapi mereka tidak akan memodifikasi contoh ini untuk menyediakan fungsionalitas ditambahkan atau membangun prosedur untuk memenuhi kebutuhan spesifik Anda.

Anda bisa membuat Microsoft Visual Basic for Applications prosedur yang menggunakan Data Access Objects (DAO) untuk memperbarui RecordsetClone yang didasarkan pada formulir. Ini memungkinkan Anda untuk menemukan pesan kesalahan apa pun yang Anda terima.

DAO berisi kumpulan kesalahan yang bisa Anda gunakan untuk menemukan informasi server khusus dalam bagian kedua kesalahan ODBC. Saat terjadi kesalahan ODBC, komponen pertama disimpan dalam elemen pertama dari kumpulan kesalahan , dan komponen kedua disimpan dalam elemen kedua.

Contoh dalam artikel ini menggunakan acara BeforeUpdate alih-alih kesalahan acara untuk menemukan kesalahan ODBC tertentu. Untuk membuat fungsi yang jebakan kesalahan ODBC tertentu saat terjadi kejadian BeforeUpdate formulir, ikuti langkah-langkah ini:

  1. Membuat database desktop kosong.

  2. Link ke dbo_Accounts tabel di database sampel AdventureWorks di Microsoft SQL Server.

  3. Gunakan panduan formulir - kolumnar tata letak untuk membuat formulir baru berdasarkan akun tabel.

  4. Simpan formulir sebagai frmAccounts.

  5. Membuat modul baru, dan ketik garis berikut ini di bagian Deklarasi jika baris tersebut belum ada:

    Opsi eksplisit

  6. Ketik atau tempelkan prosedur berikut ini ke modul:

    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. Simpan modul dengan nama yang unik dan menutup jendela modul.

  8. Atur properti BeforeUpdate frmAccounts formulir ke prosedur kejadian berikut ini:

    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. Pada Debug menu, klik menyusun < nama database Anda >

  10. Jika tidak ada kesalahan terjadi, Simpan formulir.

  11. Buka formulir frmAccounts, lalu menambahkan rekaman baru atau mengedit rekaman.

    Ketika Anda membuat perubahan pada catatan, catatan yang disimpan saat Anda memindahkan ke catatan lain. Jika terjadi kesalahan ODBC, Anda melihat pesan kustom yang didasarkan pada server khusus kesalahan, dan generik "ODBC--panggilan gagal" pesan yang terjebak.

Perlu bantuan lainnya?

Ingin opsi lainnya?

Jelajahi manfaat langganan, telusuri kursus pelatihan, pelajari cara mengamankan perangkat Anda, dan banyak lagi.

Komunitas membantu Anda bertanya dan menjawab pertanyaan, memberikan umpan balik, dan mendengar dari para ahli yang memiliki pengetahuan yang luas.

Apakah informasi ini berguna?

Seberapa puaskah Anda dengan kualitas bahasanya?
Apa yang memengaruhi pengalaman Anda?
Dengan menekan kirim, umpan balik Anda akan digunakan untuk meningkatkan produk dan layanan Microsoft. Admin TI Anda akan dapat mengumpulkan data ini. Pernyataan Privasi.

Terima kasih atas umpan balik Anda!

×