Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confirming data changes when closing form - best approach

Status
Not open for further replies.

Moss100

Technical User
Aug 10, 2004
586
GB
Hello when I close a form I would like to confirm whether the user wishes to save the data, if changes have been made.

I have researched the OnDirty event.

I am wondering whether it is best to tie the code to the Before Update event on the form or to the click event of the my forms Close button (I have disabled the forms control box to prevent the X being used to close).

Also are there any suggestions for improving the code - in particular I am uncertain that the error handling is correct.

Many thanks for your comments and suggestions.

Regards Mark

Code:
Private Sub btn_Vend_Close_DblClick(Cancel As Integer)

'Checks to see if data has changed and if it has whether the user wants to save or undo the record, then closes
 
 On Error GoTo Error_Handle
  
  'if record has been changed the dirty property will be true
   
  If Me.Dirty = True Then
  
  'Ask if record should be saved
   If MsgBox("The record has changed - do you want to save it?", _
       vbYesNo + vbQuestion, [TempVars]![tVars_dB_MsgBox_Title]) = vbNo Then
       Me.Undo
       DoCmd.Close acForm, "frm_SA_Tab_Vendor"
      MsgBox "Your changes were NOT saved"
   Else
        
      DoCmd.Close acForm, "frm_SA_Tab_Vendor"
       MsgBox "Your changes WERE saved"
      End If
   End If
   
   If Me.Dirty = False Then
     DoCmd.Close acForm, "frm_SA_Tab_Vendor"
   End If
   
Error_Handle:
    Exit Sub

End Sub
 
My main concern is for the user to know they have to double-click on a command button to get your functionality:

Code:
Private Sub btn_Vend_Close_[red]DblClick[/red](Cancel As Integer)

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
I have used 'double click' for quite a while as I feel it requires a more deliberate action from the user.

If the user does not double click the button, the form would remain open - the only way to close it would be by double clicking the close button.

From what you say, is it bad programming to get users to double click?

Thank you Mark

 
In my opinion - yes.
Imagine you use 15 windows application on a regular basis. All other application – when you see a button on the Form – it requires you to just click on it, and something happens. And then you have this one application where when you click on a button – like everywhere else – nothing happens. I would go: “What happened?” I would be confused because this (your) application does not behave like any other Windows app.

Double-click is used when you have a list with the checkboxes, for example. So when I click on an item, it is selected, but when I double-click on it, it is selected and checked.


Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Yes I can not argue with that logic

I will change my application and use single click.

I wonder whether you have time to comment whether the code above is ok?

Also is this the way you would check whether the user wants to save the data before closing a form?

Also would I be better creating a public sub for this or using the code on each form?

Thank you. Mark.
 
>whether the code above is ok?
You should test it and see if it does what you want.

If it does what you want, I would try to create a Public Sub with a logic like:

Code:
Public Sub AskToSaveChanges(ByRef frmForm As Form)
[green]
'Checks to see if data has changed and if it has whether 
'the user wants to save or undo the record, then closes
[/green] 
On Error GoTo Error_Handle
  [green]
'if record has been changed the dirty property will be true
   [/green]
If frmForm.Dirty = True Then[green]
  'Ask if record should be saved[/green]
   If MsgBox("The record has changed - do you want to save it?", _
       vbYesNo + vbQuestion, [TempVars]![tVars_dB_MsgBox_Title]) = vbNo Then
       frmForm.Undo
       DoCmd.Close acForm, frmForm.Name[green]
      'MsgBox "Your changes were NOT saved"[/green]
   Else
      DoCmd.Close acForm, frmForm.Name[green]
      ' MsgBox "Your changes WERE saved"[/green]
   End If
Else
    DoCmd.Close acForm, frmForm.Name
End If

Exit Sub

Error_Handle:
    
End Sub

Just a suggestion, code not tested.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Thank you very much.

I see on the Microsoft site that an example
Is show of the oldvalue property (see below)

Is this better than me.undo?

Thanks for your help.

Code:
Private Sub btnUndo_Click()

     Dim ctlTextbox As Control

     For Each ctlTextbox in Me.Controls
     If ctlTextbox.ControlType = acTextBox Then
        ctlTextbox.Value = ctl.OldValue
     End If
     Next ctlTextbox

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top