0

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
4

2 回答 2

0

注意:我假设可以通过设置Name属性来重命名表。
但是,我没有这样做

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

几点思考

  • TextBox1将,更改TextBox2为适当的名称,例如 TablePrefix, TableSuffix
  • 你不需要uc.ToString, whenuc 是一个字符串(例如const uc as String = "_"
于 2013-04-03T21:37:52.870 回答
0

我找到了一种修改表名的方法。我必须ADOX.Table在代码中添加一条语句,并使用/for语句来查找原始表名(基于 frmOpen 窗口中的数据)。ifthen

    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
于 2013-04-04T05:16:23.497 回答