Results 1 to 22 of 22

Thread: [RESOLVED] Porting ADO Recordset To Excel

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Resolved [RESOLVED] Porting ADO Recordset To Excel

    This is related to my other post about the Automation error, but as it is a completely different question, so I'm posting it in a new thread.

    I have an ADO recordset created in VB6. Some of the elements of this recordset is header information, and will only be used once in the, doh, header portion of the spreadsheet.

    I have that working just fine. However, there are 8 other elements in the recordset the comprise the sole of the data I'm retrieveing. Like all recordset, I have no idea how many records will be returned during any particular run as the criteria for the SELECT query is user selectable.

    My column headers for the data begin at A6 and go to F6. I want the loop that iterates through the recordset to start dumping the contents at A8 through F8 and continue down to Ax where x is whatever column will be the last one.

    What I don't know how to do is increment the row number so that it will go from A6 to Ax. How would I do that?

    (Note: I'm using Excel 2003 and VB6 SP5)
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Porting ADO Recordset To Excel

    Are you using Excels .CopyFromRecordset method? It has a parameter for where to place the data.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Porting ADO Recordset To Excel

    Quote Originally Posted by RobDog888
    Are you using Excels .CopyFromRecordset method? It has a parameter for where to place the data.
    No. CopyFromRecordSet doesn't work for me. Reference this Office Development thread.

    That is why I am switching from using CopyFromRecordset to actual code to prepare my report based on my recordset.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Porting ADO Recordset To Excel

    Quote Originally Posted by SeanK
    This is related to my other post about the Automation error, but as it is a completely different question, so I'm posting it in a new thread.

    I have an ADO recordset created in VB6. Some of the elements of this recordset is header information, and will only be used once in the, doh, header portion of the spreadsheet.

    I have that working just fine. However, there are 8 other elements in the recordset the comprise the sole of the data I'm retrieveing. Like all recordset, I have no idea how many records will be returned during any particular run as the criteria for the SELECT query is user selectable.

    My column headers for the data begin at A6 and go to F6. I want the loop that iterates through the recordset to start dumping the contents at A8 through F8 and continue down to Ax where x is whatever column will be the last one.

    What I don't know how to do is increment the row number so that it will go from A6 to Ax. How would I do that?

    (Note: I'm using Excel 2003 and VB6 SP5)
    Moderators are a nasty lot with big clubs and mean looks and they use them if you bump your thread, so I'll bump this for you.

    My motive is not totally pure, however, as I would like to know how to do this as well.

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Porting ADO Recordset To Excel

    This is how I would approach it.
    Looping through the records and rows at the same time.
    Then within that loop, loop through the fields and columns at the same time.

    Here's some sample code to give you the idea.
    VB Code:
    1. Sub sample()
    2. Dim rngRowStart As Range
    3. Dim rsYourRecordSet As ADODB.Recordset
    4. Dim x As Integer
    5.  
    6.     'Starting position for the excel table
    7.     Set rngRowStart = ThisWorkbook.Worksheets("SheetName").Range("A6")
    8.    
    9.     With rsYourRecordSet
    10.        
    11.         'Start of recordset
    12.         .MoveFirst
    13.         Do
    14.             '8 elements in the RS
    15.             For x = 0 To 7
    16.                 'Place each element in the coresponding column
    17.                 rngRowStart.Offset(0, x).Value = .Fields(x).Value
    18.             Next x
    19.        
    20.             'Next Record
    21.             .MoveNext
    22.            
    23.             'Next Row
    24.             Set rngRowStart = rngRowStart.Offset(1, 0)
    25.        
    26.         Loop Until .EOF
    27.     End With
    28. End Sub
    Last edited by DKenny; May 4th, 2006 at 04:34 PM. Reason: spelling errors
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Porting ADO Recordset To Excel

    Thanks DKenny, but I'm getting a "Method or Data Member Not Found" on
    VB Code:
    1. rngRowStart.Offset blah
    With ".Offset" being highlighted.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Porting ADO Recordset To Excel

    Sean

    Can you run the code to the error point and in the immediate window check the value of rngRowStart.Address.

    My though here is that the issue is with the range variable. Did you change "SheetName" in the above code to the name of your desyination worksheet?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Porting ADO Recordset To Excel

    I will step through it and check the value, but I don't know the name of the worksheet. I'm guessing it will be Sheet1. Here is how I'm creating it
    VB Code:
    1. Set objExcel = New Excel.Application
    2.     Set bkWorkBook = objExcel.Workbooks.Add
    3.     Set shWorkSheet = bkWorkBook.ActiveSheet
    Prior to running this code, the excel file/worksheet doesn't exist.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Porting ADO Recordset To Excel

    In that case don't use a name at all. You can refer to the sheet by its index in the worksheets collection.

    Changing your last posted code to...
    VB Code:
    1. Set objExcel = New Excel.Application
    2.     Set bkWorkBook = objExcel.Workbooks.Add
    3.     Set shWorkSheet = bkWorkBook.Worksheets(1)
    4.     Set rngRowStart = shWorkSheet.Range("A6")
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Porting ADO Recordset To Excel

    Quote Originally Posted by DKenny
    In that case don't use a name at all. You can refer to the sheet by its index in the worksheets collection.

    Changing your last posted code to...
    VB Code:
    1. Set objExcel = New Excel.Application
    2.     Set bkWorkBook = objExcel.Workbooks.Add
    3.     Set shWorkSheet = bkWorkBook.Worksheets(1)
    4.     Set rngRowStart = shWorkSheet.Range("A6")
    Ok. I did this, and I'm still getting the "Method or Data Member Not Found" on .Offset
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Porting ADO Recordset To Excel

    Can you post the entire procedure? Or at least the line that is giving the error.
    Last edited by DKenny; May 5th, 2006 at 09:54 AM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  12. #12

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Porting ADO Recordset To Excel

    This is everything minus the acutal query that creates the recordset, which is working just fine.
    VB Code:
    1. Set objExcel = New Excel.Application
    2.     Set bkWorkBook = objExcel.Workbooks.Add
    3.     Set shWorkSheet = bkWorkBook.Worksheets(1)
    4.     'Set shWorkSheet = bkWorkBook.ActiveSheet
    5.         shWorkSheet.Range("A1") = "Company Name:  " & adoRS(4) 'ordinal position for provider name
    6.         shWorkSheet.Range("A2") = "Company Number:  " & adoRS(5) 'ordinal position for provider code
    7.         shWorkSheet.Range("C1") = "FYE:  " & cboFYE.Text
    8.         shWorkSheet.Range("A3") = "Type:  " & cboType.Text
    9.         shWorkSheet.Range("C3") = "Number " & pstrOrderNumber
    10.         shWorkSheet.Range("A6") = "Item No"
    11.         shWorkSheet.Range("A6") = "Item"
    12.         shWorkSheet.Range("B6") = "Processor"
    13.         shWorkSheet.Range("C6") = "Disposition Type"
    14.         shWorkSheet.Range("D6") = "Unit Price"
    15.         shWorkSheet.Range("E6") = "Total Price"
    16.         shWorkSheet.Range("F6") = "Comments"
    17. 'Do While Not adoRS.EOF
    18. Dim rngRowStart As Range
    19.  
    20. Dim x As Integer
    21.  
    22.     Set rngRowStart = shWorkSheet.Range("A6")
    23.     'Starting position for the excel table
    24.     'Set rngRowStart = bkWorkBook.Worksheets("Sheet1").Range("A6")
    25.    
    26.     'With adoRS
    27.        
    28.         'Start of recordset
    29.         adoRS.MoveFirst
    30.         Do
    31.             '8 elements in the RS
    32.             For x = 0 To 7
    33.                 'Place each element in the coresponding column
    34.                 rngRowStart[HL="#FFFF80"].Offset[/hl](0, x).Value = adoRS(5) '<===this is highlighted when I get the error message.
    35.                 rngRowStart.Offset(0, x).Value = adoRS(2)
    36.                 rngRowStart.Offset(0, x).Value = adoRS(9)
    37.                 rngRowStart.Offset(0, x).Value = adoRS(11)
    38.                 rngRowStart.Offset(0, x).Value = adoRS(3)
    39.                 rngRowStart.Offset(0, x).Value = adoRS(4)
    40.                 rngRowStart.Offset(0, x).Value = adoRS(10)
    41.             Next x
    42.        
    43.             'Next Record
    44.             adoRS.MoveNext
    45.            
    46.             'Next Row
    47.             Set rngRowStart = rngRowStart.Offset(1, 0)
    48.        
    49.         Loop Until .EOF
    50.   '  End With
    51.  
    52.        
    53.         shWorkSheet.Columns("A:BZ").AutoFit
    54.         objExcel.Visible = True
    55. adoRS.Close
    56. Set adoRS = Nothing
    57.  
    58.  
    59. End Sub
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  13. #13
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Porting ADO Recordset To Excel

    the are a couple of issues here.

    In your code your requirement is to put non-sequential fields from the recordset into sequential columns on the spreadsheet. The For x Next loop will not achieve this. Remove the loop and specify each mapping individually.

    VB Code:
    1. For x = 0 To 7
    2.                 'Place each element in the coresponding column
    3.                 rngRowStart.Offset(0, x).Value = adoRS(5) '<===this is highlighted when I get the error message.
    4.                 rngRowStart.Offset(0, x).Value = adoRS(2)
    5.                 rngRowStart.Offset(0, x).Value = adoRS(9)
    6.                 rngRowStart.Offset(0, x).Value = adoRS(11)
    7.                 rngRowStart.Offset(0, x).Value = adoRS(3)
    8.                 rngRowStart.Offset(0, x).Value = adoRS(4)
    9.                 rngRowStart.Offset(0, x).Value = adoRS(10)
    10.             Next x
    should become
    VB Code:
    1. 'Place each element in the coresponding column
    2.             rngRowStart.Offset(0, 0).Value = adoRS(5)
    3.             rngRowStart.Offset(0, 1).Value = adoRS(2)
    4.             rngRowStart.Offset(0, 2).Value = adoRS(9)
    5.             rngRowStart.Offset(0, 3).Value = adoRS(11)
    6.             rngRowStart.Offset(0, 4).Value = adoRS(3)
    7.             rngRowStart.Offset(0, 5).Value = adoRS(4)
    8.             rngRowStart.Offset(0, 6).Value = adoRS(10)

    Next, I checked the offset and it is working correctly, therefore the issue has to be with the recordset object. ( I suspect that this is also the reason that the CopyFromRecordset method was failing)

    Here's the code I used to check the offset, basically I removed all references to the recordset and instead put the field numbers in the correct columns and changed the DO..LOOP to a FOR..NEXT - just to show it working on multiple lines.
    VB Code:
    1. Sub sample()
    2. Dim objExcel As Excel.Application
    3. Dim bkWorkBook As Excel.Workbook
    4. Dim shWorkSheet As Excel.Worksheet
    5. Dim rngRowStart As Excel.Range
    6. Dim counter As Long
    7.  
    8.     Set objExcel = New Excel.Application
    9.     Set bkWorkBook = objExcel.Workbooks.Add
    10.     Set shWorkSheet = bkWorkBook.Worksheets(1)
    11.    
    12.     shWorkSheet.Range("A6") = "Item No"
    13.     shWorkSheet.Range("A6") = "Item"
    14.     shWorkSheet.Range("B6") = "Processor"
    15.     shWorkSheet.Range("C6") = "Disposition Type"
    16.     shWorkSheet.Range("D6") = "Unit Price"
    17.     shWorkSheet.Range("E6") = "Total Price"
    18.     shWorkSheet.Range("F6") = "Comments"
    19.  
    20.     Set rngRowStart = shWorkSheet.Range("A7")
    21.            
    22.     counter = 1
    23.     Do
    24.         'Place each element in the coresponding column
    25.         rngRowStart.Offset(0, 0).Value = 5
    26.         rngRowStart.Offset(0, 1).Value = 2
    27.         rngRowStart.Offset(0, 2).Value = 9
    28.         rngRowStart.Offset(0, 3).Value = 11
    29.         rngRowStart.Offset(0, 4).Value = 3
    30.         rngRowStart.Offset(0, 5).Value = 4
    31.         rngRowStart.Offset(0, 6).Value = 10
    32.        
    33.         'Next Row
    34.         Set rngRowStart = rngRowStart.Offset(1, 0)
    35.        
    36.         counter = counter + 1
    37.     Loop Until counter = 10
    38.        
    39.     shWorkSheet.Columns("A:BZ").AutoFit
    40.     objExcel.Visible = True
    41. End Sub

    Net net of all this is that the issue has to be with the recordset object.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  14. #14

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Porting ADO Recordset To Excel

    Ok, here is something wierd.

    Given your last comment about the recordset, I went into my sub, put break immediately on the .Open statement, and a debug.print right above that. My intention was to post back the actual query.

    However, as soon as I run the sub, it immediately jumps to the Method or Data Element Not Found and highlights .Offset

    My query isn't even getting run.

    I even put a break on the Private Sub line, but it did not stop. It went straight to the error message.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  15. #15
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Porting ADO Recordset To Excel

    Change
    VB Code:
    1. rngRowStart.Offset(0, 0).Value = adoRS(5)
    to
    VB Code:
    1. Debug.Print adoRS(5)
    And lets see if that line throws an error, this will confirm that the issue is with the recordset.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  16. #16

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Porting ADO Recordset To Excel

    Quote Originally Posted by DKenny
    Change
    VB Code:
    1. rngRowStart.Offset(0, 0).Value = adoRS(5)
    to
    VB Code:
    1. Debug.Print adoRS(5)
    And lets see if that line throws an error, this will confirm that the issue is with the recordset.
    I did this, and again, immediately it went straight to the offset error, but on the next line, and there was nothing in the immediate window, which means Debug.Print adors(5) never ran.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  17. #17
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Porting ADO Recordset To Excel

    That is so weird

    OK, lets try a different approach.
    Delcare a long variable lets call it lRowNum and before we set the rngRowStart we will set this to the value 7.
    Now we can change rngRowStart to reference the entire set of cells on the row that need to be updated.
    Within the loop we will refer to each cell by using the .cells property of the range object.
    Still within the loop we can increment lRowNum and reset the rngRowStart object

    here's a revised code snippet.
    VB Code:
    1. lRowNum = 7
    2.    
    3.     With shWorkSheet
    4.         Set rngRowStart = .Range(.Cells(lRowNum, 1), .Cells(lRowNum, 7))
    5.     End With
    6.    
    7.         'Start of recordset
    8.         adoRS.MoveFirst
    9.         Do
    10.             'Place each element in the coresponding column
    11.             rngRowStart.Cells(1).Value = adoRS(5)
    12.             rngRowStart.Cells(2).Value = adoRS(2)
    13.             rngRowStart.Cells(3).Value = adoRS(9)
    14.             rngRowStart.Cells(4).Value = adoRS(11)
    15.             rngRowStart.Cells(5).Value = adoRS(3)
    16.             rngRowStart.Cells(6).Value = adoRS(4)
    17.             rngRowStart.Cells(7).Value = adoRS(10)
    18.        
    19.             'Next Record
    20.             adoRS.MoveNext
    21.            
    22.             'Next Row
    23.             lRowNum = lRowNum + 1
    24.            
    25.             With shWorkSheet
    26.                 Set rngRowStart = .Range(.Cells(lRowNum, 1), .Cells(lRowNum, 7))
    27.             End With
    28.        
    29.         Loop Until .EOF
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  18. #18

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Porting ADO Recordset To Excel

    Now the invalid method is .Value

    I backspaced .Value off of rngRowStart.Cells(1) and then put in a . so I would get the drop down intellisense, and Value is not listed. In fact, the only thing beginning with V is VerticalAlignment.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  19. #19
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Porting ADO Recordset To Excel

    which means Debug.Print adors(5) never ran.
    maybe not - perhaps the value of that field is Null, or an empty string (I haven't read enough of the thread to know ).

    Try adding a breakpoint to the Debug line, and add a watch of adors(5) ... then see what it contains (especially for the Value property).

  20. #20
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Porting ADO Recordset To Excel

    I think I got it. We didn't specify that rngRowStart was an Excel range.
    Change
    VB Code:
    1. Dim rngRowStart As Range
    to
    VB Code:
    1. Dim rngRowStart As Excel.Range

    If this works then either the .offset approach or the total range approach should work.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  21. #21

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Porting ADO Recordset To Excel

    The Excel.Range did it!

    Now, the .Offset thing works just fine. I have some formatting issues (my columns don't line up properly) but I can handle that. The important this is that I'm getting all my data.

    Thank you SIR!!!!!!!!!!!!!!!!!!
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  22. #22
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] Porting ADO Recordset To Excel

    I can't believe it took me that long. I was writing VBA in Excel so the full qualification of the object was not required. I'm just glad we finally got it working.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

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