Nota: Riteniamo importante fornire il contenuto della Guida più recente non appena possibile, nella lingua dell'utente. Questa pagina è stata tradotta automaticamente e potrebbe contenere errori di grammatica o imprecisioni. L'intento è quello di rendere fruibile il contenuto. Nella parte inferiore della pagina è possibile indicare se le informazioni sono risultate utili. Questo è l'articolo in lingua inglese per riferimento.
Se si imposta la proprietà SuErrore di una maschera di Access a una routine evento, non è possibile recuperare la descrizione di un errore ODBC nella procedura e anche non è possibile registrare un errore ODBC specifico. Quando si verifica un errore ODBC, le informazioni sola di quello passate alla routine evento errore sono il numero di un errore generico, ad esempio 3146, che corrisponde al messaggio di errore: chiamata ODBC non è riuscita.
Causa
Messaggi di errore ODBC solitamente costituita da due componenti. Il primo componente è errore 3146, la cui descrizione è:
Chiamata ODBC non riuscita
Le informazioni sull'errore specifici server è contenute nel secondo componente, da cui è possibile recuperare un numero di errore e una descrizione come:
[Microsoft] [Driver ODBC SQL Server] SQL Server < messaggio di errore specifici Server > (#< numero errore >)
Se si imposta la proprietà SuErrore di un modulo a una routine evento, è possibile registrare il numero del primo componente dell'errore, ma non è possibile registrare il numero del secondo componente. Le informazioni specifiche di server nella seconda parte dell'errore ODBC visualizzato sullo schermo dopo il codice in esecuzione, a meno che non includere la riga seguente nell'evento procedura:
Risposta = acDataErrContinue
Risoluzione
Nota: Microsoft offre esempi di programmazione per illustrazione solo, senza garanzia espressa o implicita. Include, ma non è limitata alle garanzie di commerciabilità o idoneità per uno scopo specifico. In questo articolo si presuppone che si ha familiarità con il linguaggio di programmazione in questione e gli strumenti utilizzati per creare ed eseguire il debug di procedure. Tecnici del supporto Microsoft consentono di spiegare la funzionalità di una particolare procedura, ma non possono modificare questi esempi per fornire funzionalità aggiuntive o creare procedure in base alle specifiche esigenze.
È possibile creare un Microsoft Visual Basic, Applications Edition che utilizza oggetti DAO (Data Access) per aggiornare un RecordsetClone basato sul modulo. In questo modo di intercettare qualsiasi messaggio di errore visualizzato.
DAO contiene un insieme di errori che è possibile utilizzare per registrare le informazioni specifiche di server nella seconda parte dell'errore ODBC. Quando si verifica un errore ODBC, il primo componente verrà archiviato nel primo elemento della raccolta di errori e il secondo componente è archiviato nel secondo elemento.
L'esempio in questo articolo utilizzato l'evento prima di aggiornare anziché l'evento di errore per la registrazione di errori specifici nelle ODBC. Per creare una funzione che intercetta errori specifici nelle ODBC quando si verifica l'evento prima di aggiornare di un modulo, attenersi alla seguente procedura:
-
Creare un database desktop vuoto.
-
Creare un collegamento alla tabella dbo_Accounts nel database di esempio AdventureWorks in Microsoft SQL Server.
-
Utilizzare la creazione guidata Maschera - layout a colonne per creare una nuova maschera basata sulla tabella account.
-
Salvare la maschera come frmAccounts.
-
Creare un nuovo modulo e quindi digitare la riga seguente nella sezione dichiarazioni se tale riga non è già presente:
Option Explicit
-
Digitare o incollare la procedura seguente nel modulo:
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
-
Salvare il modulo con un nome univoco e chiudere la finestra del modulo.
-
Impostare la proprietà prima di aggiornare della maschera frmAccounts alla routine evento seguenti:
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
-
Nel menu Debug fare clic su Compila < nome del database >
-
Se non si verificano errori, salvare la maschera.
-
Aprire la maschera frmAccounts, quindi aggiungere un nuovo record o modificare un record.
Quando si apporta modifiche a un record, il salvataggio del record quando si passa a un altro record. Se si verifica un errore ODBC, viene visualizzato il messaggio personalizzato basato su errori specifici di server e il generico "ODBC: chiamata non riuscita" registrazione dei colori del messaggio.