Results 1 to 9 of 9
  1. #1
    sjs94704 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Location
    Berkeley, CA
    Posts
    20

    The power of the OnActivate Event

    Hey everyone! Just a comment that I am discovering the power of the OnActivate Event. I am using it for things like requery a list box(s), Maximizing a form, and a whole host of other cool stuff. I'm just saying, I think that maybe a lot of us go without using this event. Anyway, just saying......check out for yourself the kinds of stuff it can do for you.



    For those of you who are maybe new to programming, the OnActivate Event fires every time you land on the form. So, that is why I use it to requery a list boxs and other regular tasks that need to happen every time the form becomes the current form your working on.

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I have always used the On Load and On Current Events. Do you feel On Activate provides less clutter or better readability?

  3. #3
    sjs94704 is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Nov 2012
    Location
    Berkeley, CA
    Posts
    20
    The issue with the onLoad event that I found was that in my case, I was just hiding forms using the visible property, so forms were not getting loaded again because they were already loaded and just hidden. So, in the future, I will not use this strategy in the OnLoad event. Basically, put all the stuff in the OnActivate Event that you want to be SURE happens every time the form becomes the current form that the user is working with. And yes, now that I think about clutter, since the kinds of things I'm suggesting you put here are to happen as I have explained, then being kind of out of the way in the OnActivate Event does make sense. I keep going back to my example of requerying a list box. That should happen every single time you land on a form that has a list box. So your code should look something like:

    Private Sub OnActivate ()
    Me.lstWorkOrders.Requery 'Requery the Work Order List Box
    End Sub

    Get this code out of your OnLoad event and into OnActivate! Save OnLoad for other more important stuff.....

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Generally speaking, the OnActivate event needs to be used anytime you think that you need to use the Form_GotFocus. That's because a Form can only receive Focus if there are no Controls on the Form that can receive Focus, which is a relatively rare event!

    Typically, OnActivate is used in situations like the OP described...where the user navigates from one Form to another Form, then back to the original Form, which is already opened. Also typically, it involves situations where you've added data to a Table, via the second Form, and want that data reflected in the original Form. You can use it to Requery a Listbox, a Combobox or the entire original Form.

    And, yes, given the number of times I've had to recommend it, for doing these kinds of things, over the years, it probably is underutilized!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I have a form with several combos on it. The purpose of each of these combos is to add the Key value to a Foreign Key Field. If the user triggers the Not In List event of one of the combos, they have the option to open a Pop-Up Modal form and add a new record. The Pop-Up form will requery the appropriate combo.

    The way the event is being described here is that it will fire when the form receives focus from another form, like receiving focus after my pop-up form opens. But, in my case I would not want each of my Combos to requery. So, it would be best if I use the Pop-Up to requery a specific combo.

    Furthermore, according to MSDN, the Activate event does not fire when receiving focus from another form. It only fires when the Form is first opened. After Load and before Current. So, maybe use it when you need the form to load before executing code and not every time the User navigates to a new record.
    https://msdn.microsoft.com/en-us/lib...ffice.14).aspx

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by ItsMe View Post

    ...according to MSDN, the Activate event does not fire when receiving focus from another form. It only fires when the Form is first opened
    Well, that's partially correct...which is about par for any Access help! It doesn't fire if the secondary Form is a Popup Form or if opened in Dialog Mode (which means Popup is set to Yes) but for other Forms the OnActivate will fire when you return to the original Form.

    If still in doubt...test it out!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by Missinglinq View Post
    ..If still in doubt...test it out!...
    I might. I am still trying to figure where I can use it. I know, in the past, I would have trouble with code not running properly on the load event and I did not want it in the Current event either. So, this might be helpful. However, if it fires on focus from another form too, that may hinder its benefits. I would like to use it as advertised, after load and before current when the form first opens. I could see using it for that purpose.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by Missinglinq View Post
    Well, that's partially correct...
    Yeah, I tested it in 2013 using tabbed forms and the Activate event fired when I tabbed between one form and the other. When I opened the second form using the acDialog argument, closing the Pop-Up did not fire the Activate event in the first form. I think I would like it best if it only fired when the form opened. That's my two pennies.

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    As with most tools, it just depends on what, exactly, you're trying to do! Is you're using it to Requery something, on returning to FormA from FormB, it doesn't really matter that the Requery also occurs when the Form initially loads; it's unnecessary/redundant , but it doesn't hurt anything. But using it for other things might present problems if done when OnLoad.

    One of the biggest mistakes I see here, and on other forums, vis-à-vis using the OnLoad event, is trying to set formatting for all Records based on a condition. Doing that in the OnLoad event means that the formatting for all Records will be based on the condition in the first Record! That kind of thing, when done in code, has to be in the OnCurrent event, to be Record-appropriate.

    Like I said, what tool you use depends on the job at hand; the trick is learning which tool to use...and I'm still learning after using Access for 15 years!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calling Microsoft Power Shell from within Access
    By crowegreg in forum Programming
    Replies: 1
    Last Post: 11-18-2014, 02:59 PM
  2. Replies: 2
    Last Post: 01-30-2014, 09:28 AM
  3. Replies: 0
    Last Post: 08-20-2013, 09:05 AM
  4. export to Power Point
    By lugnutmonkey in forum Import/Export Data
    Replies: 1
    Last Post: 02-23-2013, 11:39 AM
  5. After Update Event with power function
    By Fish218 in forum Forms
    Replies: 5
    Last Post: 01-15-2013, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums