Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create VP.Net code to get the names of all of the tables in an Access database and change the names of each table by adding “tbl” to the beginning of each table. Can this be done? Any help would be greatly appreciated.

What I have tried:

This is my start, but I am at a loss.

Imports System.Data.Oledb

Sub ChangeTableNames()
Dim varCountNumTables As Integer
Dim dt as DataTable

VarCountNumTables = dt.Tables.count
Dt.Tables(0).TableName = “NewTableName”

End Sub
Posted
Updated 14-Jan-20 20:18pm
v2

First of all, a data table does not contain tables. If you're talking about a data set then you must use a DataSet Class (System.Data) | Microsoft Docs[^]

You also wrote about an Access table so if these tables need to be renamed inside an Access database, you can use DoCmd.Rename method (Access) | Microsoft Docs[^]
 
Share this answer
 
To read the table names from Access is simple: issue an SQL command:
SQL
SELECT Name FROM MSysObjects WHERE (Name Not Like "MSys*") AND (Type In (1,4,6))  ORDER BY Name
and it will return them.
But ... Access doesn't support table renaming via the ALTER TABLE SQL command, so you would have to create a new table with the new name and the same structure as the old, copy all the rows over and then delete the original.Since this sounds very much like a "one of" job, I'd do it manually in Access - it'll be a load easier, and probably both more reliable and quicker!
 
Share this answer
 
Comments
Richard MacCutchan 15-Jan-20 4:20am    
I just tried that and got an exception saying no read permission on MSysObjects. Any ideas?
OriginalGriff 15-Jan-20 4:35am    
I don't have either Access engine installed ATM so I can't test, but it's a permissions problem:
https://stackoverflow.com/questions/1937703/record-cannot-be-read-no-read-permission-on-msysobjects

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900