2017-10-06 107 views
0

我正在使用Excel 2010 Workboox,其中宏从数据库表中提取数据。然后用户可以根据需要将Column06的值更新为特定值。完成后,他们可以运行一个宏来运行SQL更新,以便更新数据库中的Column06,其中COLUMN01和COLUMN02位于数据库表中。我知道ADO连接正在工作,因为我尝试了一个非常通用的SQL,它工作正常。我知道桌子的长度可能有所不同,所以我知道我可能需要循环行,这就是我卡住的地方。VBA使用ListColumns将Excel从SQL更新到MS SQL Server 2008

我试着设置一个类似于我在网上找到的另一个解决方案的循环,并开始获得运行时错误91“对象变量或块变量未设置”。我认为是由于我在新的Update Statement中使用的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 

回答

0

也许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) 
    ' ... ... ... 

+0

JonRo,在我将列常量减1后, Col_Nbr__Coumn06 As Long = 5.现在我只需要弄清楚如何抛出“如果不是空白,然后退出for循环,我应该很好去。 – drkevorkiian

+0

找出我最后的挑战,它比我更容易它。我不得不修改代码略微 CONST ColNbr_Column01只要= 0 CONST ColNbr_Column02只要= 1 CONST ColNbr_Column06只要= 5 '现在,仅选择范围[tbl_data] 的第一列的每一行在范围内(_ [tbl_data] .Cells(1,1).Address,_ [tbl_data] .Cells([tbl_data] .Rows.Count,1).Address) 如果row.Offset(0,Column01)。 Value =“”Then Then 'now,use offset to reach to the columns in the row – drkevorkiian

0

这是基本概念。

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