اذهب الي المحتوي
أوفيسنا

شخابيط ابو جودى: سلسلة منافع مختلفة لتجميعات دوال عامة وافكار مختلفة ( بداية مهمه لتسجيل الاخطاء)


ابو جودي

الردود الموصى بها

السلام عليكم ورحمة الله تعالى وبركاته

انا بصدد تصميم قاعدة بيانات فى عملى وتباعا ان شاء الله اضع بين اياديكم خلاصة مجهود وتعليم سنوات

اولا تسجيل الاخطاء ومعالجتها
اولا موديول باسم : basErrorHandling

 

Public strProcessName As String

' The name of the table where errors are logged
Public Const TABLE_ERROR_LOG_NAME As String = "tblErrorLog"

' Subroutine to log errors in the error log table
Sub ErrorLog(ByVal intErrorNumber As Integer, ByVal strErrorDescription As String, ByVal strErrorProcessName As String)
    On Error GoTo Err_ErrorLog

    Dim strErrorMsg As String
    strErrorMsg = "Error " & intErrorNumber & ": " & strErrorDescription

    ' Show a message to the user
    MsgBox strErrorMsg, vbQuestion, strErrorProcessName

    ' Log error details in the error log table
    With CurrentDb.OpenRecordset(TABLE_ERROR_LOG_NAME)
        .AddNew
        ![ErrorNumber] = intErrorNumber
        ![ErrorDescription] = Left$(strErrorDescription, 255)
        ![ErrorProcessName] = strErrorProcessName
        ![ErrorDate] = Now()
        ![userName] = GetLoggedUserName()
        .Update
        .Close
    End With

Exit_ErrorLog:
    Exit Sub

Err_ErrorLog:
    ' Error message in case of an unexpected issue
    strErrorMsg = "An unexpected situation arose in your program." & vbNewLine
    strErrorMsg = strErrorMsg & "Please write down the following details:" & vbNewLine & vbNewLine
    strErrorMsg = strErrorMsg & "Calling Proc: " & strErrorProcessName & vbNewLine
    strErrorMsg = strErrorMsg & "Error Number " & intErrorNumber & vbNewLine & strErrorDescription & vbNewLine & vbNewLine
    strErrorMsg = strErrorMsg & "Unable to record because Error " & Err.Number & vbNewLine & Err.Description & vbNewLine
    strErrorMsg = strErrorMsg & "Occurred at Line: " & Erl
    MsgBox strErrorMsg, vbCritical, "ErrorLog()"
    Resume Exit_ErrorLog
End Sub


' Subroutine to handle and log errors
' This subroutine checks for errors and logs them using the ErrorLog function.
' It clears the error after logging it.
' Parameters:
'   - strProcName: The name of the procedure where the error occurred.
Public Sub HandleAndLogError(ByVal strProcName As String)
    ' Check for errors
    If Err.Number <> 0 Then
        ' Handle the error and log it
        Call ErrorLog(Err.Number, Err.Description, strProcName)
        ' Clear the error
        Err.Clear
    End If
End Sub


' Function to get the logged username, or return "N/A" if not available
Function GetLoggedUserName() As String
    On Error Resume Next
    Dim userName As String
    userName = Environ("USERNAME")
    If Err.Number <> 0 Then
        userName = "N/A"
        Err.Clear
    End If
    On Error GoTo 0
    GetLoggedUserName = userName
End Function

 

----------------------------------------------------------------------
 

ثانيا مويدول باسم :  basInitialization

' The name of the table where errors are logged
Public Const TABLE_ERROR_LOG_NAME As String = "tblErrorLog"

' Subroutine to initialize the application
Sub InitializeApplication()
    ' Initialize the error log table if it doesn't exist
    If Not IsErrorLogTableInitialized() Then CreateErrorLogTable
End Sub

' Check if the error log table exists and is initialized
Function IsErrorLogTableInitialized() As Boolean
    Dim db As DAO.Database
    Dim rs As DAO.Recordset

    ' Use error handling to check if the error log table exists
    On Error Resume Next
    Set db = CurrentDb
    Set rs = db.OpenRecordset(TABLE_ERROR_LOG_NAME)
    On Error GoTo 0

    ' Check if the error log table is initialized (contains necessary fields)
    If Not rs Is Nothing Then
        On Error Resume Next
        rs.MoveFirst
        IsErrorLogTableInitialized = (Err.Number = 0) And (rs.Fields.Count >= 6)
        On Error GoTo 0
        rs.Close
    End If

    Set rs = Nothing
    Set db = Nothing
End Function

' Subroutine to create the error log table
Sub CreateErrorLogTable()
    On Error Resume Next

    Dim db As DAO.Database
    Set db = CurrentDb

    ' Check if the table already exists
    If Not IsTableExists(TABLE_ERROR_LOG_NAME, db) Then
        ' Define the SQL code to create the table
        Dim strSQL As String
        strSQL = "CREATE TABLE " & TABLE_ERROR_LOG_NAME & " (" & _
                 "ID AUTOINCREMENT PRIMARY KEY, " & _
                 "ErrorProcessName TEXT(255), " & _
                 "ErrorNumber LONG, " & _
                 "ErrorDescription MEMO, " & _
                 "ErrorDate DATETIME, " & _
                 "UserName TEXT(255));"

        ' Execute the SQL command to create the table directly
        DoCmd.RunSQL strSQL
    End If

    Set db = Nothing
    On Error GoTo 0
