C
CaptainBly
Several times over the years I have set up small maintenance programs
that compact backend databases at clients. I generally write a small
application and log results in a table so the client can check that the
compact worked. I then set up a scheduled event to run this
application and all seems to work great.
I was at a client this last week and wanted to set up the same thing.
My logic is using the dbengine.compactdatabase statement. I check if
the newdb exists and if so, I delete it. I then compact the sourcedb
into the newdb. Then I delete the sourcedb and copy the newdb to the
sourcedb. I also copy the newdb to a "play" db so the client has a
current play area of his data that he can work in and test different
things.
I log each of these activities in a table so the client can check if
the compacts have been working.
This has always been no issue.
This time though, for some reason, the newdb that is created comes back
with an unrecognizeable database format. The sourcedb is 275 meg and
the newdb is only 7 meg.
The other issue is that the code doesn't get any errors. Normally, the
compactdatabase will send back an error if someone is in the program or
anything like that and I log the error. This doesn't do that for some
reason. It continues on like nothing happened which would then put a
corrupt db out there as my product db and my play db.
Anyway, that obviously isn't good. The only nuance with this
application is that the database I am doing this with is an access
secured database. I open my compact application with the administrator
user and password but it still bombs. Not really sure what the issue
is. I can do a command line compact and repair with no issues so I am
at a loss on this one.
Any ideas would be greatly appreciated.
Here is the code if that would be helpful:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_form_open
Dim customix As Database
Dim filename As String
Dim filename2 As String
Dim filename3 As String
Dim sqlstring As String
Dim comphist As Recordset
Dim currfile As String
Dim playcomp As Recordset
Dim fs
Set customix = CurrentDb()
sqlstring = "Delete * from compacthistory where starttime<#" & Date
- 7 & "#;"
customix.Execute sqlstring
Set comphist = customix.OpenRecordset("CompactHistory",
DB_OPEN_DYNASET, DB_APPENDONLY)
comphist.AddNew
comphist![DBName] = "MyData.mdb"
comphist![starttime] = Now()
currfile = "FP"
filename = "Z:\Data\AAA_TEMP.MDB"
filename2 = "Z:\Data\MyData.mdb"
filename3 = "z:\data\MyData_Play.mdb"
' delete the temporary file if it exists
Set fs = CreateObject("Scripting.FileSystemObject")
If (fs.FileExists(filename)) Then
Kill filename
End If
' compact the live db to the temporary db
DBEngine.CompactDatabase filename2, filename
' delete the live db
Kill filename2
' copy the temp db to the live db
FileCopy filename, filename2
Kill filename
' delete the play db
Kill filename3
' create a new play db
FileCopy filename2, filename3
Set customix = CurrentDb()
Set playcomp = customix.OpenRecordset("tblCompanyProfile",
DB_OPEN_DYNASET)
playcomp.MoveFirst
playcomp.Edit
playcomp![companyname] = "Play Area Created " & Format(Date,
"mm/dd/yy")
playcomp.Update
comphist![stoptime] = Now()
comphist![errormsg] = "Operation Successful!"
comphist.Update
Exit_form_open:
DoCmd.Quit
Exit Sub
Err_form_open:
comphist![stoptime] = Now()
comphist![errormsg] = Error & " - " & Err.Description
comphist.Update
Resume Exit_form_open
End Sub
that compact backend databases at clients. I generally write a small
application and log results in a table so the client can check that the
compact worked. I then set up a scheduled event to run this
application and all seems to work great.
I was at a client this last week and wanted to set up the same thing.
My logic is using the dbengine.compactdatabase statement. I check if
the newdb exists and if so, I delete it. I then compact the sourcedb
into the newdb. Then I delete the sourcedb and copy the newdb to the
sourcedb. I also copy the newdb to a "play" db so the client has a
current play area of his data that he can work in and test different
things.
I log each of these activities in a table so the client can check if
the compacts have been working.
This has always been no issue.
This time though, for some reason, the newdb that is created comes back
with an unrecognizeable database format. The sourcedb is 275 meg and
the newdb is only 7 meg.
The other issue is that the code doesn't get any errors. Normally, the
compactdatabase will send back an error if someone is in the program or
anything like that and I log the error. This doesn't do that for some
reason. It continues on like nothing happened which would then put a
corrupt db out there as my product db and my play db.
Anyway, that obviously isn't good. The only nuance with this
application is that the database I am doing this with is an access
secured database. I open my compact application with the administrator
user and password but it still bombs. Not really sure what the issue
is. I can do a command line compact and repair with no issues so I am
at a loss on this one.
Any ideas would be greatly appreciated.
Here is the code if that would be helpful:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_form_open
Dim customix As Database
Dim filename As String
Dim filename2 As String
Dim filename3 As String
Dim sqlstring As String
Dim comphist As Recordset
Dim currfile As String
Dim playcomp As Recordset
Dim fs
Set customix = CurrentDb()
sqlstring = "Delete * from compacthistory where starttime<#" & Date
- 7 & "#;"
customix.Execute sqlstring
Set comphist = customix.OpenRecordset("CompactHistory",
DB_OPEN_DYNASET, DB_APPENDONLY)
comphist.AddNew
comphist![DBName] = "MyData.mdb"
comphist![starttime] = Now()
currfile = "FP"
filename = "Z:\Data\AAA_TEMP.MDB"
filename2 = "Z:\Data\MyData.mdb"
filename3 = "z:\data\MyData_Play.mdb"
' delete the temporary file if it exists
Set fs = CreateObject("Scripting.FileSystemObject")
If (fs.FileExists(filename)) Then
Kill filename
End If
' compact the live db to the temporary db
DBEngine.CompactDatabase filename2, filename
' delete the live db
Kill filename2
' copy the temp db to the live db
FileCopy filename, filename2
Kill filename
' delete the play db
Kill filename3
' create a new play db
FileCopy filename2, filename3
Set customix = CurrentDb()
Set playcomp = customix.OpenRecordset("tblCompanyProfile",
DB_OPEN_DYNASET)
playcomp.MoveFirst
playcomp.Edit
playcomp![companyname] = "Play Area Created " & Format(Date,
"mm/dd/yy")
playcomp.Update
comphist![stoptime] = Now()
comphist![errormsg] = "Operation Successful!"
comphist.Update
Exit_form_open:
DoCmd.Quit
Exit Sub
Err_form_open:
comphist![stoptime] = Now()
comphist![errormsg] = Error & " - " & Err.Description
comphist.Update
Resume Exit_form_open
End Sub