Results 1 to 6 of 6

Thread: {Resolved} Fastest VB way to load Excel from .mdb

  1. #1

    Thread Starter
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    {Resolved} Fastest VB way to load Excel from .mdb

    Hey Guys,

    I have the following routine to load Table data (of an .mdb) into a pre-defined Excel template.

    However, it is VERY slow, is there a quicker way?

    VB Code:
    1. Do While Not rst.EOF
    2.  
    3.         lngRowCntr = lngRowCntr + 1
    4.  
    5.         With objExcel
    6.             .range("A" & lngRowCntr) = Format(rst.Fields("Date").Value & "", "dd/mmm/yyyy")
    7.             .range("B" & lngRowCntr) = rst.Fields("Type").Value & ""
    8.             .range("C" & lngRowCntr) = rst.Fields("Registration").Value & ""
    9.             .range("D" & lngRowCntr) = rst.Fields("PIC").Value & ""
    10.             .range("E" & lngRowCntr) = rst.Fields("Crew").Value & ""
    11.             .range("F" & lngRowCntr) = rst.Fields("Details").Value & ""
    12.             .range("G" & lngRowCntr) = rst.Fields("Landings").Value & ""
    13.             .range("H" & lngRowCntr) = rst.Fields("SE ICUS Day").Value & ""
    14.             .range("I" & lngRowCntr) = rst.Fields("SE ICUS Night").Value & ""
    15.             .range("J" & lngRowCntr) = rst.Fields("SE Dual Day").Value & ""
    16.             .range("K" & lngRowCntr) = rst.Fields("SE Dual Night").Value & ""
    17.             .range("L" & lngRowCntr) = rst.Fields("SE Command Day").Value & ""
    18.             .range("M" & lngRowCntr) = rst.Fields("SE Command Night").Value & ""
    19.             .range("N" & lngRowCntr) = rst.Fields("ME ICUS Day").Value & ""
    20.             .range("O" & lngRowCntr) = rst.Fields("ME ICUS Night").Value & ""
    21.             .range("P" & lngRowCntr) = rst.Fields("ME Dual Day").Value & ""
    22.             .range("Q" & lngRowCntr) = rst.Fields("ME Dual Night").Value & ""
    23.             .range("R" & lngRowCntr) = rst.Fields("ME Command Day").Value & ""
    24.             .range("S" & lngRowCntr) = rst.Fields("ME Command Night").Value & ""
    25.             .range("T" & lngRowCntr) = rst.Fields("ME Co-Pilot Day").Value & ""
    26.             .range("U" & lngRowCntr) = rst.Fields("ME Co-Pilot Night").Value & ""
    27.             .range("V" & lngRowCntr) = rst.Fields("Instrument Flight").Value & ""
    28.             .range("W" & lngRowCntr) = rst.Fields("Instrument Ground").Value & ""
    29.         End With
    30.  
    31.         rst.MoveNext
    32.  
    33.     Loop


    Note: The Excel Workbook is allready set up and pre-saved (lotsa reasons - multi page and the like)so I cant simply dump the Table into Excel.




    Cheers,
    Bruce.
    Last edited by Bruce Fox; Feb 16th, 2004 at 01:12 AM.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Have you tried the Cells method of the Range object? This would avoid the conversion of lngRowCntr into a string and the concatenation of the two strings when specifying the cell.

    I have not tested the code and have no idea if it would be faster.

    VB Code:
    1. Do While Not rst.EOF
    2.  
    3.         lngRowCntr = lngRowCntr + 1
    4.  
    5.         With objExcel.Range
    6.              .Cells(1,lngRowcntr) = Format(rst.Fields("Date").Value & "", "dd/mmm/yyyy")
    7.  
    8.             .Cells(2,lngRowCntr) = rst.Fields("Type").Value & ""
    9.             '...
    10.         End With
    11.  
    12.         rst.MoveNext
    13.  
    14.     Loop

    I wonder if a Paste operation would be faster. A GetRows on the Recordset will load everything into an array. Copy the array to the Clipboard and then paste it into the Excel spreadsheet.
    Last edited by brucevde; Feb 15th, 2004 at 02:15 AM.

  3. #3

    Thread Starter
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Thanks for the reply Bruce.

    I'll give those methods a go




    Bruce.

  4. #4

    Thread Starter
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Copying the RecordSet to the Clipboard, and manualy pasting in Excel is super quick


    However, when I automate the 'paste', I get "Out of Memory"!

    VB Code:
    1. Clipboard.Clear
    2. Clipboard.SetText rst.GetString
    3. objExcel.Range("A2").Select
    4. objExcel.Range("A2") = Clipboard.GetText
    (Note: I can Ctl+V when "A2" is selected sucessfully in Excel)

    Hmmmm.



    Bruce.

  5. #5

    Thread Starter
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Ok, the problem is Clipboard.GetText fails with large data (Ctl+V still works tho in Excel).

    I guess I now need to try using SendMessage to Paste....


    Edit: Actually, PasteSpecial method may work, now to get home...


    Bruce.
    Last edited by Bruce Fox; Feb 15th, 2004 at 05:25 PM.

  6. #6

    Thread Starter
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    The PasteSpecial method is the fast; very fast (I tested with 16,000):
    VB Code:
    1. With objExcel
    2.         .Range("A2").Select
    3.         .Range("A2").PasteSpecial
    4.         .Range("$X:$X").Clear   'Clear out the (unwanted - hidden) Tables AutoNumbering
    5.         .Visible = True
    6.         .ActiveWindow.SelectedSheets.PrintPreview   'Show once completed loading data
    7.     End With

    Also, for others here is an alternative (using CopyFromRecordset (Ecell 2000+).




    Bruce

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