How to rename tables in microsoft access using vba / macro?
Asked Answered
G

2

6

I have an ms access database with 24 tables. I need to rename the tables every day from

table 1 --> table 1backup

any ideas will be appreciated.

Is it doable with VBA?

Galactometer answered 22/12, 2010 at 18:41 Comment(1)
Sounds like a hideous design. Why do you think you need to do this?Scleroprotein
B
18

You can:

Dim tdf As TableDef

For Each tdf In CurrentDb.TableDefs
    If Left(tdf.Name, 4) <> "MSys" Then
      tdf.Name = tdf.Name & "_backup"
    End If
Next
Bamberg answered 22/12, 2010 at 19:1 Comment(2)
How might this be adapted to rename a single table?Boser
CurrentDb.TableDefs("mytablename").Name = "mynewtablename"Outdo
B
2

I would suggest below code. It would replace newly renamed table, with the existed table, if already has existed with the new table name:

Dim tdf As TableDef
For Each tdf In CurrentDb.TableDefs
    If Left(tdf.Name, 7) <> "backup_" Then

        Dim newTableName As String
        newTableName = "backup_" + tdf.Name

        DoCmd.SetWarnings False
        DoCmd.Rename newTableName, acTable, tdf.Name
        DoCmd.SetWarnings True
    End If

Next
Bindman answered 12/4, 2020 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.