【问题标题】:Excel macro displays Type Mismatch error after upgrade to Office365升级到 Office 365 后 Excel 宏显示类型不匹配错误
【发布时间】:2022-01-18 00:20:40
【问题描述】:

我的宏在装有 Windows 10 和 Excel 2010 的笔记本电脑上运行良好。它也曾经在装有 Windows 10 和 Excel 2010 的台式机上运行良好。一旦我的桌面升级到 Office 365,宏就会引发类型不匹配错误。

以下是出现错误的代码:intSteps = rs.RecordCount

以下是正在运行的模块。我想知道升级到 Office 365 时连接字符串是否需要更改

Application.ScreenUpdating = False
Application.EnableEvents = False
    
Dim strQuery As String
strQuery = ActiveSheet.Name
    
ActiveSheet.Unprotect
    
Columns("M:AA").Select
Selection.ClearContents

Rows("10:1000").Select
Selection.ClearContents

Dim i As Integer
Dim r As Long
Dim c As Integer
Dim x As Integer
Dim intSteps As Integer
    
Dim strSQL As String
Dim rs As ADODB.Recordset
Dim adoSQL As ADODB.Connection
Set adoSQL = New ADODB.Connection
    
adoSQL.Provider = "SQLOLEDB.1"
adoSQL.ConnectionString = "DATABASE=MainDB;SERVER=appsrv12.www.mysite.com;UID=User1;PWD=PW12;"
adoSQL.CursorLocation = adUseClient
adoSQL.Open
    
strStartDate = Range("dtStart").Value
strEndDate = Range("dtEnd").Value

strSQL = "SELECT * FROM tbl_Events WHERE DateTime >= '" & strStartDate & "' AND DateTime < '" & strEndDate & "' AND description like '% " & strQuery & "%' ORDER BY DateTime DESC"
    
Set rs = adoSQL.Execute(strSQL)

If rs.RecordCount > 0 Then
   r = 32
   x = rs.Fields.Count
       For c = 1 To x
           Range(Chr(Asc("B") + c - 1) & r).Value = rs.Fields(c - 1).Name
       Next c
        
       Range("B33").CopyFromRecordset rs
        
End If

intSteps = 0
i = 1
    
Dim n
Dim bolUseRecipe As Boolean
    bolUseRecipe = False
    
For Each n In ActiveSheet.Names 'loop though all the named ranges
    If Right(ActiveSheet.Names(i).Name, 8) = "RecipeID" Then
         If Range("RecipeID").Value > 0 Then
            bolUseRecipe = True
            strSQL = "SELECT * FROM tbl_Recipe WHERE RecipeID = " & Range("RecipeID").Value & " ORDER BY StepNum"
                    
Set rs = adoSQL.Execute(strSQL)
                    
If rs.RecordCount > 0 Then
   rs.MoveFirst
   intSteps = rs.RecordCount      '' LINE THAT THROWS THE ERROR
                       
   ReDim arrRecipe(1 To intSteps)
                        
   For i = 1 To rs.RecordCount
       arrRecipe(i).StartTemp = rs.Fields("StartTemp")
       arrRecipe(i).Hours = rs.Fields("Duration")
                        
       rs.MoveNext
                            
       DoEvents
                            
  Next i
  End If
  End If
    
  Exit For
  End If
  i = i + 1
  Next

提前感谢您的帮助或任何解决我问题的建议。我觉得这是我所缺少的基本内容,但目前不确定它是什么。再次感谢...

【问题讨论】:

    标签: excel vba office365 connection-string type-mismatch


    【解决方案1】:

    尝试使用以下解决方法:

    rst = SomeRecordset
    dim rstCount as Long
    rstCount = CLng(rst.RecordCount)
    

    更多信息请参见'Type Mismatch' Error on ADODB.Recordset

    【讨论】:

    • .......谢谢你的帮助............我会看看那个链接......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2013-03-14
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多