End Sub

' Function to check if a table exists in the database
Function IsTableExists(tableName As String, Optional db As DAO.Database) As Boolean
    ' Use DLookup to check for the existence of the table in MSysObjects
    On Error Resume Next
    Set db = IIf(db Is Nothing, CurrentDb, db)
    IsTableExists = Not IsNull(DLookup("Name", "MSysObjects", "Name='" & tableName & "'"))
    On Error GoTo 0
End Function

وظيفة الموديول هو تهئة ما اريد لقاعدة البيانات البدء به ومن خلاله


----------------------------------------------------------------------
3- نموذج البداية وليكن الان باسم frmInitialization
وفى حدث عند التحميل نضع الكود الاتى 

 

Private Sub Form_Load()
    strProcessName = "Form Load : frmIntialization"
    On Error Resume Next
    
    ' Initialize the application when the startup form is loaded.
    InitializeApplication
    
    ' Add calls to the initialized special functions through which you want the database to be booted
    ' Or add specify the codes through which you would like to process the data later according to the requirements of your design

    ' Set the current procedure name (you can adjust the procedure name as needed)
    If Err.Number <> 0 Then
        ' Handle the error (display a message)
        Call ErrorLog(Err, Error$, strProcessName)
    
        ' Clear the error
        Err.Clear
    End If
End Sub


النتيجة المرغوب فى الخصول عليها :
عند تشغيل القاعدة فى المرة الأولى تنشئ جدول تسجيل الأخطاء من تلقاء نفسها باسم الروتين او الحدث ورقم الخطاء والوصف

المتطلبات عند اعداد الاكواد تباعا نمرر اسم الروتين من خلال المتغير  strProcessName
كما فعلت فى الحدث السابق للنموذج:   strProcessName = "Form Load : frmIntialization"
لو حدث اى خطأ مستقبلا سوف يتم تسجيله حتى يستطيع مطور النظم او القائم على اعمال صيانة قواعد البيانات او المصمم معرفة مكان حدوث الخطأ 

الشق الثانى نقوم بعمل الايقاف للاخطا ليستكمل الكود عمله حتى لو وجودت اى اخطاء من خلال : On Error Resume Next

بعد كتابة الكود كما نريد وبعد ان ننتهى منه نضع الشرط التالى  If Err.Number <> 0 Then
بذلك نضع شرط عند الدوران على الكود لتنفيذه فى حالة وجود خطأ
اولا اظهر رسالة الخطأ حتى يعلم المستخدم سبب المشكلة ثم استدعى الدالة لتسجيل هذا الخطأ
ويتم ذلك من خلال  Call ErrorLog(Err, Error$, strProcessName)

الان هذه بداية احترافية وعلى اسس صحيحة ومفيدة للمستقبل

..... يتبع

 

 

HandleAndLogError.accdb

  • Like 2
  • Thanks 1
رابط هذا التعليق
شارك

  • ابو جودي changed the title to شخابيط ابو جودى: سلسلة منافع مختلفة لتجميعات دوال عامة وافكار مختلفة ( بداية مهمه لتسجيل الاخطاء)
  • Moosak pinned this topic

ما شاء الله تبارك الله  

الموضوع حاسه كبير شويه ويحتاج للتأمل 

بس غني ومهم 

 تسلم ايدك ويجعله فى ميزان حسناتك

رابط هذا التعليق
شارك

وعليكم السلام ورحمة الله وبركاته..

ما شاء الله تبارك الرحمن .. 🌹🌷

شغل معلمين يحتاجله 10 سطل قهوة عشان تجيب كل التركيز اللي في العالم لراسك 😅👌

هدخله المكتبه ازاي ده ؟؟!!! 😂

شكر لك باش مهندس ومنتظرين التكملة 👍🏼😊

  • Haha 1
رابط هذا التعليق
شارك

طبعا سوف اضع تباعا موضوع خاص لكل نقطة ويتم ربط الموضوعات بالتتابع من خلال وضع الرابط لها هنا ليكون المنتدى غنى بالموضوعات المنفصلة على وجه العموم وليكون الموضوع هنا مرجع متكامل على وجه الخصوص

ملحوظة: ساعتمد فى المرفقات كذلك على ان يكون كل مرفق خاص بالفكرة او الالية التى تخصه فقط تسهيلا على الجميع لدراسة كل فكرة على حده لتحقيق الاستفادة القصوى
 

الجزء الثانى:  انتقاء المجلدات والملفات والعمليات المختلفة و المرتبطه بها  >---->>     من هنا

  • Like 1
  • Thanks 1
رابط هذا التعليق
شارك

تبااارك الله ، وما شاء الله ، إبدااااااع بمعنى الكلمة

وتميز يستحق رفع القبعة له :clapping:

aFFdItqwvRknCPTs.jpg.66be2aaaf53ddd2f317b4db99236aad8.jpg

دي عايزة قعدة مترتبه ودمااااغ فاصل عن الكوكب ، وهدووووووووء

عشان التركيز طبعاً ، والإستمتاع بقراءة المحتوى

  • Haha 1
رابط هذا التعليق
شارك

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

زائر
اضف رد علي هذا الموضوع....

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • اضف...

Important Information