繁体   English   中英

Excel 宏在升级到 Office365 后显示类型不匹配错误

[英]Excel macro displays Type Mismatch error after upgrade to Office365

My macro works fine on a laptop with Windows 10 and Excel 2010. It also used to work fine on my desktop with Windows 10 and Excel 2010. Once my desktop got upgraded to Office 365, the macro throws a Type MisMatch error.

以下是出现错误的代码: 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

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

尝试使用以下解决方法:

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

有关详细信息,请参阅ADODB.Recordset 上的“类型不匹配”错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM