PC Review


Reply
Thread Tools Rate Thread

Close Form Event

 
 
PeterM
Guest
Posts: n/a
 
      5th Jun 2010
I have a AC2003 bound form. The condition I'm trying to capture is if the
user modifies any item on the form and they close the form without saving
changes. Simple...right? I found out that Form_Close doesn't work so I
tried the following code in the Form_Unload event and that isn't working
either. It does not even trigger the event. Can someone please tell me what
I'm doing wrong? I would be much appreciated.

Private Sub Form_Unload(Cancel As Integer)
If Me.btnSave.Enabled Then
Dim strMsg As String
strMsg = strMsg & "Save Changes?" & Chr(13) & Chr(13)
If MsgBox(strMsg, vbQuestion + vbYesNo, "Please Confirm!") = vbYes
Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
Else
Cancel = True
End If
End If
End Sub

 
Reply With Quote
 
 
 
 
Jeanette Cunningham
Guest
Posts: n/a
 
      5th Jun 2010
You can code the save button like this:

Private Sub btnSave_Click()
Dim strMsg As String
strMsg = strMsg & "Save Changes?" & Chr(13) & Chr(13)

If Me.Dirty = True Then
If MsgBox(strMsg, vbQuestion + vbYesNo, "Please Confirm!") = vbYes Then
Me.Dirty = False
Else
Me.Undo
End If
End If
End Sub

You will need some error handling to trap errors if something stops the
save, for example a required field missing.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia

"PeterM" <(E-Mail Removed)> wrote in message
news:E5B3554A-93AE-4BA9-8D54-(E-Mail Removed)...
>I have a AC2003 bound form. The condition I'm trying to capture is if the
> user modifies any item on the form and they close the form without saving
> changes. Simple...right? I found out that Form_Close doesn't work so I
> tried the following code in the Form_Unload event and that isn't working
> either. It does not even trigger the event. Can someone please tell me
> what
> I'm doing wrong? I would be much appreciated.
>
> Private Sub Form_Unload(Cancel As Integer)
> If Me.btnSave.Enabled Then
> Dim strMsg As String
> strMsg = strMsg & "Save Changes?" & Chr(13) & Chr(13)
> If MsgBox(strMsg, vbQuestion + vbYesNo, "Please Confirm!") = vbYes
> Then
> DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
> acMenuVer70
> Else
> Cancel = True
> End If
> End If
> End Sub
>



 
Reply With Quote
 
PeterM
Guest
Posts: n/a
 
      5th Jun 2010
I don't think I've explained the problem clearly. I have the save button
setup the way you define in your example. How do I capture if the user
changes a bound item on a form and tries to close the form without clicking
on the save button first. There is no close button on the form, a form is
closed by a parent form when another form is opened. I need to determine
when the form is closed but still is dirty. THe unload and close events
don't work in that situation.

"Jeanette Cunningham" wrote:

> You can code the save button like this:
>
> Private Sub btnSave_Click()
> Dim strMsg As String
> strMsg = strMsg & "Save Changes?" & Chr(13) & Chr(13)
>
> If Me.Dirty = True Then
> If MsgBox(strMsg, vbQuestion + vbYesNo, "Please Confirm!") = vbYes Then
> Me.Dirty = False
> Else
> Me.Undo
> End If
> End If
> End Sub
>
> You will need some error handling to trap errors if something stops the
> save, for example a required field missing.
>
>
> Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
>
> "PeterM" <(E-Mail Removed)> wrote in message
> news:E5B3554A-93AE-4BA9-8D54-(E-Mail Removed)...
> >I have a AC2003 bound form. The condition I'm trying to capture is if the
> > user modifies any item on the form and they close the form without saving
> > changes. Simple...right? I found out that Form_Close doesn't work so I
> > tried the following code in the Form_Unload event and that isn't working
> > either. It does not even trigger the event. Can someone please tell me
> > what
> > I'm doing wrong? I would be much appreciated.
> >
> > Private Sub Form_Unload(Cancel As Integer)
> > If Me.btnSave.Enabled Then
> > Dim strMsg As String
> > strMsg = strMsg & "Save Changes?" & Chr(13) & Chr(13)
> > If MsgBox(strMsg, vbQuestion + vbYesNo, "Please Confirm!") = vbYes
> > Then
> > DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
> > acMenuVer70
> > Else
> > Cancel = True
> > End If
> > End If
> > End Sub
> >

>
>
> .
>

 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      5th Jun 2010
On Fri, 4 Jun 2010 22:00:58 -0700, PeterM <(E-Mail Removed)>
wrote:

>I don't think I've explained the problem clearly. I have the save button
>setup the way you define in your example. How do I capture if the user
>changes a bound item on a form and tries to close the form without clicking
>on the save button first. There is no close button on the form, a form is
>closed by a parent form when another form is opened. I need to determine
>when the form is closed but still is dirty. THe unload and close events
>don't work in that situation.


Use the form's BeforeUpdate event. It will fire if any field value is changed
and any action is taken that would save it (such as closing the form, pressing
Shift-Enter, etc.) You can set the value of a public variable in the
legitimate "close" button and check that variable's value in the BeforeUpdate
event; if you don't want to save the record, issue a message and set the
event's Cancel parameter to True.
--

John W. Vinson [MVP]
 
Reply With Quote
 
PeterM
Guest
Posts: n/a
 
      6th Jun 2010
Thanks John!

"John W. Vinson" wrote:

> On Fri, 4 Jun 2010 22:00:58 -0700, PeterM <(E-Mail Removed)>
> wrote:
>
> >I don't think I've explained the problem clearly. I have the save button
> >setup the way you define in your example. How do I capture if the user
> >changes a bound item on a form and tries to close the form without clicking
> >on the save button first. There is no close button on the form, a form is
> >closed by a parent form when another form is opened. I need to determine
> >when the form is closed but still is dirty. THe unload and close events
> >don't work in that situation.

>
> Use the form's BeforeUpdate event. It will fire if any field value is changed
> and any action is taken that would save it (such as closing the form, pressing
> Shift-Enter, etc.) You can set the value of a public variable in the
> legitimate "close" button and check that variable's value in the BeforeUpdate
> event; if you don't want to save the record, issue a message and set the
> event's Cancel parameter to True.
> --
>
> John W. Vinson [MVP]
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form close event Anand Kale Microsoft Dot NET Compact Framework 1 2nd Nov 2007 12:13 PM
Form.Closing event not fired always when i call Form.Close method berylwilson Microsoft Dot NET Framework Forms 1 20th Apr 2007 02:11 AM
Proper way to close a form in the Form Load event robert d via AccessMonster.com Microsoft Access Form Coding 2 15th Jan 2006 01:58 AM
Close Form after certain event happens =?Utf-8?B?R2lsYmVydCAyMDk3?= Microsoft Access Forms 1 13th Oct 2005 01:29 PM
Form On-Close event Johnb Microsoft Access 1 20th Oct 2004 02:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:31 PM.