Results 1 to 11 of 11
  1. #1
    lefty2cox is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    May 2013
    Posts
    163

    Updating subform VBA

    I have myForm with mySubForm1. MySubForm1 is based on a select query (used the ellipsis in the record source property) which is based on myTable.

    I also have another subform, mySubForm2, which is based on myQuery (created in query design). MyQuery is based myTable as well and performs a sum for me.



    mySubForm2 is simply used to display summary data. When there is a change in mySubform1, I'd like mySubform2 to update and display the new data. I know I'm going to be dealing with after_update but am not sure how to do it. I've tried several things but no luck.

    Thanks.

  2. #2
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Add the code below into the module of the main form and test the behavior:
    Code:
    Option Compare Database
    Option Explicit
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    'Code for the main form.
    'Sets an event listener to be able to catch the events of the subform 1.
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Private WithEvents mSF1 As Form 'Event listener of subform 1.
    '
    
    Private Sub Form_Load()
        Set mSF1 = Me.mySubForm1.Form                'Point the listener to the form of subform 1.
        mSF1.AfterUpdate = "[Event Procedure]"       'Make the event AfterUpdate enabled.
        mSF1.AfterDelConfirm = "[Event Procedure]"   'Make the event AfterDelConfirm enabled.
    End Sub 
    
    Private Sub Form_Unload(Cancel As Integer)
        Set mSF1 = Nothing 'Release the event listener.
    End Sub
    
    Private Sub mSF1_AfterDelConfirm(Status As Integer)
        If Status = acDeleteOK Then
            'A record in subform 1 has been deleted.
            'Requery the subform 2.
            Me.mySubForm2.Requery
        End If
    End Sub
    
    Private Sub mSF1_AfterUpdate()
        'A record in subform 1 has been updated.
        'Requery the subform 2.
        Me.mySubForm2.Requery
    End Sub
    I hope it helps,
    John

  3. #3
    lefty2cox is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    May 2013
    Posts
    163
    Quote Originally Posted by accesstos View Post
    Add the code below into the module of the main form and test the behavior:
    Code:
    Option Compare Database
    Option Explicit
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    'Code for the main form.
    'Sets an event listener to be able to catch the events of the subform 1.
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Private WithEvents mSF1 As Form 'Event listener of subform 1.
    '
    
    Private Sub Form_Load()
        Set mSF1 = Me.mySubForm1.Form                'Point the listener to the form of subform 1.
        mSF1.AfterUpdate = "[Event Procedure]"       'Make the event AfterUpdate enabled.
        mSF1.AfterDelConfirm = "[Event Procedure]"   'Make the event AfterDelConfirm enabled.
    End Sub 
    
    Private Sub Form_Unload(Cancel As Integer)
        Set mSF1 = Nothing 'Release the event listener.
    End Sub
    
    Private Sub mSF1_AfterDelConfirm(Status As Integer)
        If Status = acDeleteOK Then
            'A record in subform 1 has been deleted.
            'Requery the subform 2.
            Me.mySubForm2.Requery
        End If
    End Sub
    
    Private Sub mSF1_AfterUpdate()
        'A record in subform 1 has been updated.
        'Requery the subform 2.
        Me.mySubForm2.Requery
    End Sub
    I hope it helps,
    John
    Worked perfectly. Thank you so much. I was adding code to the module for mySubForm1. Makes much more sense now.

  4. #4
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by lefty2cox View Post
    Worked perfectly. Thank you so much. I was adding code to the module for mySubForm1. Makes much more sense now.
    You are welcome.

    As you see, with this way, you have the absolute control of a specific loaded form in one variable.
    The variable mSF1 could point to any loaded Form object, and, you have access to any property or method of that form, and in addition, the keyword "Withevents" makes available all its events.

    Cheers,
    John

  5. #5
    lefty2cox is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    May 2013
    Posts
    163
    I don't fully understand it yet but I'll read about it some more. Works flawlessly though. Thanks again.

  6. #6
    lefty2cox is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    May 2013
    Posts
    163
    This code has been working really well for a while now but I'm getting an error lately and was wondering if it makes sense. It's a weird behavior. Occasionally, it will throw the following error when loading and unloading:

    The expression on Unload you entered as the event property setting produced the following error: Object class does not support the set of events.

    My guess is it's my fault, because it usually is, but I can't figure it out. This problem can be fixed by opening VBA. Once I open it to the code, the problem goes away and stays away for the session.

    I'm puzzled.
    Thanks

    Edit: Sorry about that...I thought I replied with quote but I'm referring to the bit just above that accesstos posted for the event listener.
    Last edited by lefty2cox; 03-22-2021 at 11:02 AM.

  7. #7
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by lefty2cox View Post
    This code has been working really well for a while now but I'm getting an error lately and was wondering if it makes sense. It's a weird behavior. Occasionally, it will throw the following error when loading and unloading:

    The expression on Unload you entered as the event property setting produced the following error: Object class does not support the set of events.

    My guess is it's my fault, because it usually is, but I can't figure it out. This problem can be fixed by opening VBA. Once I open it to the code, the problem goes away and stays away for the session.

    I'm puzzled.
    Thanks

    Edit: Sorry about that...I thought I replied with quote but I'm referring to the bit just above that accesstos posted for the event listener.
    Hi!

    If you didn't change something in the code or the objects of the form, try at first a Compact and Repair. Usually, this resolves issues such this you are mention.

  8. #8
    lefty2cox is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    May 2013
    Posts
    163
    Compact / Repair didn't work. I did change stuff on the form... added a button or two...maybe another subform.

    I found that I have to open the actual class object in VBA. Opening VBA alone doesn't solve it. I have to open the class object for the specific form. Then I can close it and all is well until I restart the program again.

    I'll try to remove things one at a time to pin it down and report back.

    Thanks

  9. #9
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Did you Compile your code before Compact and Repair?
    Try to keep compiled and saved your project before the Compact and Repair.

    Sorry, I can't help further with provided info.

  10. #10
    lefty2cox is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    May 2013
    Posts
    163
    I did not the first time. But after reading your post, I went ahead and repeated with compiling. That worked. Thank you.

  11. #11
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by lefty2cox View Post
    I did not the first time. But after reading your post, I went ahead and repeated with compiling. That worked. Thank you.
    I apologize, I had to mention it at first.

    Glad you have it resolved.

    Cheers,
    John

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

Similar Threads

  1. subform not updating
    By Synergy.ron@gmail.com in forum Access
    Replies: 2
    Last Post: 01-12-2021, 04:27 PM
  2. After Updating Subform
    By KBNETGUY in forum Programming
    Replies: 6
    Last Post: 04-19-2018, 01:40 PM
  3. Replies: 2
    Last Post: 07-13-2016, 02:19 PM
  4. Replies: 1
    Last Post: 11-07-2012, 05:31 PM
  5. updating subform
    By cgrinage in forum Access
    Replies: 1
    Last Post: 08-21-2012, 05:38 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