0

I have a form, "FRM", on which there's a subform "SFRM". When the users unloads FRM, I want to give them the choice of keeping all the changes, losing them by deleting the records from the underlying table, or keeping the FRM open.

But I don't want to bug them with this choice if nothing's been changed. In other words, they should only get the message box if SFRM is "Dirty".

In FRM I tried this:

Private Sub Form_Unload(Cancel As Integer)
    
    If Me.SFRM.Dirty Then
         'Here is Msgbox asking the user for a Yes, No, or Cancel.
         'That's followed by a Select Case block
    End If
End Sub

I then learned that there's no such thing as Me.SFRM.Dirty. That is, "Dirty" isn't a valid property when you refer to the subform this way.

Even if I 'could' read the Dirty status from within FRM, I read that it wouldn't matter, because once the focus has returned to FRM, the Dirty property is reset to False.

What's the easiest way to track whether the subform has been modified?

Shawn V. Wilson
  • 1,002
  • 3
  • 17
  • 42
  • 1
    Assuming SFRM is name of subform container control, syntax is `Me.SFRM.Form.Dirty`. However, as you noted as soon as focus returns to main form, data in subform is committed and it is no longer dirty. – June7 Jun 26 '23 at 22:00
  • What you want could be complicated and likely involve Transaction method. Review https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/transaction-statement-microsoft-access-sql and https://stackoverflow.com/questions/2078432/do-we-have-transactions-in-ms-access#:~:text=Access%20doesn%27t%20have%20transactions%2C%20because%20Access%20is%20not,I%27ve%20used%20it%20%28since%20Jet%202.x%2C%20e.g.%2C%201996%29. Possibly would need to set a global variable as soon as any edit is initiated and test that variable before prompting user. – June7 Jun 26 '23 at 22:00

1 Answers1

2

You'll need to track this in the subform.

I would put a checkbox on the main form, visible while debugging, invisible in production.

Then in the subform you do

Private Sub Form_AfterUpdate()

    ' Notify main form of changes in the subform
    Me.Parent.chkSubModified = True
    
End Sub

and then in the main Form_Unload event, check the state of this checkbox.

If your subform inserts or deletes records too, you may need to repeat the above code in the appropriate subform events.

Andre
  • 26,751
  • 7
  • 36
  • 80
  • Not only do I like this idea, but it may be better than you think. This warning I'm doing is supposed to occur if the hasn't pressed the "Save" button on FRM. Instead of an invisible checkbox, I could make the Save button Disabled normally and Enabled when needed! – Shawn V. Wilson Jun 26 '23 at 23:40