简体   繁体   中英

VB.NET - Modifying MS Access Table Name with ADOX

I'm trying to modifying a table name based on two textbox inputs. I have one form creating the table using OleDb so I want to use the inputted information from that form to search for the table in the database then rename it to what the use wants on the current form.

Example output From AAAAAAA_000 To BBBBBBB_111

Import ADOX
Import ADODB

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnModify.Click
    Dim ADOXCatalog As New ADOX.Catalog
    Dim ADOConnection As New ADODB.Connection
    Dim uc As Char = "_"
    ADOConnection.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\Users\...\Documents\Visual Studio 2012\Projects\WindowsApplication3\WindowsApplication3\timer.mdb;" & _
    "Jet OLEDB:Engine Type=5;")

    ADOXCatalog.ActiveConnection = ADOConnection
    ADOXCatalog.Tables('" & frmOpen.TextBox1.Text & uc.ToString & frmOpen.TextBox2.Text & "').Name ='" & txtTextBox1.Text & uc.ToString & TextBox2.Text & "'

    ADOXCatalog.ActiveConnection.Close()
    ADOXCatalog.ActiveConnection = Nothing

    Me.Close()

End Sub

Note: I am assuming that the table can be renamed by setting the Name property.
However, I haven't done that

dim oldTableName as string
dim newTableName as string

oldTableName = frmOpen.TextBox1.Text & uc.ToString & frmOpen.TextBox2.Text
newTableName = txtTextBox1.Text & uc.ToString & TextBox2.Text

ADOXCatalog.Tables(oldTableName).Name = newTableName

A few points to think of

  • Change the TextBox1 , TextBox2 to appropriate name such as TablePrefix , TableSuffix .
  • You don't need uc.ToString , when uc is a string (eg const uc as String = "_" )

I found a way to modify a table name. I had to add ADOX.Table into the code with a for statement and use that to find the original table name (based on the data in the frmOpen window) with an if / then statement.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnModify.Click
    Dim ADOXCatalog As New ADOX.Catalog
    Dim ADOXTable As ADOX.Table

    Dim ADOConnection As New ADODB.Connection

    Const uc As String = "_"

    Dim oldTableName As String
    Dim newTableName As String

    oldTableName = frmOpen.TextBox1.Text & uc & frmOpen.TextBox2.Text
    newTableName = txtABINum.Text & uc & txtABIRev.Text

    ADOConnection.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\Users\028861\Documents\Visual Studio 2012\Projects\WindowsApplication3\WindowsApplication3\timer.mdb;" & _
    "Jet OLEDB:Engine Type=5;")
    Try
        ADOXCatalog.ActiveConnection = ADOConnection
        For Each ADOXTable In ADOXCatalog.Tables
            If ADOXTable.Name = oldTableName Then
                ADOXTable.Name = newTableName
            End If
        Next ADOXTable
        ADOXCatalog.ActiveConnection.Close()
        ADOXCatalog.ActiveConnection = Nothing
        Me.Close()
    Catch ex As Exception

        MsgBox(ex.Message, MsgBoxStyle.Critical, "ADOX Error")

    End Try
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM