【问题标题】:Excel VBA query recordset value from SQL SP Type Mismatch error来自 SQL SP 类型不匹配错误的 Excel VBA 查询记录集值
【发布时间】:2017-01-03 05:20:58
【问题描述】:

使用 SQL Server 2012 \ Excel 2010。Excel 调用 SQL SP,它将返回 1 或 2,然后我需要基于此执行其他操作。

Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.RecordSet
Dim WSP1 As Worksheet

Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.RecordSet

' Log into our SQL Server, and run the Stored Procedure
con.Open "Provider=SQLOLEDB;Data Source=xxxxxxxx;Initial Catalog=xxxxxxx;Integrated Security=SSPI;Trusted_Connection=Yes;"
cmd.ActiveConnection = con

' Set up the parameter for our Stored Procedure
' (Parameter types can be adVarChar,adDate,adInteger)
cmd.Parameters.Append cmd.CreateParameter("User", adVarChar, adParamInput, 15, Trim(Range("C7").Text))

Application.StatusBar = "Running stored procedure..."

cmd.CommandText = "usp_GL_Code_Access"

Set rs = cmd.Execute(, , adCmdStoredProc)

If rs = 1 Then

MsgBox "true"

Else

MsgBox "false"

rs.Close
Set rs = Nothing
Set cmd = Nothing

con.Close
Set con = Nothing

目前我只是使用 msgbox true\false 来查看来自 rs\SQL SP 的值,但我不断收到错误消息“类型不匹配”并且它突出显示了

IF rs = 1 Then

线。

有什么想法吗?

【问题讨论】:

    标签: sql-server vba excel


    【解决方案1】:

    rs 是一个记录集对象,将其与1 进行比较是无效的,它不是过程返回的值。

    如果 1 或 2 是来自 SELECT 的值,则读取单个值:

    if rs.eof then
       msgbox "no rows"
    else
       result = rs.collect(0)
       msgbox result
    end if
    

    【讨论】:

    • Excel 真的会看到 1 或 2 吗?我需要继续 SP 并说 if 1 then do this else do something else
    【解决方案2】:

    我认为这样做会更好。

    该函数使用 ADO 将 SQL Server 数据插入目标 Excel 范围。

    Function ImportSQLtoRange(ByVal conString As String, ByVal query As String, _
        ByVal target As Range) As Integer
    
        On Error Resume Next
    
        ' Object type and CreateObject function are used instead of ADODB.Connection,
        ' ADODB.Command for late binding without reference to
        ' Microsoft ActiveX Data Objects 2.x Library
    
        ' ADO API Reference
        ' http://msdn.microsoft.com/en-us/library/ms678086(v=VS.85).aspx
    
        ' Dim con As ADODB.Connection
        Dim con As Object
        Set con = CreateObject("ADODB.Connection")
    
        con.ConnectionString = conString
    
        ' Dim cmd As ADODB.Command
        Dim cmd As Object
        Set cmd = CreateObject("ADODB.Command")
    
        cmd.CommandText = query
        cmd.CommandType = 1         ' adCmdText
    
        ' The Open method doesn't actually establish a connection to the server
        ' until a Recordset is opened on the Connection object
        con.Open
        cmd.ActiveConnection = con
    
        ' Dim rst As ADODB.Recordset
        Dim rst As Object
        Set rst = cmd.Execute
    
        If rst Is Nothing Then
            con.Close
            Set con = Nothing
    
            ImportSQLtoRange = 1
            Exit Function
        End If
    
        Dim ws As Worksheet
        Dim col As Integer
    
        Set ws = target.Worksheet
    
        ' Column Names
        For col = 0 To rst.Fields.Count - 1
            ws.Cells(target.row, target.Column + col).Value = rst.Fields(col).Name
        Next
        ws.Range(ws.Cells(target.row, target.Column), _
            ws.Cells(target.row, target.Column + rst.Fields.Count)).Font.Bold = True
    
        ' Data from Recordset
        ws.Cells(target.row + 1, target.Column).CopyFromRecordset rst
    
        rst.Close
        con.Close
    
        Set rst = Nothing
        Set cmd = Nothing
        Set con = Nothing
    
        ImportSQLtoRange = 0
    
    End Function
    

    代码cmets:

    The query parameter can contain a SELECT or EXECUTE query.
    The resulting data will be inserted starting from the top left cell of the target range.
    Using Object types and the CreateObject function instead of direct use of ADO types
    lets to avoid setting ActiveX Data Objects 2.x Library references on user computers.
    This code works in Microsoft Excel 2003-2016.
    Always use Set Nothing statements for ADODB.Connection and ADODB.Recordset objects to free resources.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 2014-01-05
      • 2019-08-01
      • 2016-12-19
      相关资源
      最近更新 更多