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

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Have 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.

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.

This article was originally posted on the Developing for Dynamics GP Blog and has been reposted on http://www.winthropdc.com/blog.

6 thoughts on “Quick Tip: Solving “Do you want to Save?” dialog appearing after VBA customization

  1. 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

    Like

  2. 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

    Like

  3. 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

    Like

Please post feedback or comments

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.