简体   繁体   中英

CopyFromRecordset Fails in Server 2012, but not in Windows 7, or Windows 10 in VBA/Excel

I have an Excel document that uses ADO to connect to a BASIS database, and uses SQL to grab data. The data is put into a recordset and then displayed on a Worksheet using CopyFromRecordset.

This process works fine in Excel on Windows 7, and Windows 10, however, when I run the same spreadsheet on our Terminal Server running Windows Server 2012 R2 I get the error Method 'CopyFromRecordset' of object 'Range' failed. The error number is -2147417848.

I have read many posts that claim the problem is with too much data, too many columns, or too many rows, but I have modified my query to bring in 1 field, in 1 record of data, also, the query with all the data works fine on Windows 7 and 10.

When I bring in information using connection.OpenSchema into a Recordset and display that data on the worksheet there are no problems, but when I use resultset.open or connection.execute to bring in field data I get the error every time.

Also, when I use functions such as movefirst, and movenext in the openschema recordset there is no problem, but when I use these functions on the field data recordset Excel crashes completely and gives me these problem details.

Problem signature: Problem Event Name: APPCRASH Application Name: EXCEL.EXE Application Version: 16.0.4690.1000 Application Timestamp: 5acd084f Fault Module Name: BBjODBC.dll Fault Module Version: 16.2.4.0 Fault Module Timestamp: 58d03d38 Exception Code: c0000005 Exception Offset: 0000000000179cc1 OS Version: 6.3.9600.2.0.0.16.7 Locale ID: 1033

The relevant parts of the code that I am using are as follows.

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset

    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset

    cn.ConnectionString = "DSN=ODBC_RIC;UID=RIC;"
    cn.Open

    cmdText = "select * from prospectmaster"

    'I tried all these open combinations
    'rs.Open cmdText, cn, adOpenDynamic, adLockReadOnly
    'rs.Open cmdText, cn, adOpenForwardOnly, adLockOptimistic
    'rs.Open cmdText, cn, adOpenDynamic, adLockPessimistic
    'rs.Open cmdText, cn, adOpenDynamic, adLockOptimistic
    'rs.Open cmdText, cn, adOpenDynamic, adLockUnspecified
    'rs.Open cmdText, cn, adOpenUnspecified, adLockUnspecified

    'I settled on this one that works everywhere else    
    Set rs = cn.Execute(cmdText)

    ' Display Headers (works Fine)
    For n = 0 To rs.Fields.Count - 1
        Range("A6").Offset(0, n).Value = rs.Fields(n).Name
    Next n

   ' Display the data (does not work fine)
    Range("A7").CopyFromRecordset rs

    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing

I am at a loss as to what to try next. Any help would be appreciated.

I found out that the problem was that my odbc driver was 32 bit, and only worked with 32-bit versions of Excel. The computer I was testing it on was 64-bit. I installed the proper version of Office and it worked. Alternatively, I could have installed the correct driver and it would work that way too.

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