简体   繁体   中英

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.

Following is the code where the error appears: intSteps = rs.RecordCount

Following is the module that is running. I'm wondering if the connection string needs to change with the upgrade to 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

Thanks in advance for your help or any suggestion to resolve my issue. I feel like it is something basic that I'm missing but not sure what it is at this time. Thanks again...

Try to use the following workaround:

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

See 'Type Mismatch' Error on ADODB.Recordset for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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