PDA

View Full Version : Command for a combo box to write to a table



skytbest
08-02-2011, 08:06 AM
Currently I have several combo boxes with control sources linked to columns on a table. On this form there is a 'save' button meant to save any changes you've made to the current entry being displayed. From the bottom of the page you can scroll through different entry's (different rows on the table). I like having the control source set up because the combo boxes will automatically display the information for that entry when you scroll through.

My issue is that when you change an entry (select a different option in the combo box) it changes the entry immediatley on the table. I would like for this only to happen when the user clicks the 'save' button. This way if the user changes something in a combo box by accident and then moves on to the next entry that change does not get saved. Is there any way to do this? Or does the face that the combo box has a control source mean that it will always update as soon as it have been changed?

Thank you.

hansup
08-02-2011, 09:18 AM
My issue is that when you change an entry (select a different option in the combo box) it changes the entry immediatley on the table.Your description sounds like the updated combo value is immediately saved to the table. Are you positive that's what's happening?

I would like for this only to happen when the user clicks the 'save' button. This way if the user changes something in a combo box by accident and then moves on to the next entry that change does not get saved. Is there any way to do this? Or does the face that the combo box has a control source mean that it will always update as soon as it have been changed?You could use the form's Before Update event to decide whether to allow or deny saving the record. In the Save button's click code set a boolean variable to True (blnDoIt = True). Then in the form's Before Update, if not blnDoIt = True, set Cancel = True to cancel the update and keep the user on the changed (but not saved) record. You could also pop up a message box to ask the user if she wants to discard her changes to the current record.

Also you should initially set blnDoIt = False in the form's On Current event.

skytbest
08-02-2011, 10:50 AM
After looking into it more, you're correct, that is not the case. But it does still save the changes if I move to a new record. Is there any way to avoid this? Is there any way to get to the code behind those move between record buttons at the bottom left? When ever I go into design view they dissapear.

*edit: this also happens if I close the form

hansup
08-02-2011, 01:32 PM
I think we're closer now, but I'm unsure how to get the rest of the way. The navigation buttons don't expose any methods directly. In the second part of my previous answer, I tried to direct your attention to form Before Update.

If there are unsaved changes to the current record, navigating to a different record will cause Access to save the changes. When that happens, the form's Before Update event fires. So you can add code to the Before Update event to intercede at the last moment before the save actually happens. For example, you could put up a message box to ask the user whether she wants to save or discard the changes. If she confirms "save", let the update proceed ... Access will do it's thing and the user will be at the next record. If she chooses "discard", Cancel the update and perhaps do Me.Undo to discard the changes.

I think that's your answer in a nutshell. You can go more elaborate if you want ... but that will take more code. However, nearly any time you want behavior different than Access' defaults, it's likely to take more code.

Also it seems we've been talking about edits to existing records. If you also want more control over insertion of new records, you will need to apply a similar strategy to form Before Insert.