Results 1 to 6 of 6

Thread: Need better ways to copy all data incl. fieldname from recordset to excel.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    341

    Need better ways to copy all data incl. fieldname from recordset to excel.

    I am using the following code right now to copy the data to an excel sheet:

    Code:
    rs.Open "SELECT * FROM " & sDBTableName, cn, adOpenKeyset, adLockOptimistic, adCmdText
    sDataArray = rs.GetRows   
     
    For Each f In rs.Fields
            i = i + 1
            oRange.cells(1, i).Value = f.Name
    Next
    
    For i = 0 To UBound(sDataArray, 2)    'iterates through all records
            For k = 0 To UBound(sDataArray, 1)    'iterates through each field
                oRange.cells(i + 2, k + 1).Value = sDataArray(k, i)
            Next
    Next
    I guess there might be better ( as in shorter, faster and more reliable) ways to do this?

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Need better ways to copy all data incl. fieldname from recordset to excel.

    Take a Look at the ArrFillFromTopLeft Helperfunctions here:
    http://www.vbforums.com/showthread.p...=1#post4615193

    Olaf

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    341

    Re: Need better ways to copy all data incl. fieldname from recordset to excel.

    Thank you Olaf, I will dig into that thread soon. Seems to be a long one to read ...

    BTW. Today I'm really having troubles finding the equivalent to "Open File For Append" in vbRichclient ... So far, I can only see related methods:

    WriteTextContent - Tested and it seems to rewrite the entire file.
    Another problem related with cFSO is that, I can't find any equivalent to "fso.CopyFolder". There is only *copy file* functions in cFSO. And yes, I know I can use cDirList to get a list of filenames and then copy them wherever I like but please forgive me, I want some *shorter* approaches from vbRichClient...
    Last edited by bPrice; Feb 3rd, 2015 at 04:20 AM.

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440

    Re: Need better ways to copy all data incl. fieldname from recordset to excel.

    Eh? Why not just use the "CopyFromRecordset"-Method?

    https://msdn.microsoft.com/en-us/lib...ice.11%29.aspx
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Need better ways to copy all data incl. fieldname from recordset to excel.

    Quote Originally Posted by Zvoni View Post
    Eh? Why not just use the "CopyFromRecordset"-Method?

    https://msdn.microsoft.com/en-us/lib...ice.11%29.aspx
    Yep, worth a try - though this method behaved a bit "funny" (on older Excel- and/or ADO-versions)...

    Rs.GetRows allows a bit more influence (since you can go over certain columns again pretty fast -
    with regards to formatting and stuff - and it is also slightly faster IIRC than CopyFromRecordset.

    Olaf

  6. #6
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Need better ways to copy all data incl. fieldname from recordset to excel.

    Quote Originally Posted by bPrice View Post
    Thank you Olaf, I will dig into that thread soon. Seems to be a long one to read ...
    The Helperfunctions I mean, are in my post #13.

    Quote Originally Posted by bPrice View Post
    BTW. Today I'm really having troubles finding the equivalent to "Open File For Append" in vbRichclient ...
    Well, there's the Stream-Object you can use for that (e.g. when used in conjunction with
    a StringBuilder as intermediate InMemory-Buffer):

    Code:
    Private Sub AppendToFile(SB As cStringBuilder, FileName As String)
      With New_c.FSO.OpenFileStream(FileName, STRM_WRITE Or STRM_SHARE_DENY_NONE)
        .SetPosition 0, STRM_SeekFromEnd '<- moves the StreamPointer to the end 
        .WriteFromByteArr SB.ToUTF8 '<- appends the current content of the StringBuilder
      End With
    End Sub
    Here's the whole concept in context with some Parent-Child-XML-writing:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Dim SB As cStringBuilder
      Set SB = New_c.StringBuilder
      
      'gather stuff first in an intermediate InMemory-Buffer
      SerializeParent SB
      
      'from time to time, append the SB.Content to a File
      AppendToFile SB, "c:\temp\test.txt"
      
      SB.Clear 'clear the StringBuilder-Buffer (since the Data is now safe in a File)
      
      '... pass the StringBuilder around for more data-gathering
    End Sub
    
    Private Sub SerializeParent(SB As cStringBuilder)
    Dim Children As cArrayList 'just some DemoData, the simulated Parent knows about
    Set Children = New_c.ArrayList(vbString, "Child1", "Child2", "Child3")
      
      SB.Append "<Parent"
        SB.AppendXMLAttribute "ChildCount", Children.Count: SB.AppendNL ">"
          SerializeChildren SB, Children
      SB.AppendNL "</Parent>"
    End Sub
    
    Private Sub SerializeChildren(SB As cStringBuilder, Children As cArrayList)
      Dim i As Long
      For i = 0 To Children.Count - 1
        SB.Append "  <Child>"
          SB.AppendXMLNodeContent Children(i)
        SB.AppendNL "</Child>"
      Next i
    End Sub
    
    Private Sub AppendToFile(SB As cStringBuilder, FileName As String)
      With New_c.FSO.OpenFileStream(FileName, STRM_WRITE Or STRM_SHARE_DENY_NONE)
        .SetPosition 0, STRM_SeekFromEnd '<- moves the StreamPointer to the end 
        .WriteFromByteArr SB.ToUTF8 '<- appends the current content of the StringBuilder
      End With
    End Sub
    Quote Originally Posted by bPrice View Post
    Another problem related with cFSO is that, I can't find any equivalent to "fso.CopyFolder". There is only *copy file* functions in cFSO. And yes, I know I can use cDirList to get a list of filenames and then copy them wherever I like but please forgive me, I want some *shorter* approaches from vbRichClient...
    The cFSO.MultiFilexxx functions can do that job ... e.g. to copy all *.csv files from c:\temp1\ into c:\temp2\ you do:

    Code:
      New_c.FSO.MultiFileCopy "c:\temp1\*.csv", "c:\temp2\"
    Olaf
    Last edited by Schmidt; Feb 3rd, 2015 at 07:23 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width