Need To Revert Multiple Controls Back To Previous Values - vb.net

I have the following situation where I have multiple controls on a form. The user can select an Edit mode which allows them to modify the contents of the controls (textboxes, checkboxes, comboboxes, etc.) After editing has been made, the user can either select the Save or Cancel button.
Obviously clicking on save will save all changes to the database, however, if the user clicks on cancel, I want to 'somehow' revert all controls back to their previous state.
Currently, when the user clicks on cancel, the form turns off edit mode, but the controls remain with all the changes made until they reload the form.
Is there an easy way around this, or do I need to reload the entire form content from the database each time the user clicks on cancel?
Thanks in advance.

I'm just going to outline a possible solution here.
You could have a dictionary in your form in which you store the content of your controls whenever a user first modifies them, indexing them by the control name.
When the user starts modifying a control, you check if the control already exists in the dictionary. If it doesn't you add a new entry to the dictionary with the current value of teh control.
If the user cancels the editing process, you can loop through the dictionary, find each control in your form by its name (Form.Controls(ControlName) or something like that I think), set its value back to what it was and remove the entry from the dictionary.
It's not the most elegant solution as it will involve linking up all your controls to the TextChanged event or equivalent to capture their value when they are modified...
To avoid that, you could fill the dictionary right at the start when you load your form, after populating the values from the DB and then only set those controls' value back which have a different value when the user cancels.

Related

in Access, Bound Combo Boxes do not update unless the form is already Dirty

I'm working on a fairly complex Access Database, trying to build forms with custom buttons for working with records. I'm using list boxes to display and navigate through records and all fields for existing records are disabled unless the user presses an edit button. The problem I'm having is that if I press the Edit or Add New button, enabling all of the fields, and then try to change the selection in a combo box, it does not update on the first try.
If I edit a text box first then the combo boxes work fine.
I've determined that the control's beforeUpdate and afterUpdate events are not firing on the first try but the action triggers the form_Dirty event and then it works as expected. I tried setting Me.Dirty = True with the Edit button and that solved the problem but it causes problems with some of my other code and it seems like an unnecessary workaround if only I understood the actual cause of the problem. It also works as expected if I leave the Combo Box unbound, but I am trying to build a template that doesn't require too much work to build new forms off of and I would rather not go that route.
It must have something to do with some bit of code I'm using because I can start a basic form and the combo boxes work fine.
I've started a basic test form and am adding code from my template form bit by bit until the problem arises, but it's a tedious process. Any help would be appreciated.
What am I missing? Is there some way of getting the Combo Box events to fire before the form is dirty?
UPDATE:
I have templates for a basic form, a form with a single subform, and a form with multiple subs in a tab control.
After some more testing I discovered that this problem does not apply to the basic form which has no subforms. This template uses similar code to the others for new, edit, cancel, save and delete buttons and for preventing accidental changes, preventing Form_unload during an edit and so on. The main difference I can think of off the top of my head is that the templates with subforms use class modules and collections to hold various data and pass it between the main form and subforms. Not sure if or how this might relate to combo box functionality.
I built most of this last winter then got too busy over the summer to work on it. Just now picking it up again and I'm having to re-learn a lot of the details of how my code works.
you can try to use Me.CboName.Requery in your After Update event.
I figured it out. I was calling a procedure with a form refresh in it from the Form_Dirty event and for whatever reason the refresh at exactly the wrong time was causing the combo box Before_Update and After_Update events to be skipped and the combo box value to not change. It was also causing text entered into a textbox to overwrite existing text on the first keystroke.

Allowing User to Select off of Partial Records in a Continuous Form in Access

I am creating a form in access to allow users to input multiple production records for a day.
The form is set as a continuous data entry form and has data validation in place to ensure the information being entered is consistent.
I am having a problem where if a user starts typing something on a new entry, they essentially have no way to back out of it or cancel the entry without completely filling out the form.
I want to keep the data validation to ensure the data being recorded is accurate, but also do not want to lock users into an entry unless it is completely filled out.
I think the ideal would be to allow users to create a new record or select other records without needing to save the current record.
If it would be possible to make it so records only save when a button at the top of the form is pressed I think that would be ideal, but I have not found a good way to do this without requiring it on every single entry.
I have attached a picture of what I am talking about, there could be various error messages but essentially if you try to click off when a record is incomplete it will give an error until the entire form is filled out.
Example of Error Message and Image of Continuous Form
'deselect' without saving but still save what had been entered up to
that point.
That you can simulate by setting the DefaultValue of each control in the AfterUpdate event of each control:
Me!SomeTextBox.DefaultValue = Nz(Me!SomeTextBox.Value)
However, I'm not sure that will be a good idea. And you may have to reset the default values when opening the form.

Subform validation (datasheet view)

I have a subform where the user can create rows. A new row is created by clicking a button on the main form, after which the user can set the other values. Two fields are required, and the user must not be allowed to leave them empty, even by closing the form. Instead, a MessageBox should alert the user and he should stay on the offending row, allowing him to set the required values.
This must work even if the user has not made any change to the fields.
I'm trying different events with message boxes to see if any of them fire, but none of them fire when I try to leave the fields empty, for example by changing row or closing the main form. I'm looking for some equivalent to "OnBlur" in Javascript.
I've tried BeforeUpdate and On Dirty, but both of them require a change. I've solved the one for when closing the form, using On Unload with the Cancel parameter.
If I understood it correctly you are trying to validate user's entry before inserting a new row. Did you try the event 'Before Insert' of the subform? Use Cancel = -1 to disallow inserting. That should work even if the user tries to close the main form.

YUI3: Re-fire an event that I stopped from propagating?

I've got a form within a tabview with input fields. If the user changes the input fields, doesn't save, and then tries to change the tab I want to ask them if they want to save changes before the tab is changed. I'm preventing the tab-change from occurring using e.stopPropagation(). I then prompt the user to ask if they want to save their changes, discard them, or cancel. If the user chooses save or cancel I'd like to perform the operation and then change them over to the tab they requested.
Is there a way for me to re-fire the event? Or will I have to extract what tab they were requesting from the event and then manually change the active tab?
I would think that the best way to tackle this issue would be to select the tab programmatically instead of trying to "re-fire" the event somehow.
To select a tab programmatically with tabView, you need to use the selectChild(index) method.

Hiding columns in a datasheet

I am trying to hide specific columns in an Access 2007 split form through code. I need the form to check certain conditions to see whether it needs to display a column or not. I have code in the form's 'Activate' event to hide the column like this:
txtControl.ColumnHidden = True
This code works in the "Open" event, but if I hide the column on Activate, it won't display these changes until I close the form and open it again. I have tried calling the form's refresh, repaint, and requery methods, but this doesn't work. Please help!
Edit: Ideally, I need this event to occur whenever the focus switches to this form.That's why I'm using the Activate event rather than the Open event.
Try setting it in either the form's Current or Load events. You will also probably need to requery the control after setting that property: Me.TextControl.Requery Current is called every time a form's record is changed, the form is repainted or requeried. Load, as its name suggests, is called once, after the form has opened when the form loads its records. These have always been more reliable for me than using Activate, which really has to do with more the focus of the form, not really what you want.
I've had a problem like this before working in Access 2002. I was able to solve the problem with a subform by setting the subform source object equal to itself and then running the requery.
Me.SubForm.SourceObject = Me.SubForm.SourceObject
Me.SubForm.Requery
See if this technique works for your particular situation.