Prisijunkite prie „Microsoft“
Prisijunkite arba sukurkite paskyrą.
Sveiki,
Pasirinkti kitą paskyrą.
Turite kelias paskyras
Pasirinkite paskyrą, kurią naudodami norite prisijungti.

Pastaba: Norėtume jums kuo greičiau pateikti naujausią žinyno turinį jūsų kalba. Šis puslapis išverstas automatiškai, todėl gali būti gramatikos klaidų ar netikslumų. Mūsų tikslas – padaryti, kad šis turinys būtų jums naudingas. Gal galite šio puslapio apačioje mums pranešti, ar informacija buvo naudinga? Čia yra straipsnis anglų kalba, kuriuo galite pasinaudoti kaip patogia nuoroda.

Jei nustatysite ypatybę Esant_klaidai prieigos formos įvykio procedūrą, nuskaitote ODBC klaidos procedūros aprašymas, ir taip pat negalima esančioms konkrečius ODBC klaida. Kai ODBC klaida įvyksta, tik informacija, perduodama klaidos įvykio procedūra yra skaičius bendro pobūdžio klaida, pvz., 3146, atitinkantį klaidos pranešimas: nepavyko ODBC skambutis.

Priežastis

ODBC klaidų pranešimai paprastai sudaro dvi dalys. Pirmasis komponentas yra klaida 3146, kurių aprašas yra:

ODBC – nepavyko

Serverio konkrečių klaidų informaciją, pateiktą antrojo komponento, iš kurių galite gauti klaidos kodas ir aprašą, tokių kaip:

(Microsoft) [ODBC SQL serverio tvarkyklė] [SQL serverio] < serverio konkrečių klaidos pranešimas > (< klaidos numerį > #)

Jei nustatysite ypatybę Esant_klaidai formos įvykio procedūrą, gali perdengti pirmojo komponento klaidos skaičių, tačiau negalite esančioms antrojo komponento skaičių. Serverio susijusi informacija antrojoje dalyje ODBC klaida ekrane rodomą meniu Baigę kodą vykdymo, išskyrus atvejus, kai įtraukiate šią eilutę įvykių procedūrą:

Atsakymas = acDataErrContinue

Sprendimas

Pastaba: "Microsoft" teikia programavimo pavyzdžiai tik, be jokių garantijų ar numanomų. Tai yra, bet neapsiribojant, tinkamumo konkrečiam tikslui garantijas. Šiame straipsnyje daroma prielaida, kad esate susipažinę su programavimo kalba, kuri yra buvo parodytas ir įrankius, kurie naudojami kurti ir derinti procedūras. "Microsoft" palaikymo inžinierių gali padėti paaiškindami tam tikros procedūros, tačiau jie bus ne modifikuoti šių pavyzdžių, kad numatytų papildomą funkcinę galimybę arba sukurtų procedūras savo specifinius reikalavimus.

Galite sukurti Microsoft Visual Basic for Applications procedūra, kuri naudoja duomenų prieigos objektai (DAO) atnaujinti įrašų pagal formą. Tai suteikia galimybę perdengti bet gaunate klaidos pranešimą.

DAO yra klaidų rinkinio, kuriuos galite naudoti esančioms serverio susijusi informacija antrojoje dalyje ODBC klaida. Kai ODBC klaida įvyksta, pirmame saugoma pirmojo elemento klaidų rinkinio ir antrojo komponento saugoma antrą elementą.

Į šiame straipsnyje pavyzdyje įvykis BeforeUpdate paleidžia vietoj klaidos įvykis esančioms konkrečias ODBC klaidas. Norėdami sukurti funkciją, kuri sulaiko konkrečias ODBC klaidas, kai įvykis BeforeUpdate paleidžia formos, atlikite šiuos veiksmus:

  1. Tuščia kompiuterio duomenų bazės kūrimas.

  2. Saitas su "Microsoft SQL Server" duomenų bazės AdventureWorks dbo_Accounts lentelę.

  3. Naudokite formų vediklį - stulpelio maketai, kuriuos galite kurti naują formą pagal sąskaitų lentelę.

  4. Įrašykite formą kaip frmAccounts.

  5. Sukurkite naują modulį, ir tada įveskite šią eilutę sekcijoje aprašai, jei tos linijos dar nėra:

    Option Explicit

  6. Įveskite arba įklijuokite modulio šią procedūrą:

    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. Įrašykite modulio unikalų pavadinimą ir uždarykite langą modulis.

  8. Nustatyti ypatybę BeforeUpdate frmAccounts formos įvykio šią procedūrą:

    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. Derinti meniu spustelėkite sudarydami < savo duomenų bazės pavadinimą >

  10. Jei nėra klaidų, įrašykite formą.

  11. Atidaryti formą frmAccounts, ir tada įtraukti naują įrašą arba redaguoti įrašą.

    Kai pakeičiate įrašą, įrašas išsaugoma, kai perkeliate į kitą įrašą. Jei ODBC klaida įvyksta, matote pasirinktinį pranešimą, pagrįstą serverio konkrečių klaidų, ir kad bendras "ODBC – iškviesti nepavyko" pranešimą yra fiksuojamas.

Reikia daugiau pagalbos?

Norite daugiau parinkčių?

Sužinokite apie prenumeratos pranašumus, peržiūrėkite mokymo kursus, sužinokite, kaip apsaugoti savo įrenginį ir kt.

Bendruomenės padeda užduoti klausimus ir į juos atsakyti, pateikti atsiliepimų ir išgirsti iš ekspertų, turinčių daug žinių.

Ar ši informacija buvo naudinga?

Ar esate patenkinti kalbos kokybe?
Kas turėjo įtakos jūsų įspūdžiams?
Paspaudus mygtuką Pateikti, jūsų atsiliepimai bus naudojami tobulinant „Microsoft“ produktus ir paslaugas. Jūsų IT administratorius galės rinkti šiuos duomenis. Privatumo patvirtinimas.

Dėkojame už jūsų atsiliepimą!

×