未设置对象变量或带块变量

问题描述 投票:0回答:1

我有这个 VBA 代码,它可以根据位置生成扫描文档的报告。该代码曾经在其他机器上工作,但无法正常工作,并给出错误“未设置对象变量或块变量”。我尝试向 ObjRS 提供该值,但它不起作用。代码看起来不错。

这是我的代码:

Option Explicit

Function Main_Func(ByVal strIniFilePath As String, ByVal strDate As String, ByVal strServer As String)
     Dim objConn As ADODB.Connection 'Object

     Dim strDocumentURL As String
     Dim strDocFolderURL As String
     Dim strRet As String
     Dim intCount As Integer
     'Read M Document URL from INI File
     intCount = IniGetStringDef(strIniFilePath, "M URL", "DocumentURL", strDocumentURL)
     'Read M Document Folder URL from INI File
     intCount = IniGetStringDef(strIniFilePath, "M URL", "DocumentFolderURL", strDocFolderURL)
    
    If strIniFilePath <> vbNullString Then intCount = IniGetStringDef(strIniFilePath, "Database", "ConnectionString", strRet)
       
       Dim strConStr As Variant
       strConStr = Split(strRet, ";")
    
    strRet = ""
       
    Dim i As Integer
    For i = 0 To UBound(strConStr)
    
    If InStr(strConStr(i), "word=") > 0 Then
       strRet = strRet + "Password=" + EncryptDecrypt(Mid(strConStr(i), 10)) + ";"
     Else
    strRet = strRet + strConStr(i) + ";"
    End If
       Next

  If strRet <> vbNullString Then
        Set objConn = New ADODB.Connection
        objConn.CursorLocation = adUseClient
        objConn.Open strRet
        
        Dim strRangeEnd As String
        Dim objRange As Range
        Dim objCmd As ADODB.Command
        Dim objRS As ADODB.Recordset
        Set objCmd = New ADODB.Command
        With objCmd
           .CommandText = "sp_ScanStatReport"
           .CommandType = adCmdStoredProc
           .Parameters.Append .CreateParameter("@p_scan_date", adVarChar, adParamInput, 10, strDate)
            Set .ActiveConnection = objConn
            Set objRS = .Execute
         End With
           
                LoadSheetData objRS, ScanConstant.STAT_SUCCESS, strServer, strServer
                LoadSheetData objRS, ScanConstant.STAT_SENT, "SENT", strServer
                LoadSheetData objRS, ScanConstant.STAT_SCAN, "SCAN", strServer
                LoadSheetData objRS, ScanConstant.STAT_FAIL, "ERRORS", strServer
                LoadFinalData objRS, ScanConstant.STAT_SUCCESS, strServer + " Imaging Station", strDocumentURL, strDocFolderURL, strDate, strServer
                objRS.Close

      End If
          
Exit_Function:

    If CBool(objRS.State And adStateOpen) Then 'i am getting error here
        objRS.Close
        Set objRS = Nothing
      End If
    If Not objConn Is Nothing Then
        If CBool(objConn.State And adStateOpen) Then objConn.Close
        Set objConn = Nothing
    End If
    Exit Function

ErrorHandler:
    Err.Raise Err.Number, Err.Source, Err.Description
End Function
excel vba adodb
1个回答
0
投票

如果 strRet = vbNullString 则 objRS 永远不会被声明或初始化。也许可以在 IF 语句中添加 ELSE 部分并处理所有分支可能性。尝试 strConStr 是零长度字符串 ("") 的场景。然后 split 返回一个空数组,即没有元素也没有数据的数组。 strRet 仍然 = "" 并且当 "If CBool(objRS.State..." 被击中时你会得到错误。

© www.soinside.com 2019 - 2024. All rights reserved.