J
Janis
I'm not expert at error trapping but On error goto 0 turns off error trapping
which is what I want in this script since if there is an error it quits the
script before compacting the db. My question is why are there 3 On Error
Goto 0 statements? Why is 2nd On Error statement repeated ? Is it just to
make sure it is turned off in the If clause? The 3rd On Error statement
seems like a mistake but I'm not sure.
TIA,
Option Explicit
Public Sub CompactBackEnd()
Dim strMsg
'Dim objScript As Object
'Dim objScript As Scripting.FileSystemObject
Dim dbe As DBEngine
Set dbe = CreateObject("dao.dbengine.36")
'path is to System.mdb because it is a split database
dbe.SystemDB = "Z:\SwimClub\System.mdw"
dbe.DefaultUser = "Brian"
dbe.DefaultPassword = "Kiyote#3"
On Error GoTo 0
'perform db compact of backend mdb into a temp mdb first
'if there is a problem the original mdb is saved
dbe.CompactDatabase "Z:\SwimClub\acsc_be.mdb",
"Z:\SwimClub\temp_acsc_be.mdb", , , ";pwd=Kiyote#3"
If (Err.Number <> 0) Then
On Error GoTo 0
' There was an error. Inform the user and halt execution
strMsg = "The following error was encountered while compacting database:"
strMsg = strMsg & vbCrLf & vbCrLf & Err.Description
Call MsgBox(strMsg)
DoCmd.Quit
End If
On Error GoTo 0
' Back up the original file as Filename.mdbz. In case of undetermined
' error, it can be recovered by simply removing the terminating "z".
' deletes previous .mdbz file
If Dir("Z:\SwimClub\acsc_be.mdb" & "z") <> "" Then Kill
"Z:\SwimClub\acsc_be.mdb" & "z"
FileCopy "Z:\SwimClub\acsc_be.mdb", "Z:\SwimClub\acsc_be.mdb" & "z"
' Copy the compacted mdb by into the original file name
FileCopy "Z:\SwimClub\temp_acsc_be.mdb", "Z:\SwimClub\acsc_be.mdb"
' We are finished with TempDB. Kill it.
If Dir("Z:\SwimClub\temp_acsc_be.mdb") <> "" Then Kill
"Z:\SwimClub\temp_acsc_be.mdb"
Call MsgBox("compact successful")
End Sub
which is what I want in this script since if there is an error it quits the
script before compacting the db. My question is why are there 3 On Error
Goto 0 statements? Why is 2nd On Error statement repeated ? Is it just to
make sure it is turned off in the If clause? The 3rd On Error statement
seems like a mistake but I'm not sure.
TIA,
Option Explicit
Public Sub CompactBackEnd()
Dim strMsg
'Dim objScript As Object
'Dim objScript As Scripting.FileSystemObject
Dim dbe As DBEngine
Set dbe = CreateObject("dao.dbengine.36")
'path is to System.mdb because it is a split database
dbe.SystemDB = "Z:\SwimClub\System.mdw"
dbe.DefaultUser = "Brian"
dbe.DefaultPassword = "Kiyote#3"
On Error GoTo 0
'perform db compact of backend mdb into a temp mdb first
'if there is a problem the original mdb is saved
dbe.CompactDatabase "Z:\SwimClub\acsc_be.mdb",
"Z:\SwimClub\temp_acsc_be.mdb", , , ";pwd=Kiyote#3"
If (Err.Number <> 0) Then
On Error GoTo 0
' There was an error. Inform the user and halt execution
strMsg = "The following error was encountered while compacting database:"
strMsg = strMsg & vbCrLf & vbCrLf & Err.Description
Call MsgBox(strMsg)
DoCmd.Quit
End If
On Error GoTo 0
' Back up the original file as Filename.mdbz. In case of undetermined
' error, it can be recovered by simply removing the terminating "z".
' deletes previous .mdbz file
If Dir("Z:\SwimClub\acsc_be.mdb" & "z") <> "" Then Kill
"Z:\SwimClub\acsc_be.mdb" & "z"
FileCopy "Z:\SwimClub\acsc_be.mdb", "Z:\SwimClub\acsc_be.mdb" & "z"
' Copy the compacted mdb by into the original file name
FileCopy "Z:\SwimClub\temp_acsc_be.mdb", "Z:\SwimClub\acsc_be.mdb"
' We are finished with TempDB. Kill it.
If Dir("Z:\SwimClub\temp_acsc_be.mdb") <> "" Then Kill
"Z:\SwimClub\temp_acsc_be.mdb"
Call MsgBox("compact successful")
End Sub