+ Reply to Thread
Results 1 to 22 of 22

Class modules and Events

  1. #1
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Class modules and Events

    I'm looking for an utter idiots guide to user defined events and how to use them in my code. I've found some stuff but, to be honest, as a non-programmer (beyond what I did 30 years ago and years of using VBA since) its double dutch. Really just looking to put together an event to tell me when the last of a number of external data queries have finished refreshing so that I can get the rest of the code going. As the next bit of code relies on the results I can't used background refresh but seeing as there are about 6 queries I don't want to run them one after another either. Hence the problem - and the need for an idiots guide on custom events. Many thanks.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Class modules and Events

    The querytable object has a event you might be able to use.

    In order to create the event you need to use a class object.

    Class module Class1
    Please Login or Register  to view this content.
    normal code module

    Please Login or Register  to view this content.
    When it works you should see the 2 msgboxes in the test routine get executed, whilst the message box in the event will happen once refresh completes.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    Andy

    That may be the first time I've made head or tail out of what has been going on with the event handler like this. So presumably I could use the code as given and then loop with a second delay say in my main code until such time as the result is the data has been refreshed?

  4. #4
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    Actually ignore that rubbish. Just twigged what was actually going on. Thanks again.

  5. #5
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    Andy

    Sorry to be a nuisance. Wondered if you could help me a bit further with this - mainly due to my incompetence with it!!

    If I have say 5 queries and I need to know when the last one has finished how would I implement that? I have played around with for loops etc but as it sets each connection as qQueryCallback it doesn't work as expected. I've also played around with a lot of other stuff but can't quite implement it as I had hoped. Thanks.

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Class modules and Events

    Something like this, where you have a property in the class to determine is completed.
    Then you run a routine when the complete event fires. If all have then your done.

    Note, I have not tested any of this code.

    Standard code module
    Please Login or Register  to view this content.
    Class module

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    Cheers Andy

    In the TestbackgroundRefresh it runs through connindex OK but the piece of code to refresh in for each callback doesn't appear to run. So I get the "done with this code" msgbox but not the "start in background" ones.

    If I run the CheckAllRefreshes code it fails at for each callback as there's no object - not sur if this is down to the first part of code failing or not. As you can guess I'm out of my current knowledge zone to work that bit out!

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Class modules and Events

    Sorry forgot to add each callback in to the collection so they would remain in memory

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    Thanks for your patience here - much appreciated.

    That added line fails due to no object or with block.

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Class modules and Events

    I think it's the gQueryCallback and qQueryCallbacks. When you type in forum exit box it underlines in red and you can't see the different between q and g

    Please Login or Register  to view this content.
    I have tested this code and it works for my query test

  11. #11
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    I should really have spotted that on the VBA editor itself. Works a treat. Struggling to follow it though. Not sure how the CheckAllRefreshes code starts without apparently having a call to it. Looks like it is to do with the callbacks in gQueryCallbacks but that's about as far as I can work out *dummy*

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Class modules and Events

    That routines is being called from the event in the class. So originally it just showed a msgbox, now it calls a routine to check all objects in the collection.

    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    Doh! Didn't look in the class module (like a plum). Not going to pretend I know all of the ins and outs of what is happening there but can see it is doing what I need - now just to wrap it into my code now. If I call TestbackgroundRefresh from my main code to start the refresh routine, how do I stop the rest of the main routine running until the CheckAllRefreshes has finished (never had to work with anything like this before)

  14. #14
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    I tried some variations of this but looking at the Queries and Connections panel the refresh never starts and I have to escape the code running, no idea why though as my tiny brain thinks it should work OK.

    Please Login or Register  to view this content.
    The CheckAllRefreshes instead of giving a msgbox to say it is done sets the flag RefreshComplete to true. What did I get wrong?

  15. #15
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Class modules and Events

    Not sure what you are doing.

    Perhaps this is the approach you should be doing.

    Please Login or Register  to view this content.
    You kick off the main_test routine, which starts all your queries.
    The once the CheckAllRefreshs is complete you execute the rest of your code.

  16. #16
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    Good point that! I was so stuck in the code I had I couldn't see a way round it. Thanks very much for the help with this. Hopefully be golden from here. And you've left me with plenty learning to be getting on with in there too!

  17. #17
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    Andy

    Sorry to pester you. Was wondering something. Is there any way to get the name of the query that has caused the event to trigger (ie has just completed)? I can get the name easily enough in the TestBackgroundrefresh but not in either the class event or the CheckAllRefreshes. Thanks again.

  18. #18
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Class modules and Events

    Pass as an argument when you call

    Please Login or Register  to view this content.
    You will also need to add argument to the CheckAllRefreshes routine

  19. #19
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    Tried everywhere except there with that as a combination - cheers for that.

  20. #20
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    Think I'm being a bit dim - added the above to the class module. In CheckAllRefreshes changed the opening line to:

    Please Login or Register  to view this content.
    Application-defined or object-defined error in the class module at the CheckAllRefreshes MyQuery.Name line. Any ideas?
    Last edited by malcmail; 04-21-2020 at 02:09 PM.

  21. #21
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Class modules and Events

    I had a look, using the locals window, at the MyQuery object and it appears the Name property is not assigned, which causes the error.

    Try this.
    Please Login or Register  to view this content.
    You can also have a look through the object in local window to see if there is another property that would be more useful for you purpose if that name is not.

  22. #22
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Class modules and Events

    That worked like an absolute charm. Thanks for the time and effort. Much appreciated.

+ 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. Writing Class modules
    By Geetha Gupta in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-09-2011, 04:28 AM
  2. Class Modules Help
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2009, 06:45 AM
  3. Class Modules?
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-14-2008, 06:39 AM
  4. Re: Events, Class Modules and Addins ... oh my!
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-01-2006, 09:50 AM
  5. [SOLVED] Events, Class Modules and Addins ... oh my!
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-31-2006, 09:23 PM
  6. Class Modules
    By MattShoreson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-22-2006, 06:40 AM
  7. [SOLVED] Basic question - modules and class modules - what's the difference?
    By Mark Stephens in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-08-2005, 07:08 AM
  8. [SOLVED] Class Modules
    By Pavlos in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-19-2005, 02:06 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