# Edit Table Name

**URL:** <https://forum.xojo.com/t/edit-table-name/40717>\
**Category:** Databases\
**Created:** [November 18, 2017, 2:52am UTC](https://forum.xojo.com/t/edit-table-name/40717 "2017-11-18T02:52:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Giltinan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@Mark\_Giltinan](https://forum.xojo.com/u/Mark_Giltinan)\
**Post date:** [November 18, 2017, 2:52am UTC](https://forum.xojo.com/t/edit-table-name/40717/1 "2017-11-18T02:52:47Z")

</div>

Hi Xojo Support Team

is it possible to edit the name of a Table without having to create a new table and copy the data from the old table to the new table and delete the original table name?

If there is a way it would be great to know how to do it.

Thank you

---

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [November 18, 2017, 3:09am UTC](https://forum.xojo.com/t/edit-table-name/40717/2 "2017-11-18T03:09:15Z")

</div>

Something like:

```auto
myDatabase.SQLExecute("ALTER TABLE TableName RENAME TO NewTableName;")
```

?

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [November 18, 2017, 3:11am UTC](https://forum.xojo.com/t/edit-table-name/40717/3 "2017-11-18T03:11:10Z")

</div>

Depends on the database. It’s not a Xojo question. Find the right syntax for your database and use sqlexecute like Jason points out.

---

<div class="post-metadata">

**Author:** ![Mark\_Giltinan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@Mark\_Giltinan](https://forum.xojo.com/u/Mark_Giltinan)\
**Post date:** [November 18, 2017, 3:46am UTC](https://forum.xojo.com/t/edit-table-name/40717/4 "2017-11-18T03:46:01Z")

</div>

Thanks guys. I am using an ODBC Database and this is the code I am using. Would you mind taking a look to see if this is the correct syntax…

```
Dim db As New ODBCDatabase
db.DataSource = datbasename
// oldname and newname are strings
if db.connect then
  db.sqlexecute("ALTER TABLE '" + oldname + "' RENAME TO '" + newname +"';")
end if
if db.error then
  msgbox(db.errormessage)
end if

```

Appreciate your help.

---

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [November 18, 2017, 3:49am UTC](https://forum.xojo.com/t/edit-table-name/40717/5 "2017-11-18T03:49:34Z")

</div>

As Tim said, it will depend on the type of database. ODBC is not a type of database  it’s a way of connecting to databases. It would help if you advised the type of database that you are connecting to.

My code was for SQLite. I can see a problem with your SQL string: you’ve used single quote marks around the table names. If you take a look at my sample code, these are not required (at least for SQLite).

---

<div class="post-metadata">

**Author:** ![Mark\_Giltinan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@Mark\_Giltinan](https://forum.xojo.com/u/Mark_Giltinan)\
**Post date:** [November 18, 2017, 3:51am UTC](https://forum.xojo.com/t/edit-table-name/40717/6 "2017-11-18T03:51:35Z")

</div>

Thanks Jason. I am connecting to a Microsoft Access Database \*.mdb. Does this help?

Thank you

---

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [November 18, 2017, 3:54am UTC](https://forum.xojo.com/t/edit-table-name/40717/7 "2017-11-18T03:54:33Z")

</div>

It’s been a while since I’ve used an MDB but I think my code might work.

So just replace:

```auto
db.sqlexecute("ALTER TABLE '" + oldname + "' RENAME TO '" + newname +"';")
```

with:

```auto
db.sqlexecute("ALTER TABLE " + oldname + " RENAME TO " + newname + ";")
```

Note that all I’ve done is to remove the single quotes from your SQL statement.

If it doesn’t work, it would help if you share the error message.

---

<div class="post-metadata">

**Author:** ![Mark\_Giltinan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@Mark\_Giltinan](https://forum.xojo.com/u/Mark_Giltinan)\
**Post date:** [November 18, 2017, 4:03am UTC](https://forum.xojo.com/t/edit-table-name/40717/8 "2017-11-18T04:03:54Z")

</div>

Thanks Jason

I tried that and coming up with an error message:-  
‘[Microsoft][ODBC Microsoft Access Driver] Syntax error in ALTER TABLE statement’

This is the newcode I am using

```
Dim db As New ODBCDatabase
db.DataSource = DBname
if db.connect then
  db.sqlexecute("ALTER TABLE " + oldname + " RENAME TO " + newname + ";")
end if
if db.error then
  msgbox(db.errormessage)
end if

```

Would it matter if the oldname and newname have spaces in the string?

Thank you

---

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [November 18, 2017, 4:05am UTC](https://forum.xojo.com/t/edit-table-name/40717/9 "2017-11-18T04:05:51Z")

</div>

Yep. Then you’d need to enclose the table names in square brackets, so:

```auto
db.sqlexecute("ALTER TABLE [" + oldname + "] RENAME TO [" + newname + "];")
```

---

<div class="post-metadata">

**Author:** ![Mark\_Giltinan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@Mark\_Giltinan](https://forum.xojo.com/u/Mark_Giltinan)\
**Post date:** [November 18, 2017, 4:12am UTC](https://forum.xojo.com/t/edit-table-name/40717/10 "2017-11-18T04:12:16Z")

</div>

Thanks Jason, Ive tried this and unfortunately still coming up with the same error message. Is there anything else I could be missing?

---

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [November 18, 2017, 4:21am UTC](https://forum.xojo.com/t/edit-table-name/40717/11 "2017-11-18T04:21:04Z")

</div>

Then I think you’ve got a problem :). I just did a quick search and I didn’t find a reference to the “RENAME” command being supported by Microsoft Access databases. According to [this page](https://bytes.com/topic/sql-server/answers/704018-rename-ms-access-table-using-sql-query), Access databases don’t support it. You’ll have to stick with creating a new table and then running an SQL statement to insert the data from the old table into the new one. You can do the insert in one statement.

---

<div class="post-metadata">

**Author:** ![Mark\_Giltinan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@Mark\_Giltinan](https://forum.xojo.com/u/Mark_Giltinan)\
**Post date:** [November 18, 2017, 4:28am UTC](https://forum.xojo.com/t/edit-table-name/40717/12 "2017-11-18T04:28:12Z")

</div>

Thanks Jason, I think I solved the problem with the link you included in your last message. I had to use two lines of code:-

SELECT \* INTO newtablename FROM oldTableName  
DROP TABLE oldTableName

Thanks for your help with this.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:59pm UTC](https://forum.xojo.com/t/edit-table-name/40717/13 "2020-10-29T18:59:49Z")

</div>


