+ Reply to Thread
Results 1 to 8 of 8

Excel VBA - Loop through recordsets ADODB with For Loop Error - Help

  1. #1
    Forum Contributor
    Join Date
    08-22-2017
    Location
    USA
    MS-Off Ver
    2016 WINDOWS and MAC
    Posts
    294

    Excel VBA - Loop through recordsets ADODB with For Loop Error - Help

    Hi all,

    Basically, to reduce overhead and repetitive code, I need to loop through SQL query definitions 10 times. I have ten unique recordset.source strings. With each query, fields are pulled to userform... BUT I cannot seem to get the for loop to allow this. I get error "Operation is not allowed when the object is closed"... .. .. .. .. . . . Its not closed. Im opening it every time.

    Please see my code below:

    Please Login or Register  to view this content.

    Thank you for the help!

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Excel VBA - Loop through recordsets ADODB with For Loop Error - Help

    Try change
    1)
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    2)
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Last edited by jindon; 08-26-2018 at 12:56 AM.

  3. #3
    Forum Contributor
    Join Date
    08-22-2017
    Location
    USA
    MS-Off Ver
    2016 WINDOWS and MAC
    Posts
    294

    Re: Excel VBA - Loop through recordsets ADODB with For Loop Error - Help

    Jiindon,

    Same error.

    But I did not follow your last recommendation. The closeconnection line name must close the recordset and connection. It is also out of the loop so its not suspect to be any issue.

    The code works find without a loop, i should have mentioned.

    I still receive the same error.

    Thanks for your help with this.


    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Excel VBA - Loop through recordsets ADODB with For Loop Error - Help

    You seem to using the same connection string but different data source, so get
    Please Login or Register  to view this content.
    to outside of the loop. (before the loop) then apply the changes I suggested.

  5. #5
    Registered User
    Join Date
    08-23-2018
    Location
    Ireland
    MS-Off Ver
    Office365
    Posts
    17

    Re: Excel VBA - Loop through recordsets ADODB with For Loop Error - Help

    Which line is the error occurring on? Can you step thru the macro to test and update us (use F8 to step thru the macro)

  6. #6
    Forum Contributor
    Join Date
    08-22-2017
    Location
    USA
    MS-Off Ver
    2016 WINDOWS and MAC
    Posts
    294

    Re: Excel VBA - Loop through recordsets ADODB with For Loop Error - Help

    Hi,

    Thank you both for your replies and concerns with my problem.

    So I was able to get the code to work using Jindon's suggestions:

    Please Login or Register  to view this content.
    I realize now that I am seeing clearly, i was asking the code to close the recordset and connection despite the connection and recordset already being closed in the end of the For Loop, thus "Operation can't be completed when object is close".

    Thanks for the help with this. Much better code than what I had before. Faster!!

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Excel VBA - Loop through recordsets ADODB with For Loop Error - Help

    Good to know it worked and thanks for the rep.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  8. #8
    Forum Contributor
    Join Date
    08-22-2017
    Location
    USA
    MS-Off Ver
    2016 WINDOWS and MAC
    Posts
    294

    Re: Excel VBA - Loop through recordsets ADODB with For Loop Error - Help

    Jindon,

    I will confirm that I have no issues this evening. Then solve the thread.

    Thank you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Working with adodb recordsets (virtual and table based)
    By Saintor in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-05-2018, 10:57 AM
  2. [SOLVED] Go to next loop iteration if current loop has error
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-03-2016, 09:54 PM
  3. What's the best way to combine SQL and ACE ADODB recordsets?
    By dmasters4919 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-30-2015, 04:39 PM
  4. HOW TO: Pause loop, fix error on a popup UserForm, continue loop
    By AndyMachin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-19-2014, 04:37 PM
  5. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  6. Nesting a loop within a loop error
    By PunPryde in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2011, 08:07 PM
  7. ADODB Recordsets
    By cosmarchy in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-24-2010, 04:53 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1