Results 1 to 3 of 3
  1. #1
    shubhamgandhi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Jul 2011
    Posts
    22

    How do you overwrite a table created in VBA? [DELETE]

    I'm creating a table in VBA within a loop and when I run the code a table is created. But the next time I run it, an error comes up telling me that the table exists and the remainder of the code is not executed. How can I have it overwrite the existing table (from the previous run)?

    Here is my code:



    Code:
    Option Compare Database
    
    Public Function createTables()
    
        Dim db As DAO.Database
        Dim tdf As DAO.TableDef
        Dim rst As DAO.Recordset
        Dim fld As DAO.Field
        Dim strSQL As String
    
        strSQL = "Select SKUS from SKUS"
        Set db = CurrentDb()
        Set rst = db.OpenRecordset(strSQL)
        Set fld = rst.Fields("SKUS")
    
        'MsgBox fld.Value
    
        rst.MoveFirst
        Do While Not rst.EOF
            Set tdf = db.CreateTableDef(fld.Value)
    
            Set fld = tdf.CreateField("SKUS", dbText, 30)
            tdf.Fields.Append fld
    
            Set fld = tdf.CreateField("Count", dbInteger)
            tdf.Fields.Append fld
    
            db.TableDefs.Append tdf
    
            rst.MoveNext
        Loop
    
    End Function
    Can anyone help me out please? Thanks!

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    you'll have to drop the existing table first if you want to overwrite it, or alternately perform a delete (data) query then repopulate the existing table.

    In your code it would be

    db.execute ("DROP TABLE <tablename>")

  3. #3
    shubhamgandhi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Jul 2011
    Posts
    22
    Sorry, bad question, this wasn't even what is causing problem in my code. I created a new thread...

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

Similar Threads

  1. Replies: 11
    Last Post: 03-30-2011, 01:08 PM
  2. Replies: 1
    Last Post: 09-21-2010, 09:49 AM
  3. Delete Table on Report_Close
    By nhabegger in forum Programming
    Replies: 6
    Last Post: 07-20-2010, 09:32 PM
  4. Table Overwrite
    By ml2009 in forum Access
    Replies: 3
    Last Post: 01-14-2010, 07:06 AM
  5. Importing a table created in word to Access
    By anthony_f_justin in forum Access
    Replies: 4
    Last Post: 01-04-2006, 09:06 AM

Tags for this Thread

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