使用rs.UpdateBatch(MS SQL)

时间:2016-01-06 15:07:46

标签: excel vba excel-vba

尝试运行以下代码时出现“查询过于复杂”的VBA错误,而且似乎无法找出原因?

Public Sub Update9MonthsDB(ByVal colSelection As Collection)

Dim rs As ADODB.Recordset
Dim strSQL As String

Call Open_Conn(strDb)

Set rs = New ADODB.Recordset

rs.ActiveConnection = CON
rs.CursorType = adOpenKeyset
rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic
rs.Open ("Select * From [PortfolioDB$]")

For Each Item In colSelection

    strSQL = "Grid_Ref='" & Item & "'"

    rs.Find strSQL
    Debug.Print rs!Grid_Ref
    rs("MonthCheck9") = "1"
    Debug.Print rs!MonthCheck9

Next Item

rs.UpdateBatch
rs.Close

Call Close_Conn

End Sub

我的连接字符串在代码的其他地方使用,并且正常用于正常的更新/选择等语句。我的调试打印显示该集合不为空,并返回正确的值与另一个调试值检查。但是一旦它到达rs.UpdateBatch,它就会崩溃到“查询太复杂”的错误。

编辑:只是为了澄清抱歉,这是更新作为数据库的excel电子表格(后端)

编辑:评论中要求的下面的连接字符串。

Sub Open_Conn(strDb As String)
On Error GoTo conError
    Debug.Print (Time() & " - Connecting to: " & strDb)
    Set CON = New ADODB.Connection
    CON.Provider = "Microsoft.ACE.OLEDB.12.0"
    CON.Open "Data Source=" & strDb & ";" & _
    "Extended Properties=""Excel 12.0 XML;HDR=Yes"""
    Debug.Print (Time() & " - Connected")
    Exit Sub
conError:
    logevents ("There was an error processing the connection: " & strSQL & _        
    vbNewLine & vbTab & vbTab & vbTab & vbTab & Error(Err))
    Resume Next
End Sub

0 个答案:

没有答案