0

我有一些代码,它工作得非常好,可以在打开数据库时压缩 Access 数据库。

但是,我对如何做下一步有点困惑。

我想要发生的是,当数据库表中的项目被标记为存档并随后存档时,我希望这段代码几乎像 AutoExec 一样运行。

你能建议最好的方法吗?

提前致谢

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 = Now()
  ' 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, "DDMMYY") & ".mdb"
        DBEngine.CompactDatabase DBName, 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
4

0 回答 0