Results 1 to 10 of 10
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095

    Table object not deleting "consistently"


    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

  2. #2
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    564
    If the structure remains constant, then don't drop / delete the table, just delete all the records in it.

    Instead of that whole DoCmd.DeleteObject ... stuff, why not just basically truncate the table?

    Code:
    CurrentDB.Execute "DELETE * FROM [" & sTableName "];", dbFailOnError
    then you don't need to recreate anything.

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095
    Yes, like I wrote in my OP.
    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.
    That is what I've done.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Perhaps add a DoEvents?, as I initially thought your create code had not worked, as I could not see the new table.

    However, creating the table once and then deleting seems better to me, though I know that will create some bloat. Shame MS did not include Foxpro's Zap command into Access when they bought it. The just nicked the fast method that Foxpro had. It was called Rushmore.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095
    As I posted in #3, I've abandoned the delete and re-create of the table, instead simply emptying the permanent table and filling anew as needed.

    Notice within the code posted in #1 that I trim the tblLog to 100 records. WHAT YOU'RE NOT SEEING is the full coarse of processing where once the session completes its logging that I append import the Log.txt file into the table, trim to 100 records and export the updated table back to disc. With each successive session, I always end up with a 100 record Log written to disc. Each session typically logs about 20 to 25 events.
    Code:
    If intRecCount > 100 Then
    rsLog.MoveLast
        For I = 1 To intRecCount - 100
            rsLog.Delete
            rsLog.MoveLast
        Next I
    End If

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    You still have to consider bloat?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095
    You're a lot smarter than I am, so I must be missing something that you're seeing. Where's the bloat going to come from? Each session trims the log to 100 records before updating the Log.txt file and each session isn't going to ultimately create more than probably 35 or 40 log records. (Bloat to me means a DB is growing in size to a point of unacceptability in many regards, i.e., size, performance, etc.)

  8. #8
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    564
    Bloat. So what. Run Compact & Repair every month or so. (or when the database doesn't have a lot of records but the database size seems way larger than it should be)

  9. #9
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095
    Automation apps running at 2AM each day on a client computer need to be given every consideration given the assumption that no client hands-on will be available, so potential bloating needs to be given its due. In this case, any significant bloating has the potential of adding complications to the automated DB backups.

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    What about using a Temp or Side database?
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 12-11-2024, 09:56 AM
  2. Replies: 5
    Last Post: 05-07-2020, 06:58 AM
  3. PrintOut not consistently printing to default tray
    By TheKillerMonkey in forum Access
    Replies: 5
    Last Post: 04-07-2017, 11:41 AM
  4. Replies: 3
    Last Post: 08-29-2014, 10:57 AM
  5. Replies: 6
    Last Post: 08-16-2011, 12:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums