Call to .CopyFromRecordSet corrupts formatting in a different worksheet

ijourneaux

New Member
Joined
Jul 9, 2018
Messages
36
I have queried a SQL database to get a recodset
VBA Code:
    cnn.Open ConnectionString
    cnn.CommandTimeout = 900
    rst.Open StrQuery, cnn
My orignal attempt to write the data to a worksheet was
VBA Code:
   ThisWorkbook.Worksheets("RMS Data").Range("A2").CopyFromRecordset rst
   fldCount = rst.Fields.Count
   rst.MoveFirst
   recArray = rst.GetRows

When the command .CopyFromRecordset is executed, the cell formatting a some cells in another worksheet is unexpectedly changed from Number to Date.

VBA Code:
   fldCount = rst.Fields.Count
    Set rngDst = ThisWorkbook.Worksheets("RMS Data").Range("A2")

    With rst
        If Not .BOF And Not .EOF Then
            .MoveFirst
            While Not .EOF
                For i = 0 To .Fields.Count - 1
                    rngDst.Offset(, i) = .Fields(i).value
                Next i
                rst.MoveNext
                Set rngDst = rngDst.Offset(1)
            Wend
        End If
    End With

Works without corrupting the other worksheet. Anyone have any thoughts on what might be happening when using .CopyFromRecordset?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Example?
When the command .CopyFromRecordset is executed, the cell formatting a some cells in another worksheet is unexpectedly changed from Number to Date.

If the above works, why not just fix the formatting?
 
Upvote 0
Unfortunately everytime this line is executed, the formating get changed. FOr now I moved away from CopyFromRecordSet but this is a very particular error.
VBA Code:
   ThisWorkbook.Worksheets("RMS Data").Range("A2").CopyFromRecordset rst
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top