【问题标题】:VBA Update sql from Excel to MS SQL Server 2008 using ListColumnsVBA 使用 ListColumns 将 sql 从 Excel 更新到 MS SQL Server 2008
【发布时间】:2017-10-06 22:41:54
【问题描述】:

我正在开发 Excel 2010 Workboox,其中宏从数据库表中提取数据。然后,如果需要,用户可以将 Column06) 的值更新为特定值。完成后,他们可以运行一个宏来运行 SQL 更新,以便在数据库表中 COLUMN01 和 COLUMN02 所在的位置更新数据库中的 Column06。我知道 ADO 连接正在工作,因为我尝试了一个非常通用的 sql,它运行良好。我知道表格可能有不同的长度,所以我知道我可能需要遍历行,这就是我卡住的地方。

我尝试设置一个类似于我在网上找到的另一个解决方案的循环,并开始收到运行时错误 91“对象变量或未设置块变量”。我认为是由于我在新的更新语句中使用的 ListObject.ListColumns。我尝试使用其他示例来声明这些,但通常会导致其他错误。我一定是错过了什么,或者做错了什么。任何帮助将不胜感激。

Sub Updatetbl_data()
'
' Updatetbl_data Macro
' test
Sheets("Sheet2").Select
Dim cnn As ADODB.Connection
Dim uSQL As String

Set cnn = New Connection
    cnnstr = "Provider=SQLOLEDB; " & _  
    "Data Source=MySource; " & _  
    "Initial Catalog=MyDB;" & _
    "User ID=ID;" & _
    "Password=Pass;" & _
    "Trusted_Connection=No"
cnn.Open cnnstr
' New Update Statement idea based on possible solution found online
Dim row As Range
For Each row In [tbl_data].Rows
uSQL = "UPDATE tbl_data SET Column06 = '" & (row.Columns  (row.ListObject.ListColumns("Column06").Index).Value) & _
"' WHERE Column01 = '" & (row.Columns(row.ListObject.ListColumns ("Column01").Index).Value) & _
"' AND Column02 = '" & (row.Columns(row.ListObject.ListColumns("Column02").Index).Value) & "' "
'Debug.Print (uSQL)
cnn.Execute uSQL
Next

cnn.Close
Set cnn = Nothing
Exit Sub

'
End Sub

【问题讨论】:

    标签: vba excel sql-update ado


    【解决方案1】:

    也许row.Columns 不是为您想要实现的目标而设计的。你可以给this link to another article on stackoverflow 看看更多信息。接下来,我对您的代码进行了一些更改,这可能会奏效。

    
        ' ... ... ...
        Dim row As Range
        'For Each row In [tbl_data].Rows ==>> has to be replaced by
        'For Each row In [tbl_data] ==>> which returns all cells, perhaps better the following
        Const ColNbr_Column01 As Long = 1
        Const ColNbr_Column02 As Long = 2
        Const ColNbr_Column06 As Long = 6
        ' now, select only the first column of the range [tbl_data]
        For Each row In Range( _
                            [tbl_data].Cells(1, 1).Address, _
                            [tbl_data].Cells([tbl_data].Rows.Count, 1).Address)
    
        ' now, use offset to reach to the columns in the row
        uSQL = "UPDATE tbl_data SET Column06 = '" & row.Offset(0, ColNbr_Column06).Value & _
        "' WHERE Column01 = '" & row.Offset(0, ColNbr_Column01).Value & _
        "' AND Column02 = '" & row.Offset(0, ColNbr_Column02).Value & "' "
        'Debug.Print (uSQL)
        ' ... ... ...
    
    

    【讨论】:

    • JonRo,在我将 Column 常量每个减少 1 之后,它就起作用了,例如Col_Nbr__Coumn06 As Long = 5。现在我只需要弄清楚如何输入“IF 非空白,然后退出 for 循环,我应该可以开始了。
    • 解决了我的最后一个挑战,这比我做的要容易。我只需要稍微修改代码 Const ColNbr_Column01 As Long = 0 Const ColNbr_Column02 As Long = 1 Const ColNbr_Column06 As Long = 5 ' 现在,仅选择范围 [tbl_data] For Each row In Range( _ [tbl_data] .Cells(1, 1).Address, _ [tbl_data].Cells([tbl_data].Rows.Count, 1).Address) If row.Offset(0, Column01).Value = "" Then Exit For ' now,使用偏移量到达行中的列
    【解决方案2】:

    这是基本概念。

    Sub InsertInto()
    
    'Declare some variables
    Dim cnn As adodb.Connection
    Dim cmd As adodb.Command
    Dim strSQL As String
    
    'Create a new Connection object
    Set cnn = New adodb.Connection
    
    'Set the connection string
    cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Database;Data Source=Server_Name"
    
    
    
    'Create a new Command object
    Set cmd = New adodb.Command
    
    'Open the Connection to the database
    cnn.Open
    
    'Associate the command with the connection
    cmd.ActiveConnection = cnn
    
    'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
    cmd.CommandType = adCmdText
    
    'Create the SQL
    strSQL = "UPDATE TBL SET JOIN_DT = '2017-10-08' WHERE EMPID = 1"
    
    'Pass the SQL to the Command object
    cmd.CommandText = strSQL
    
    
    'Execute the bit of SQL to update the database
    cmd.Execute
    
    'Close the connection again
    cnn.Close
    
    'Remove the objects
    Set cmd = Nothing
    Set cnn = Nothing
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2013-06-12
      • 1970-01-01
      • 2014-04-15
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多