Call to .CopyFromRecordSet corrupts formatting in a different worksheet

ijourneaux

New Member
Joined
Jul 9, 2018
Messages
30
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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

CSmith

Well-known Member
Joined
Jan 13, 2020
Messages
686
Office Version
  1. 365
  2. 2010
  3. 2007
Platform
  1. Windows
  2. Mobile
  3. Web
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?
 

ijourneaux

New Member
Joined
Jul 9, 2018
Messages
30
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
 

Forum statistics

Threads
1,141,049
Messages
5,703,942
Members
421,321
Latest member
blusky4

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
Top