Results 1 to 2 of 2
  1. #1
    dumbledown is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    46

    Delete tables if the exist

    I have several tables that may or may not exist in my database (e.g. tableAPPLE, tableORANGE, tablePEAR)



    How would I write code to delete these tables if they exist?

    and for a bonus is it possible to delete text files (knowing the folder) in the same way? so if APPLE.txt and ORANGE.txt are present they are deleted, but if PEAR.txt isn't nothing happens?

    Many thanks

  2. #2
    PRMiller is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Location
    St. Paul, MN
    Posts
    16
    Quote Originally Posted by dumbledown View Post
    I have several tables that may or may not exist in my database (e.g. tableAPPLE, tableORANGE, tablePEAR)

    How would I write code to delete these tables if they exist?
    Try something like this:

    Code:
    Private Sub DeleteSomeTable()
    
        Dim dbCurrent As DAO.Database
        Dim strTable As String
        Dim tdfTable As DAO.TableDef
        
        Set dbCurrent = CurrentDb
        strTable = "SomeTableName"
        
        For Each tdfTable In dbCurrent.TableDefs
            If tdfTable.Name = strTable Then
                dbCurrent.TableDefs.Delete (strTable)
            End If
        Next
    
        Set tdf = Nothing
        Set dbCurrent = Nothing
    
    End Sub

    Quote Originally Posted by dumbledown View Post
    and for a bonus is it possible to delete text files (knowing the folder) in the same way? so if APPLE.txt and ORANGE.txt are present they are deleted, but if PEAR.txt isn't nothing happens?
    Use the FileSystemObject:

    Code:
    Private Sub TestDelete()
        
        Dim fs As Object
        Dim strFileName As String
        Dim strFilePath As String
        
        strFileName = "apple.txt"
        strFilePath = "C:\Documents and Settings\prmiller\My Documents\"
        If FileExists(strFilePath & strFileName) = False Then
            Exit Sub
        End If
        'strFileName = Dir(strFilePath)
        
        Set fs = CreateObject("Scripting.FileSystemObject")
        fs.DeleteFile strFilePath & strFileName
        Set fs = Nothing
    End Sub
    This might be sloppy (wrote it before 6am) but it should do the trick. I left the "strFileName = Dir(strFilePath)" in there but commented out to give you some ideas -- you could adapt this to check for the existence of ANY file, then perhaps write a Case statement to tell the application what to do depending on the filename.

    For the FileExists function, see Allen Browne's website at http://allenbrowne.com/func-11.html.

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

Similar Threads

  1. Macro to delete ImportErrors tables
    By lmnnt in forum Programming
    Replies: 16
    Last Post: 06-22-2011, 03:15 PM
  2. Error: Could not delete from specified tables
    By kaledev in forum Queries
    Replies: 1
    Last Post: 03-03-2011, 02:07 PM
  3. Replies: 7
    Last Post: 11-16-2009, 11:56 AM
  4. Update / delete and recreate access tables
    By metro17 in forum Access
    Replies: 4
    Last Post: 09-23-2009, 04:45 PM
  5. Could not delete from specified tables.
    By bullwinkle55423 in forum Queries
    Replies: 0
    Last Post: 08-31-2006, 08:14 AM

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