I have the following function that I found online. Unfortunately I can't remember where I got it since I've had it for a little while. Today, when I tried to actually put it to use it didn't work.
I'm calling it from a form as follows: CompactDB (tblHotword)
tblHotword is just a random table from the back end. My understanding of the function was that it would use that table to connect and get the file name of the back end.
Whenever I run it, Access pops up a window that says "Object required" and nothing else. It doesn't look like a standard error message popup. When I click 'OK', Access continues with the rest of the code as if nothing went wrong. The function doesn't run though.
Any idea where I've gone wrong?
Here is the (not yet completed) code that is actually calling the function
I'm calling it from a form as follows: CompactDB (tblHotword)
tblHotword is just a random table from the back end. My understanding of the function was that it would use that table to connect and get the file name of the back end.
Whenever I run it, Access pops up a window that says "Object required" and nothing else. It doesn't look like a standard error message popup. When I click 'OK', Access continues with the rest of the code as if nothing went wrong. The function doesn't run though.
Any idea where I've gone wrong?
Code:
Public Function CompactDB(TableName As String) As Boolean
On Error GoTo Err_CompactDB
Dim stFileName
DoCmd.Hourglass True
stFileName = db.TableDefs(TableName).Connect
stFileName = Mid(stFileName, InStr(stFileName, "=") + 1)
DBEngine.CompactDatabase stFileName, stFileName & "TMP"
If Dir(stFileName & ".BCK") <> "" Then _
Kill stFileName & ".BCK"
Name stFileName As stFileName & ".BCK"
Name stFileName & "TMP" As stFileName
If Dir(stFileName & "TMP") <> "" Then _
Kill stFileName & "TMP"
CompactDB = True
Exit_Compactdb:
DoCmd.Hourglass False
Exit Function
Err_CompactDB:
DoCmd.Hourglass False
CompactDB = False
If Err.Number = 3356 Then
MsgBox "The database is currently being used by another User. " & _
"You can only Compact the Database if you are the only person using it." & vbCr & _
vbCr & "Please try again later.", vbExclamation, "Database in Use by Another User"
Else
MsgBox Err.Description
End If
Resume Exit_Compactdb
End Function
Here is the (not yet completed) code that is actually calling the function
Code:
Private Sub Form_Timer()
Dim LocalTime As Date
LocalTime = Format(TimeValue(Now()), "hh:mm")
'scheduled maintenance prompt
If LocalTime > #1:00:00 AM# And LocalTime < #1:15:00 AM# Then
Dim Response
Response = MsgBox("Run scheduled maintenance?" & vbCrLf & vbCrLf & _
"Selecting YES will momentarily disable the database." & vbCrLf & _
"Select NO if you have unsaved work.", vbYesNo, "Scheduled Maintenance Required")
If Response = vbYes Then
'set logout flag - logout flag is monitored by other front ends
DoCmd.SetWarnings False
DoCmd.OpenQuery "qryLogoutTrue"
DoCmd.SetWarnings True
'close frmHome - this disconnects front end from back end
DoCmd.Close acForm, "frmHome"
'display maintenance message
Me.Visible = True
CompactDB (tblHotword)
Me.Visible = False
'reset logout flag
DoCmd.SetWarnings False
DoCmd.OpenQuery "qryLogoutFalse"
DoCmd.SetWarnings True
'open frmHome
DoCmd.OpenForm "frmHome"
Else
'***WAIT 10 MINUTES AND ASK AGAIN***
End If
End If
End Sub