简体   繁体   中英

CopyFromRecordset Returning Strange Characters

I have the following code below:

Function downloadsqltoexcel(conn As ADODB.Connection, sSQL As String, exceldestinationrangename As String, sqltablename As String, bDownload As Boolean, Optional ws As Worksheet) As Variant

'================================================================================================================================
'== Procedure Name: downloadsqltoexcel
'== Purpose: downloads SQL table data (or query data) to Excel named range or grabs a specific value from an SQL table
'== Notes: ensure that SQL table or query name and Excel named range are identical
'================================================================================================================================

Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset

Dim DestinationRange As Range

With rsPubs

    .ActiveConnection = conn
    .Open sSQL, conn, adOpenStatic, adLockReadOnly, adCmdText

    If bDownload Then 'if download switch is on, dump into Excel named range

        If ws Is Nothing Then
            Set DestinationRange = Range(exceldestinationrangename)
        Else
            Set DestinationRange = ws.Range(exceldestinationrangename)
        End If

        With DestinationRange
            .ClearContents
            .CopyFromRecordset rsPubs
        End With

   .... more code follows, but not relevant

The code itself executes well. However, the .CopyFromRecordset rsPubs line returns very strange character data when I point to newly created Prod SQLServer database, which was copied directly from QA database in SQLServer as well.. When I say very strange, I mean like blank spaces mixed with Japanese style characters or some font set that I don't even recognize.

rsPubs returns exact record count as expected, so I know i am getting results I want. Also confirmed that data is written into SQLServer DB as needed.

Any ideas how to fix it so values return as expected from call to Prod SQLServer DB?

I faced a similar problem too and I strongly suspect the number of records returned in your RecordSet is quite large. Therefore, CopyFromRecordSet is not working.

I would suggest using the follwoing approach:

  1. Populating a 2-D Array in VBA with the RecordSet data.

    Dim arr as Variant arr=rs.GetRows() rs.close conn.close

  2. Transpose the Array (Since this results into an inverted Array)

    arr=Application.WorksheetFunction.Transpose(arr)

  3. Once, the Array is transposed, you can write the Array to the worksheet Range.

    testWS.Range("A1").Resize(UBound(arr, 1) + 1, UBound(arr, 2)

This worked for me. Hope that works for you well!

Happy coding!

I ended up looping through the recordset and placing each value one-by-one. That said, the array answer provided probably will work faster for large data sets. At the time, this fit my needs and worked.

If bDownload Then 'if download switch is on, dump into Excel named range

            If ws Is Nothing Then
                Set DestinationRange = Range(exceldestinationrangename)
            Else
                Set DestinationRange = ws.Range(exceldestinationrangename)
            End If

            r = 0

            DestinationRange.ClearContents
            '.CopyFromRecordset rsPubs 'this is breaking on PDLOBList download (last column not showing up) 'PAQPT-488

            If .RecordCount > 0 Then
                .MoveFirst
                Do Until .EOF
                    For c = 0 To .Fields.Count - 1
                        DestinationRange.Offset(r, c).value = .Fields(c)
                    Next
                    .MoveNext
                    r = r + 1
                Loop

(I left out the rest of the if blocks as they are not relevant to this answer.)

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