Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename A Table

Status
Not open for further replies.

Hackster

Programmer
Mar 28, 2001
173
0
0
US
I need to rename a table in an Access database (this is an import database that gets shipped to us once a week, and for reasons too lenghty to go into, I have to rename one of the tables before I can process that data in it.) Here is what I have:
Code:
Dim ADOCn As ADODB.Connection
Dim ConnString As String
Dim adoRS As ADODB.Recordset
Dim sSQL As String

ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=c:\DBName.mdb;" & _
        "Persist Security Info=False"

Set ADOCn = New ADODB.Connection
ADOCn.ConnectionString = ConnString
ADOCn.Open ConnString
sSQL = "ALTER TABLE table_name RENAME 'table_name_new'"
ADOCn.Execute sSQL
I'm getting a syntax error, the description for which is anything but helpful. How do you programmatically rename a table in a DB?
 
What is the error message. I know you say it isnt helpful but it may help????
 
I am not saying that you can't do it somehow like that but I've never seen it.

You could do a

SELECT * INTO table_name_new FROM table_name

and then

DROP TABLE table_name

 
Here is also a snippet of code that will do the rename job:

Code:
Private Sub Form_Load()
    Dim ADOcn As Object
    Set ADOcn = CreateObject("ADODB.Connection")
    ADOcn.provider = "Microsoft.Jet.OLEDB.4.0"
    ADOcn.open "C:\db.mdb"
    
    RenameTable ADOcn, "Table2", "Table2_New"
End Sub

Public Sub RenameTable(ADOcn As Object, OldName As String, NewName As String)
    With CreateObject("ADOX.Catalog")
        .activeConnection = ADOcn
        .tables(OldName).Name = NewName
    End With
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top