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.