In the InitLog function within the general module below, the "DoCmd.DeleteObject" executes successfully, but the "CREATE TABLE" statement that follows fails because the table still exists. If I Reset and close the DB and then open the DB and re-run to cursor the TextLog Sub everything works as expected. Now, as it is, I could solve the issue if I simply emptied the table, keeping it permanently in the DB, I wouldn't have to delete and re-create at all, which I'll do in any event here but still want to know what the problem is.
Code:
Option Compare Database
Option Explicit
Dim rsLog As DAO.Recordset
Public Sub TestLog()
Dim I As Integer
Dim intRecCount As Integer
On Error GoTo ErrorHandler
If InitLog("tblLog") <> 0 Then
MsgBox "tblLog failed to initialize."
Exit Sub
End If
WriteLog ("EventLog") 'put the field name as first record.
For I = 1 To 10 'Write some test records
WriteLog ("Test Line Number " & I)
Next I
CloseLog 'Close the DAO Recordset
'Now add the old log history to what we have now.
DoCmd.TransferText TableName:="tblLog", Filename:="c:\SDMA\PDFActivities\Log.txt", HasFieldNames:=True
'Open the DAO Rececordset again
Set rsLog = DBEngine(0)(0).OpenRecordset("tblLog")
intRecCount = rsLog.RecordCount
If intRecCount > 100 Then
rsLog.MoveLast
For I = 1 To intRecCount - 100
rsLog.Delete
rsLog.MoveLast
Next I
End If
CloseLog
NormalExit: Exit Sub
ErrorHandler: MsgBox "Testing error " & Err.Number & " " & Err.Description
GoTo NormalExit
End Sub
Public Function InitLog(tblName As String) As Integer
'*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
' Create the new table and bind it to the DAO Recordset rsLog. The logging action
' will record the actions into the DAO Recordset.
'*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
On Error Resume Next
DoCmd.DeleteObject acTable, tblName 'Get rid of any old log table
On Error GoTo ErrorHandler
CurrentDb.Execute "CREATE TABLE " & tblName & " (EventLog Text)" 'Create a new log table
Set rsLog = DBEngine(0)(0).OpenRecordset(tblName) 'Bind new log to DAO REcordset
InitLog = 0
NormalExit: Exit Function
ErrorHandler: InitLog = Err.Number
MsgBox "Initialization error " & Err.Number & " " & Err.Description
GoTo NormalExit
End Function
Public Function WriteLog(strEvent As String)
'*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
' Add event to the log
'*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
rsLog.AddNew
rsLog!EventLog = strEvent
rsLog.Update
End Function
Public Function CloseLog()
'*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
' Close the Recordset and append the log's history file.
'*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
rsLog.Close
Set rsLog = Nothing
End Function