【问题标题】:How To ALTER New Columns In Table by Items from ListBox如何通过列表框中的项目更改表中的新列
【发布时间】:2020-02-16 09:34:32
【问题描述】:

我的数据库在 MS Access 2010 中完美运行,代码可以更改表中的新字段,所有这些都来自 ListBox 由下面的用户代码从另一个表填充的项目:

Sub AlterTableX()
   Dim intLoop As Integer
   Dim strSQL As String
   Dim strSQL1 As String
   For intLoop = 0 To Me.field_row.ListCount - 1
   strSQL = "ALTER TABLE Emp_Sal ADD " & _
   Me.field_row.Column(0, [intLoop]) & "  LONG INTEGER"
   CurrentProject.Connection.Execute strSQL
   strSQL1 = "ALTER TABLE tbl_General ADD " & _
   Me.field_row.Column(0, [intLoop]) & "  LONG INTEGER"
   CurrentProject.Connection.Execute strSQL1
   Next intLoop
'  MsgBox "Success Adding Columns", vbOKOnly, "Success"
End Sub

好吧,现在我想将该 VBA 代码转换为 VB.NET 2012

我需要你的帮助。

【问题讨论】:

  • 第一句话没看懂。请修正语法。我不明白你的问题到底是什么。
  • 欢迎来到本站!所以您想在VB.NET 中而不是直接在Access 中发出这些alter table 命令?你需要一个oledbcommand 对象!
  • 我想更改两个名为 Emp_sal 和 tbl_General 的表,其中包含由用户作为列填充的列表框 field_row 中的所有项目
  • 你试过什么?显示您的代码。有什么错误吗?
  • 您将不得不学习 ADO.net。广泛的主题。

标签: vba vb.net multiple-columns alter


【解决方案1】:

如果您只是想在 VB.NET 中执行相同的命令,请尝试类似的操作。它应该可以帮助您入门,但最好按照 cmets 中的建议阅读 ADO.NET

    Using SqlCMD As New OleDb.OleDbCommand()
        SqlCMD.Connection = CurrentProject.Connection
        Try
            SqlCMD.Connection.Open()
            For intLoop As Integer = 0 To Me.field_row.ListCount - 1
                SqlCMD.CommandText = "ALTER TABLE Emp_Sal ADD " & Me.field_row.Column(0, [intLoop]) & "  LONG INTEGER"
                SqlCMD.ExecuteNonQuery()
                SqlCMD.CommandText = "ALTER TABLE tbl_General ADD " & Me.field_row.Column(0, [intLoop]) & "  LONG INTEGER"
                SqlCMD.ExecuteNonQuery()
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "ERROR ALTERING TABLE")
        Finally
            SqlCMD.Connection.Close()
        End Try
    End Using

【讨论】:

    猜你喜欢
    • 2017-07-21
    • 2022-01-14
    • 2016-06-09
    • 2013-07-06
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 2011-07-01
    相关资源
    最近更新 更多