rename table name

accessman2

Registered User.
Local time
Yesterday, 17:30
Joined
Sep 15, 2005
Messages
335
In MS Access , form,

how can write the VB for rename the table name?

If I created the table named "app"

then I made a button for rename the table to "aaa"

How can I write?
Thanks.
 
The following function will do what you require. Note the references you need to perform it.

Code:
Public Function RenameTable(ByVal TableName As String, _
    ByVal NewName As String) As Boolean

    ' NOTES: Function requires the following references:
    '
    ' 1) Microsoft ActiveX Data Objects 2.x Library
    ' 2) Microsoft ADO Ext. 2.x for DDL and Security

    On Error GoTo Err_RenameTable

    Dim cnn As ADODB.Connection ' ADODB.Connection object
    Dim cat As ADOX.Catalog     ' ADOX.Catalog object
    Dim tbl As ADOX.Table       ' ADOX.Table object
    
    Set cnn = CurrentProject.Connection ' Assign current database to connection object
    Set cat = New ADOX.Catalog          ' Create a new instance of the catalog object
    Set cat.ActiveConnection = cnn      ' Assign connection object to catalog object
    Set tbl = cat.Tables(TableName)     ' Assign table object to relevant table
    
    tbl.Name = NewName ' Assign new name to table
    
    RenameTable = True ' Function successful, table renamed
    
Exit_RenameTable:

    ' Release object variables from memory
    Set tbl = Nothing   ' Release ADOX.Table object from memory
    Set cat = Nothing   ' Release ADOX.Catalog object from memory
    Set cnn = Nothing   ' Release ADODB.Connection object from memory
    
    Exit Function ' RenameTable
    
Err_RenameTable:

    RenameTable = False ' Function failed, table not renamed
    
    Resume Exit_RenameTable

End Function ' Rename Table
 

Users who are viewing this thread

Back
Top Bottom