簡體   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