PERFORMING STARTUP SYSTEM CHECKS

When you're creating applications in the real world, it's important to make them the most robust systems possible. To do so, you must start when the application is first loading.

It's highly recommended that you perform startup checking at various levels, especially regarding the back-end database and the connections into it. Many problems can occur for which you can provide a way to resolve automatically or at least have the system back users out gracefully. Some problems that can occur, and for which no simple programming solution exists, happen when Windows causes an application error or when the front-end database is corrupt.

If Windows causes an application error (commonly referred to as a General Protection Fault), Windows 9x/NT usually displays a polite message box and returns you to the main desktop (see Figure 26.1). No more having to shut down Windows and restart the machine!

Figure 26.1. The Windows 9x/NT version of the General Protection Fault doesn't require rebooting Windows.


If the front end becomes corrupted, the user or administrator must repair the database. To do so from Access, follow these steps:

1.
Open Microsoft Access without specifying a database.

2.
Click Cancel in the Open Database dialog.

3.
From the Tools menu, choose Database Utilities and then Repair Database.

4.
In the Compact and Repair Database dialog, find the front-end .mdb file to repair and click Compare.

With the back end, it's important to trap all situations that can come up. This chapter can't cover every situation, but it provides solutions to some of the major ones. First, however, look at the routine that does the startup checking, in Listing 26.1. The code is broken up with comments, and each code segment is explained throughout the rest of this chapter. The function listed is ap_AppInit(), found in the modGlobalUtilities module in the VideoApp.mdb database, on the accompanying CD-ROM in the Examples folder.

Listing 26.1. VideoApp.mdb: The Main Startup System Checking Routine
Function ap_AppInit()

    Dim dbLocal As Database, dbNet As Database
    Dim dynSharedTables As Recordset, dynTestTable As Recordset
    Dim intCurrError As Integer, strCurrError As String

    DoEvents
    DoCmd.Echo True, "Checking Connections..."

    flgLeaveApplication = False

    Set dbLocal = CurrentDb()

    pstrAppPath = CurrentProject.Path
    pstrBackEndName = ap_GetDatabaseProp(dbLocal, "BackEndName")
    pstrBackEndPath = ap_GetDatabaseProp(dbLocal, "LastBackEndPath")
    '-- Section 2: User requested to logout, quit the application
    If ap_LogOutCheck(pstrBackEndPath) Then
        Beep
        MsgBox "Maintenance is being performed on the backend" & vbCrLf _
          & vbCrLf & "All users are requested to logout at this time.", _
          vbOKOnly + vbCritical, "Logging Out for Maintenance"
        Application.Quit
        Exit Function
    End If

    '-- Section 3: Open the table containing the list of linked tables
    Set dynSharedTables = dbLocal.OpenRecordset("tblSharedTables", _
       dbOpenDynaset)

    On Error Resume Next

    dynSharedTables.MoveLast

    Set dynTestTable = dbLocal.OpenRecordset(dynSharedTables!TableName, _
         dbOpenDynaset, dbSeeChanges)

    intCurrError = Err.Number
    strCurrError = Err.Description

    Do Until intCurrError = 0

       On Error GoTo Error_ap_App_Init

       Select Case intCurrError
         Case apErrFileNotFound, apErrTableNotFound, _
              apErrPathNotValid, apErrDeviceNotAvlble

            '-- Section 4: If the Data MDB is found in the App Directory,
            '--            link the files.

             If Dir(pstrAppPath & "" & pstrBackEndName) = _
                              pstrBackEndName Then

                ap_LinkTables dbLocal, dynSharedTables, pstrAppPath & _
                               "" & pstrBackEndName
                pstrBackEndPath = pstrAppPath

             Else
                '-- Section 5: Allow the user to locate the BackEnd MDB
                If Not ap_LocateBackend(dbLocal, dynSharedTables, _
                       strCurrError) Then
                   flgLeaveApplication = True
                End If

             End If

         Case apErrDBCorrupted1, apErrDBCorrupted2

            '-- Section 6: Backend Corrupted. Compact/Repair?
            Beep

            If MsgBox("The Backend Database is Corrupted." & vbCrLf & _
               vbCrLf & "Would you like to log users out and " & _
               " attempt to compact/repair it?", vbYesNo + vbCritical, _
               "Corrupted Backend!") = vbYes Then

               DoCmd.OpenForm "ap_CompactDatabase", acForm

            Else

               flgLeaveApplication = True

            End If

       End Select

       '-- Section 7: Leave the application if requested
       If flgLeaveApplication Then
          Application.Quit
          Exit Function
       End If

       On Error Resume Next

       '-- Section 8: Let's try and open the first table again.
       dynSharedTables.MoveFirst

       Set dynTestTable = _
          dbLocal.OpenRecordset(dynSharedTables!TableName, dbOpenDynaset)

       intCurrError = Err.Number
       strCurrError = Err.Description
    Loop

    On Error GoTo Error_ap_App_Init

    '-- Section 9: Check the version of the front end,
    '--            and point to a new one if necessary
    Set dbNet = OpenDatabase(pstrBackEndPath & "" & pstrBackEndName)
    If ap_GetDatabaseProp(dbLocal, "FrontEndVersion") <> _
        ap_GetDatabaseProp(dbNet, "FrontEndVersion") Then

       Beep
       MsgBox ap_GetDatabaseProp(dbNet, "NewVersionMessage"), _
              vbInformation, "New Version Available"
       Application.Quit
    End If

    '-- Section 10: Save the BackEnd path in the BackEndPath property
    '--             for future use.
    ap_SetDatabaseProp dbLocal, "LastBackEndPath", pstrBackEndPath

    '-- Check for locally logged errors
    ap_ErrorCheckLocal

    '-- Check for Replicated Tables
    ap_CheckReplicatedTables

    '-- Turn on the Monitor Form
    DoCmd.OpenForm "UserLogOutMonitor", , , , , acHidden

    DoCmd.Close acForm, "SplashScreen"
    DoCmd.Echo True
    dynSharedTables.Close

Exit_ap_App_Init:
   Exit Function

Error_ap_App_Init:
   Beep
   MsgBox "The following error occurred: " & Err.Description & vbCrLf & _
           vbCrLf & "The system will now be closed down!"
   Application.Quit

End Function

The first section in Listing 26.1 stores the following:

  • The front-end path to the variable pstrAppPath

  • The back-end name to pstrBackEndName

  • The last good back-end path to pstrBackEndPath

ap_AppInit()performs these tasks by storing and retrieving the values from properties on the database itself.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.72.212