הערה: אנו מעוניינים לספק לך את תוכן העזרה העדכני ביותר במהירות האפשרית, בשפה שלך. דף זה תורגם באמצעות אוטומציה והוא עשוי לכלול שגיאות דקדוק או אי-דיוקים. מטרתנו היא כי תוכן זה יהיה שימושי עבורך. האם תוכל לספר לנו אם המידע הועיל לך, בחלק התחתון של דף זה? לנוחותך, הנה המאמר באנגלית.
אם תגדיר את המאפיין OnError של טופס Access שגרת אירוע, לא ניתן לאחזר את התיאור של שגיאת ODBC באותו ההליך ולאחר גם אין אפשרות ללכוד שגיאה ODBC ספציפית. כאשר מתרחשת שגיאה ODBC, המידע היחיד המועברים לפרוצדורה אירוע שגיאה הוא מספר שגיאה כללית, כגון 3146, המתאים להודעת השגיאה: שיחת ODBC נכשלה.
סיבה
הודעות שגיאה של ODBC כוללים בדרך כלל שני מרכיבים. הרכיב הראשון הוא שגיאה 3146, התיאור שלו היא:
שיחת ODBC נכשלה
שגיאה ספציפית שרת שהמידע כלול ברכיב השני, שממנו תוכל לאחזר מספר שגיאה ותיאור כגון:
[Microsoft] [מנהל התקן של שרת ODBC SQL] [SQL Server] < הודעת שגיאה ספציפית Server > (#< מספר שגיאה >)
אם תגדיר את המאפיין OnError של טופס שגרת אירוע, באפשרותך ללכוד מספר הרכיב הראשון השגיאה, אך אין באפשרותך ללכוד מספר הרכיב השני. המידע ייחודיים לשרת בחלק השני של שגיאת ODBC מופיע במסך בסיום הקוד פועל, אלא אם כן אתה כולל את השורה הבאה האירועים הליך:
תגובה = acDataErrContinue
רזולוציה
הערה: Microsoft מספקת דוגמאות התכנות להמחשה בלבד, ללא אחריות, בין מפורשת ובין משתמעת. פעולה זו כוללת, אך אינה מוגבלת, אחריות לגבי סחירות או התאמה למטרה מסוימת. מאמר זה מניח שאתה מכיר את שפת התכנות המודגמת ועם כלים שבהם נעשה שימוש ליצירת וכדי באגים ההליכים. מהנדסי התמיכה של Microsoft יכולים לסייע בהסברת הפונקציונליות של פרוצדורה מסוימת, אך הם לא ישנו דוגמאות אלה כדי לספק פונקציונליות נוספת או כדי לבנות פרוצדורות שיענו על צרכיך הספציפיים.
באפשרותך ליצור של Microsoft Visual Basic for הליך יישומים המשתמשת אובייקטי גישה לנתונים (DAO) כדי לעדכן שהשתמשת המבוסס על הטופס. פעולה זו מאפשרת לך ללכוד כל הודעת השגיאה שאתה מקבל.
DAO מכיל אוסף שגיאות שבהם באפשרותך להשתמש כדי ללכוד את מידע ספציפי שרת בחלק השני של שגיאת ODBC. כאשר מתרחשת שגיאה ODBC, הרכיב הראשון מאוחסן בהרכיב הראשון של האוסף Errors, ומאוחסן הרכיב השני ברכיב השני.
הדוגמה במאמר זה עושה שימוש באירוע לפני עדכון במקום האירוע Error ללכידת שגיאות ספציפיות של ODBC. כדי ליצור בפונקציה המבצעת השמנת שגיאות ODBC ספציפיות כאשר האירוע לפני עדכון של טופס, בצע את הפעולות הבאות:
-
צור מסד נתונים ריק של שולחן עבודה.
-
קישור אל הטבלה dbo_Accounts במסד הנתונים לדוגמה AdventureWorks ב- Microsoft SQL Server.
-
השתמש באשף הטפסים - פריסה טורי כדי ליצור טופס חדש המבוסס על הטבלה חשבונות.
-
שמור את הטופס כ- frmAccounts.
-
צור מודול חדש ולאחר מכן הקלד את השורה הבאה במקטע ההצהרות אם שורה זו עדיין אינה קיימת:
האפשרות מפורשת
-
הקלד או הדבק את ההליך הבא לתוך המודול:
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
-
שמור את המודול עם שם ייחודי ולסגור את החלון מודול.
-
הגדר את המאפיין לפני עדכון של הטופס 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
-
בתפריט איתור באגים, לחץ על < שם מסד הנתונים שלך > הידור
-
אם מתרחשות שגיאות, שמור את הטופס.
-
פתח את הטופס frmAccounts, ולאחר מכן להוסיף רשומה חדשה או לערוך רשומה.
כאשר אתה מבצע שינוי רשומה, הרשומה נשמר בעת העברת לרשומה אחרת. אם מתרחשת שגיאה ODBC, תראה את ההודעה מותאם אישית המבוסס על השגיאה server ספציפי, ולחץ על כללי "ODBC - הקריאה נכשלה" לכוד ההודעה.