G
Gntlhnds
I have code set up that I have found in these newsgroups and modified to
compact/backup a backend. The code opens a dialog box for the user to select
the backend to compact/backup. Since there is only one backend, what I would
like to do is have the code programmatically select the backend to
compact/backup. I have pulled the code from mvps.org, but I have no idea how
to implement it. Here's the code that I have now:
'*************** Code Start **************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
'
Function fGetLinkPath(strTable As String) As String
Dim dbs As Database, stPath As String
Set dbs = CurrentDb()
On Error Resume Next
stPath = dbs.TableDefs(strTable).Connect
If stPath = "" Then
fGetLinkPath = vbNullString
'can change this to currentdb.name
Else
fGetLinkPath = Right(stPath, Len(stPath) _
- (InStr(1, stPath, "DATABASE=") + 8))
End If
Set dbs = Nothing
End Function
Sub sListPath()
Dim loTd As TableDef
CurrentDb.TableDefs.Refresh
For Each loTd In CurrentDb.TableDefs
Debug.Print fGetLinkPath(loTd.name)
Next loTd
Set loTd = Nothing
End Sub
Function DoBackup()
On Error GoTo Err_DoBackup
Dim strBackupFile As String
Dim strCurrentFile As String
Dim strDateStamp As String
Dim strCurrentLockFile As String
MsgBox "First, select the file that contains the tables. Second, select
the folder to save the backup copy.", , "Compact/Backup Procedures"
strCurrentFile = ahtCommonFileOpenSave(DialogTitle:="Please Select the
File with Your Tables")
If Len(strCurrentFile) = 0 Then
MsgBox "Your Tables were not backed up", , "Cancel Backup"
GoTo Exit_DoBackup
Else
'something was selected
End If
DoCmd.Hourglass True
strDateStamp = Format(Date, "yyyy_mm_dd")
strBackupFile = ahtCommonFileOpenSave(FileName:=strDateStamp,
DefaultExt:="mdb", DialogTitle:="Please Select a Location to Save Your
Backup...", OpenFile:=False)
If Len(strBackupFile) = 0 Then
MsgBox "Your Tables were not backed up", , "Cancel Backup"
GoTo Exit_DoBackup
Else
'something was selected
End If
strCurrentLockFile = strCurrentFile & ".ldb"
If Len(Dir(strCurrentLockFile)) > 0 Then
MsgBox "Cannot backup the database: it's in use", , "Backup "
Else
If Len(Dir(strBackupFile)) > 0 Then
Kill strBackupFile
End If
Application.CompactRepair strCurrentFile, strBackupFile
Kill strCurrentFile ' or better to rename it
Application.CompactRepair strBackupFile, strCurrentFile
DoCmd.Beep
MsgBox "Database backup successful...", , "Backup Confirmation"
End If
' Copy the database
Exit_DoBackup:
DoCmd.Hourglass False
Exit Function
Err_DoBackup:
MsgBox Str(Err)
MsgBox Error$
Resume Exit_DoBackup
End Function
**********************************
What I don't know is what to put "strCurrentFile = " to. Right now it is
set to open a Open File dialog box. I prefer leaving the strBackupFile to
what it is now because it allows the user to set the location of the backup
file and to name it what they want. Thanks for your help.
compact/backup a backend. The code opens a dialog box for the user to select
the backend to compact/backup. Since there is only one backend, what I would
like to do is have the code programmatically select the backend to
compact/backup. I have pulled the code from mvps.org, but I have no idea how
to implement it. Here's the code that I have now:
'*************** Code Start **************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
'
Function fGetLinkPath(strTable As String) As String
Dim dbs As Database, stPath As String
Set dbs = CurrentDb()
On Error Resume Next
stPath = dbs.TableDefs(strTable).Connect
If stPath = "" Then
fGetLinkPath = vbNullString
'can change this to currentdb.name
Else
fGetLinkPath = Right(stPath, Len(stPath) _
- (InStr(1, stPath, "DATABASE=") + 8))
End If
Set dbs = Nothing
End Function
Sub sListPath()
Dim loTd As TableDef
CurrentDb.TableDefs.Refresh
For Each loTd In CurrentDb.TableDefs
Debug.Print fGetLinkPath(loTd.name)
Next loTd
Set loTd = Nothing
End Sub
Function DoBackup()
On Error GoTo Err_DoBackup
Dim strBackupFile As String
Dim strCurrentFile As String
Dim strDateStamp As String
Dim strCurrentLockFile As String
MsgBox "First, select the file that contains the tables. Second, select
the folder to save the backup copy.", , "Compact/Backup Procedures"
strCurrentFile = ahtCommonFileOpenSave(DialogTitle:="Please Select the
File with Your Tables")
If Len(strCurrentFile) = 0 Then
MsgBox "Your Tables were not backed up", , "Cancel Backup"
GoTo Exit_DoBackup
Else
'something was selected
End If
DoCmd.Hourglass True
strDateStamp = Format(Date, "yyyy_mm_dd")
strBackupFile = ahtCommonFileOpenSave(FileName:=strDateStamp,
DefaultExt:="mdb", DialogTitle:="Please Select a Location to Save Your
Backup...", OpenFile:=False)
If Len(strBackupFile) = 0 Then
MsgBox "Your Tables were not backed up", , "Cancel Backup"
GoTo Exit_DoBackup
Else
'something was selected
End If
strCurrentLockFile = strCurrentFile & ".ldb"
If Len(Dir(strCurrentLockFile)) > 0 Then
MsgBox "Cannot backup the database: it's in use", , "Backup "
Else
If Len(Dir(strBackupFile)) > 0 Then
Kill strBackupFile
End If
Application.CompactRepair strCurrentFile, strBackupFile
Kill strCurrentFile ' or better to rename it
Application.CompactRepair strBackupFile, strCurrentFile
DoCmd.Beep
MsgBox "Database backup successful...", , "Backup Confirmation"
End If
' Copy the database
Exit_DoBackup:
DoCmd.Hourglass False
Exit Function
Err_DoBackup:
MsgBox Str(Err)
MsgBox Error$
Resume Exit_DoBackup
End Function
**********************************
What I don't know is what to put "strCurrentFile = " to. Right now it is
set to open a Open File dialog box. I prefer leaving the strBackupFile to
what it is now because it allows the user to set the location of the backup
file and to name it what they want. Thanks for your help.