Hi
I am having a problem using the below code that I found somewhere using GOOGLE......I am not getting any error. I wanted to modify the below vba to not only compact&repair but to also move the database to another folder
I added the cfso.Movefile ofcourse after i defined it. Check below code.
Now the problem i am having and cant figure out a way around it is the compacted database is moved to the desired folder but it is not being named correctly. I always get NewDBName.
I have more than one database to compact so having an output of "NewDBName" is replacing the other databases.
But the vba logic is to take the current Database and add the date code to it compact it then move it and then go to the next database and so on so forth
Any help will be greatly appreciated, I just cant see what am I doing wrong
I am having a problem using the below code that I found somewhere using GOOGLE......I am not getting any error. I wanted to modify the below vba to not only compact&repair but to also move the database to another folder
Code:
Option Compare Database
Private Sub Form_Timer()
'==================================================================
'The Timer event runs this code every minute. It compares your
'system time with the StartTime variable. When they match, it
'starts to compact all databases in the DBNames table.
'==================================================================
Dim StartTime As String
' Set this variable for the time you want compacting to start.
StartTime = "02:58 PM"
' If StartTime is now, open the DBNames table and start compacting
If Format(Now(), "medium time") = Format(StartTime, _
"medium time") Then
Dim RS As Recordset, DB As Database
Dim NewDBName As String, DBName As String
Set DB = CurrentDb()
Set RS = DB.OpenRecordset("DBNames")
On Error Resume Next
RS.MoveFirst
Do Until RS.EOF
DBName = RS("DBFolder") & "\" & RS("DBName")
' Create a new name for the compacted database.
' This example uses the old name plus the current date.
NewDBName = Left(DBName, Len(DBName) - 4)
NewDBName = NewDBName & " " & Format(Date, "MMDDYY") & ".mdb"
DBEngine.CompactDatabase DBName, NewDBName
Loop
' Close the form, and then close Microsoft Access
DoCmd.Close acForm, "CompactDB", acSaveYes
RS.Close
DoCmd.Quit acSaveYes
End If
End Sub
I added the cfso.Movefile ofcourse after i defined it. Check below code.
Code:
Dim StartTime As String
' Set this variable for the time you want compacting to start.
Dim cFso
Set cFso = CreateObject("Scripting.FileSystemObject")
StartTime = "02:58 PM"
' If StartTime is now, open the DBNames table and start compacting
If Format(Now(), "medium time") = Format(StartTime, _
"medium time") Then
Dim RS As Recordset, DB As Database
Dim NewDBName As String, DBName As String
Set DB = CurrentDb()
Set RS = DB.OpenRecordset("DBNames")
On Error Resume Next
RS.MoveFirst
Do Until RS.EOF
DBName = RS("DBFolder") & "\" & RS("DBName")
' Create a new name for the compacted database.
' This example uses the old name plus the current date.
NewDBName = Left(DBName, Len(DBName) - 4)
NewDBName = NewDBName & " " & Format(Date, "MMDDYY") & ".mdb"
DBEngine.CompactDatabase DBName, NewDBName
cFso.Movefile "S:\Departments\Quality\QA Database\NewDBName", "S:\Departments\Quality\Databases Backup\NewDBName"
RS.MoveNext
Loop
' Close the form, and then close Microsoft Access
DoCmd.Close acForm, "CompactDB", acSaveYes
RS.Close
DoCmd.Quit acSaveYes
End If
End Sub
Now the problem i am having and cant figure out a way around it is the compacted database is moved to the desired folder but it is not being named correctly. I always get NewDBName.
I have more than one database to compact so having an output of "NewDBName" is replacing the other databases.
But the vba logic is to take the current Database and add the date code to it compact it then move it and then go to the next database and so on so forth
Any help will be greatly appreciated, I just cant see what am I doing wrong