Rename a Table in an Access DatabaseVersion Compatibility: Visual Basic 5 Visual Basic 6 Instructions: Copy the declarations and code below and paste directly into your VB project.Declarations: '(None) Code:Public Function RenameTable(DatabaseName As String, _ ByVal OldTableName As String, _ ByVal NewTableName As String) As Boolean 'DataBaseName is the file/path name of the database 'OldTableName is the name of the table you want to rename 'NewTableName is new table name 'Returns true if successful, false otherwise 'Project must include reference to DAO On Error GoTo errorhandler Dim oDB As DAO.Database Dim td As DAO.TableDef Set oDB = Workspaces(0).OpenDatabase(DatabaseName) On Error GoTo errorhandler If Not TableExists(oDB, OldTableName) Then GoTo errorhandler If TableExists(oDB, NewTableName) Then GoTo errorhandler 'Create table object Set td = oDB.TableDefs(OldTableName) td.Name = NewTableName oDB.TableDefs.Refresh oDB.Close RenameTable = True Exit Function errorhandler: If Not oDB Is Nothing Then oDB.Close Set td = Nothing End Function
'(None)
Code:Public Function RenameTable(DatabaseName As String, _
ByVal OldTableName As String, _
ByVal NewTableName As String) As Boolean
'DataBaseName is the file/path name of the database
'OldTableName is the name of the table you want to rename
'NewTableName is new table name
'Returns true if successful, false otherwise
'Project must include reference to DAO
On Error GoTo errorhandler
Dim oDB As DAO.Database
Dim td As DAO.TableDef
Set oDB = Workspaces(0).OpenDatabase(DatabaseName)
On Error GoTo errorhandler
If Not TableExists(oDB, OldTableName) Then GoTo errorhandler
If TableExists(oDB, NewTableName) Then GoTo errorhandler
'Create table object
Set td = oDB.TableDefs(OldTableName)
td.Name = NewTableName
oDB.TableDefs.Refresh
oDB.Close
RenameTable = True
Exit Function
errorhandler:
If Not oDB Is Nothing Then oDB.Close
Set td = Nothing
End Function