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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

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,143,654
Messages
5,720,094
Members
422,266
Latest member
Mattyw

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