Piezīme.: Mēs vēlamies sniegt jums visjaunāko palīdzības saturu jūsu valodā, cik vien ātri to varam. Šī lapa ir tulkota automatizēti, un tajā var būt gramatiskas kļūdas un neprecizitātes. Mūsu nolūks ir šo saturu padarīt jums noderīgu. Vai lapas lejasdaļā varat mums pavēstīt, vai informācija jums noderēja? Ērtai atsaucei šeit ir šis raksts angļu valodā.
Ja iestatāt rekvizītu OnError Access veidlapas notikuma procedūru, nevar ielādēt ODBC kļūda šajā procedūrā apraksts un arī nevar konstatētu noteiktu ODBC kļūda. Kad ODBC kļūda rodas, vienīgā informācija, kas tiek pārsūtīts uz kļūdas notikuma procedūra ir skaits vispārēju kļūdas, piemēram, 3146, kas atbilst kļūdas ziņojums: ODBC izsaukums neizdevās.
Iemesls
ODBC kļūdu ziņojumus parasti sastāv no divām daļām. Pirmais komponents ir kļūda 3146, kuru apraksts ir:
ODBC izsaukums neizdevās
Servera kļūdas informācija ir iekļauta otrais komponents, no kuriem var izgūt kļūdas numuru un aprakstu, piemēram:
[Microsoft] [ODBC SQL servera draivera] SQL Server < servera raksturīgās kļūdas ziņojums > (#< kļūdas numurs >)
Ja iestatāt veidlapas rekvizītu notikuma procedūru, varat konstatētu pirmais komponents kļūdu skaitu, bet nevar konstatētu otro komponentu skaits. Servera saistītas informācijas otrajā daļā ODBC kļūda tiek parādīta ekrānā pēc kods ir pabeigta, darbojas, ja vien jūs iekļaut šādu rindu notikumu procedūru:
Atbilde = acDataErrContinue
Izšķirtspēja
Piezīme.: Microsoft nodrošina programmēšanas piemēri ilustrācija, tikai bez garantijām vai izteikta netiešās. Tas ietver, bet neaprobežojoties, ne netiešu garantiju par pieprasījumu, vai atbilstību noteiktam mērķim. Šajā rakstā ir pieņemts, ka jums ir pazīstami ar programmēšanas valoda, kas ir to un rīkus, kas tiek izmantoti, lai izveidotu un atkļūdošanas procedūras. Microsoft atbalsta inženieri var palīdzēt izskaidrot konkrētu procedūru funkcionalitāti, bet tās nevar mainīt šos piemērus, lai sniegtu pievienotās funkcionalitāti vai izveidotu procedūras, kas atbilstu īpašām prasībām.
Varat izveidot Microsoft Visual Basic for Applications procedūra, kas izmanto datu piekļuves objekti (DAO-Data Access Objects), lai atjauninātu rekvizītu , kuras pamatā ir veidlapas. Tas ļauj jums, lai konstatētu jebkuru kļūdas ziņojumu, ko saņemat.
DAO ir kļūdas kolekcijā, kurā var izmantot, lai konstatētu servera saistītas informācijas otrajā daļā ODBC kļūda. Kad ODBC kļūda rodas, pirmais komponents tiek glabāta kļūdu kolekcijas pirmā elementa, un otrā daļa tiek glabāta otrais elements.
Šajā rakstā piemērā BeforeUpdate notikuma vietā kļūdas notikumu, lai konstatētu konkrētās ODBC kļūdas. Lai izveidotu darblapas funkcija, kas aiztur ODBC konkrētās kļūdas, kad notiek BeforeUpdate notikums veidlapas, rīkojieties šādi:
-
Izveidojiet tukšu datora datu bāzi.
-
Saite uz Microsoft SQL Server datu bāzes AdventureWorks parauga dbo_Accounts tabulā.
-
Izmantojiet formu vednis — kolonnveida izkārtojumu, lai izveidotu jaunas veidlapas pamatā ir tabula kontiem.
-
Saglabājiet veidlapu kā frmAccounts.
-
Izveidot jaunu moduli un pēc tam ierakstiet šādu rindu deklarāciju sadaļā, ja šīs līnijas vēl nav norādīts:
Opcija precīzas
-
Ierakstiet vai ielīmējiet tālāk norādīto procedūru moduļa:
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
-
Saglabājiet moduļa ar unikālu nosaukumu un aizveriet moduļa loga.
-
Iestatiet rekvizītam BeforeUpdate frmAccounts veidlapas notikumu šādi:
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
-
Izvēlnē atkļūdošana noklikšķiniet uz Kompilēt < datu bāzes nosaukumu >
-
Ja kļūdu nav, saglabājiet formu.
-
Atveriet frmAccounts formu, un pēc tam pievienojiet jaunu ierakstu vai ieraksta rediģēšana.
Kad veicat izmaiņas ar ierakstu, ieraksts tiek saglabāts, kad pārejat uz citu ierakstu. Ja rodas ODBC kļūda, redzat pielāgotu ziņojumu, kuras pamatā ir servera kļūdas, un vispārēju "ODBC — zvans neizdevās" uztver ziņojumu.