Quick Tip: Solving "Do you want to Save?" dialog appearing after VBA customization


David Meego - Click for blog homepageHave you ever used Modifier and Visual Basic for Applications (VBA) to add an additional field to a window?  Have you then had the issue when navigating through records on that window, it keeps coming up and asking "Do you want to Save, Discard or Cancel?" or "Do you want to Save, Delete or Cancel?".

How do I stop Dynamics asking "Do you want to Save?" when no changes have been made? 

To answer this question we need to first understand what is happening.  Dexterity has a "changed" flag for each window on a form and each field has a boolean property for SetChangeFlag. So when a field that has the property SetChangeFlag=True is edited, the window's changed flag is set.  When you navigate away from a record (using the browse buttons, the lookup button or closing the window) the changed flag is checked and if it has been set, the Handle_Changes script will prompt with the "Do you want to Save?" dialog.

You might say "But the user did not make any changes to the fields on the window".  However, as far as Dexterity is concerned, they did.

When VBA is used (usually on the key field's Changed() event) to read the additional fields from SQL or the DUOS and set the value on the window, the act of setting the value on the window behaves like the user just entered the data into the field.  VBA is "driving" the interface like a user.  This means that every time we move to a new record, the VBA code to display the additional data is causing the window's changed flag to be set. Hence the dialog is displayed when we try to navigate away.

So, now we know what the problem is, the solution is simple.... Just add the following VBA code after the script lines which read the data and set the window fields.

Me.Changed=False

Note: This technique might also be used when using VBA to set default values on a window. 

For examples of adding additional fields to a window, see the following page: 

Hope this Quick Tip is helpful. 

David

24-Jul-2012: Add Link to related post: The "Do you want to save?" dialog appears when no changes are made.

Comments (5)

  1. Kleytman Aular says:

    This is exactly what I was looking for!

  2. Vaidy Mohan says:

    This is an excellent tip, David. I had faced this message in the past and I used to write a Sanscript code to clear changes OR this VBA statement, depending on how VBA is used.

    This tip will certainly help many developers out there.

    Vaidy

  3. Patrick Roth [MSFT] says:

    I do this a little differently.

    On the fields that you add, change the property of SetChangeFlag to false (defaults to true).  Now when you set the field via code the window change flag isn't set.

    However since we likely DO want the window change flag to be changed if the user changes the field manually, on the field_AfterUserChanged() event, use that to _set_ the window change flag.

    Me.Changed=true

    This change mimics more closely how the process works with Dexterity.

    patrick

    dev support

  4. David Musgrave says:

    Hi Pat

    I think that the approach in the article is simpler as once the change flag for the window is cleared, we let the standard behaviour for Dexterity using SetChangeFlag=True work for the added fields.  It just requires one line added to an existing script rather than changing default field properties and additional scripts on those fields.

    David

  5. David Musgrave says:

    Posting from Mark Polino at DynamicAccounting.net

    msdynamicsgp.blogspot.com/…/quick-tip-solving-you-want-to-save.html

Skip to main content