将RecordSet设置为存储过程结果。无法初始化尝试对数据提供程序中的结果进行排序/筛选

时间:2016-06-08 19:41:00

标签: vba sql-server-2008 ms-access-2013

我和我的团队支持大型MS访问应用程序网络。最近我们将Microsoft Office从2010年升级到2013年。此升级中唯一突破的是我们能够对从ADO RecordSet填充的数据表进行排序/过滤。试图过滤导致错误

  

无法初始化数据提供者

从我的研究中我了解到微软认为ADO已被弃用,并且已停止支持ADO作为填充RecordSets的手段。

然而,我是在一个特定的绑定中并且一直在尝试每一个"修复"我发现为了让这个数据表可以过滤。我对所有建议持开放态度。

我最接近的是通过存储过程填充数据表:



 Dim cn As New ADODB.connection
   
   Dim cm As New ADODB.Command
   
  
   Set cn = New ADODB.connection
      With cn
         .Provider = "Microsoft.Access.OLEDB.10.0"
         .Properties("Data Provider") = "SQLNCLI10"
         .Properties("Data Source") = "ascsql2012sbox"
         .Properties("Integrated Security") = "SSPI"
         .Properties("Initial Catalog") = "assetQuality_dev"
      End With
   
   cn.Open
  
   Dim rs As ADODB.recordset
   Set rs = New ADODB.recordset
   With cm
        .ActiveConnection = cn
        .CommandText = "dbo.accountIDproc"
        .CommandType = adCmdStoredProc
        .parameters.Refresh
        End With
        With rs
        .ActiveConnection = cn
        .CursorType = adOpenForwardOnly
        .CursorLocation = adUseClient
        End With
        
    Set rs = cm.Execute
    
    cm.ActiveConnection = Nothing
    
    Set Me.recordset = rs
    
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    




但同样的错误

  

无法初始化数据提供者

每当我尝试从A-Z或Z-A中对列进行排序时,

仍然会出现。对于某些包含整数值的列。它要求我输入一个值,然后告诉我"这不是一个有效的值"当它绝对有效时。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

我最终用@parfiat

建议的非常可靠的解决方案来回答我自己的问题

我创建了一个临时表,然后从记录集中插入值。然后我将临时表设置为我的数据表中提取的DAO.recordset。这是我的编码解决方案:



  
    Set rs = New ADODB.recordset
    Set cn2 = New ADODB.connection
    With cn2
       .Provider = "Microsoft.Access.OLEDB.10.0"
       .Properties("Data Provider") = "SQLNCLI10"
       .Properties("Data Source") = "ascsql2012sbox"
       .Properties("Integrated Security") = "SSPI"
       .Properties("Initial Catalog") = "assetQuality_dev"
    End With
    cn2.Open
    cn2.CommandTimeout = 0
    rs.CursorLocation = adUseClient
    rs.LockType = adLockOptimistic
    rs.CursorType = adOpenKeyset
    rs.Source = "SELECT " & sqlSelect & " FROM " & sqlFrom & IIf(sqlWhere <> "", " WHERE " & sqlWhere, "") & IIf(sqlOrder <> "", " ORDER BY " & sqlOrder, "")
    Set rs.ActiveConnection = cn2
    rs.Open
      
    If AccessTableExists("ThisTable") = True Then
    DoCmd.DeleteObject acTable = acDefault, "ThisTable"
    End If
    
    ' Create temp table
    Dim dbs As Database
  
    ' on your computer
    Set dbs = CurrentDb
 
    ' Create a table with two text fields.
    dbs.Execute "CREATE TABLE ThisTable " _
    & "(accountID int, customerName CHAR, phoneHome VARCHAR(25), taxID VARCHAR(9), address VARCHAR(200), addressState VARCHAR(2), addressCity VARCHAR(2), bank VARCHAR(35), dateReview VARCHAR(200), orderDateReview VARCHAR(100), dateDue VARCHAR(100), balCustomer VARCHAR(200), activityCode VARCHAR(100), dateActivity VARCHAR(100), accountNumber VARCHAR(25));"
   
    dbs.Close
     
    Dim dbsTemp As DAO.Database
    Dim rstemp As DAO.recordset
 
    Set dbsTemp = CurrentDb
    Set rstemp = dbsTemp.OpenRecordset("ThisTable", dbOpenDynaset)
    
    Do While Not rs.EOF
    rstemp.AddNew
    rstemp!accountID.value = rs!accountID.value
    rstemp!customerName.value = rs!customerName.value
    rstemp!balCustomer.value = rs!balCustomer.value
    rstemp!accountNumber.value = rs!accountNumber.value
    rstemp!dateDue.value = rs!dateDue.value
    rstemp!taxID.value = rs!taxID.value
    rstemp!phoneHome.value = rs!phoneHome.value
    rstemp!dateReview.value = rs!dateReview.value
    rstemp!activityCode.value = rs!activityCode.value
    rstemp!dateActivity.value = rs!dateActivity.value
    rstemp.update
    rs.MoveNext
Loop
    Set Me.recordset = rstemp
    rs.Close
    Set rs = Nothing
    cn2.Close
    Set cn2 = Nothing
&#13;
&#13;
&#13;