TESTING AND REPAIRING CORRUPTED JET BACK-END DATABASES

Section 6 of the ap_AppInit() function deals solely with handling the repair of the back end if it requires it:

  Case apErrDBCorrupted1

     '-- Section 6: Backend Corrupted. 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

This code hasn't changed from the DAO version.

Note

One big change that has occurred with Jet is that you no longer use Compact and Repair to perform the separate functions. They're combined in the CompactDatabase method in ADO and DAO. DAO is located off the Workspace object, as before; ADO can be found off the JRO.JetEngine object.


Section 6 starts by handling the situation where lngCurrError is equal to apErrDBCorrupted1, which is the constant used for corrupted database error. Next, a message box informs the user of the situation and asks whether he wants to try to compact/repair it.

After displaying the message box, the ap_CompactDatabase form opens. The OnLoad event of ap_CompactDatabase first checks whether anyone has already created the LogOut.flg file. If someone has created the file, the routine exits the system. Listing 27.5 shows the OnLoad procedure for ap_CompactDatabase.

Listing 27.5. VideoApp(ADO).mdb: Attempting to Compact/Repair the Database
Private Sub Form_Load()

   Dim strBackEndPath As String
   Dim strBackEndName As String
   Dim intCurrError As Integer

   '-- Grab the backend and path
   strBackEndPath = ap_GetDatabaseProp("LastBackEndPath")
   strBackEndName = ap_GetDatabaseProp("BackEndName")

   '-- If something else is up exit
   If ap_LogOutCheck(strBackEndPath) Then
     Beep
     MsgBox "Somebody has already requested users to log out." & vbCrLf _
        & vbCrLf & "All users are requested to logout at this time.", _
        vbOKOnly + vbCritical, "Logging Out for Maintenance"
     GoTo Exit_Form_Load
   End If

   '-- Create the flag file to log users out
   ap_LogOutCreate
   On Error GoTo Error_Form_Load

   DoCmd.SelectObject A_FORM, "ap_CompactDatabase"

   intCurrError = -1

   '-- Keep trying until no error
   Do While intCurrError <> 0

      DoCmd.Echo True, "Attempting To Compact BackEnd Database..."

      '-- This lets windows catch up
      DoEvents

      '-- This flag is set in the cmdCancel click event
      If intCancel Then
         GoTo Exit_Form_Load
      End If

      On Error Resume Next

      Dim jeBackend As New JRO.JetEngine

      ' Make sure that a file doesn't exist with the name of
      ' the compacted database.
      '-- Try to compact into a temp file
      jeBackend.CompactDatabase _
            apProvider & _
            "Data Source=" & strBackEndPath & strBackEndName, _
            apProvider & _
            "Data Source=" & strBackEndPath & "CompTemp.mdb"

      intCurrError = Err.Number

      '-- if no error, continue on
      If intCurrError = 0 Then

         On Error GoTo Error_Form_Load

         '-- Delete the original database,
         '-- then rename the temp to the original name
         Kill strBackEndPath & strBackEndName
         Name strBackEndPath & "CompTemp.mdb" As strBackEndPath & _
            strBackEndName
      End If

   Loop

   '-- Delete the flag file
   ap_LogOutRemove

   MsgBox "Backend Compacted Successfully!"

Exit_Form_Load:

   DoCmd.Close acForm, "ap_CompactDatabase"
   Exit Sub

Error_Form_Load:

   MsgBox "The backend has not been compacted. The request for " & _
     "users to log out will be canceled!" & vbCrLf & vbCrLf & _
     "Please notify the system administrator.", vbCritical, _
     "Compact Canceled"
   ap_LogOutRemove

   Resume Exit_Form_Load

End Sub

Tip

Notice that the constant apProvider is being used. If you take a look at the declarations section, you will see the following line of code:

Public Const apProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"

By using a constant such as this one for the provider string, you will save yourself many hassles later when you have to upgrade to a new version of ADO or Jet, or switch providers altogether.


Section 7 is unchanged between ADO and DAO. Section 8 uses the Open method off the recordset object, rather than the OpenRecordset method that DAO uses:

'-- 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.
rstTestTable.Open strTableName, cnnLocal, adOpenStatic
lngCurrError = Err.Number
strCurrError = Err.Description

Loop

The last section is section 9:

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

'-- Check for Replicated Tables
ap_CheckReplicatedTables

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

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

This code performs the following actions:

1.
It stores the current back-end path into the ztblDatabaseProperties table's LastBackEndPath field. (The ap_SetDatabaseProp routine was covered earlier in the section “Setting and Retrieving System Settings with ADO.”)

2.
The next two routines perform tasks that are discussed in other chapters. The ap_ErrorCheckLocal subroutine, discussed in Chapter 7, “Handling Your Errors in Access with VBA,” helps log errors into a table. The ap_CheckReplicatedTables subroutine, discussed in Chapter 28, “Creating Maintenance Routines,” replicates tables from the back end to the front end for performance purposes. The ADO version of ap_CheckReplicatedTables is discussed in the next section.

3.
The code opens the UserLogOutMonitor form. This form, used to monitor whether users need to log out, was discussed in Chapter 26 in the section “Logging Users Out in the Middle of the Application.”

4.
It closes the splash screen.

5.
The code closes the rstSharedTables recordset.

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

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