Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / May 2007

Tip: Looking for answers? Try searching our database.

Close Button

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex - 30 May 2007 16:23 GMT
I have a form with the below code in the BeforeUpdate event.

I created a 'New Record' button:  DoCmd.GoToRecord , , acNewRec
and a 'Close' button:  DoCmd.Close, but they aren't working right.  When
they are clicked the message box "Do you want to save this new log?" appears
(which it should).  However, even if a user clicks "yes" to save a log, the
DoCmd.GoToRecord or DoCmd.Close are carried out.  How can I stop the going to
a new record or closing if a user wants to save the log?  Thanks.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
   If MsgBox("Do you want to save this new log?", vbYesNo) = vbNo Then
   Cancel = True
   Me.Undo
   Exit Sub
   
   End If
   End If

If IsNull(Me.Part__) Then
MsgBox "There is information missing.", vbOKOnly
Cancel = True
End If
End Sub
Klatuu - 30 May 2007 17:25 GMT
Move this part of the code to your other events.
> If Me.NewRecord Then
>     If MsgBox("Do you want to save this new log?", vbYesNo) = vbNo Then
>     Cancel = True
>     Me.Undo
>     Exit Sub

   If Me.NewRecord Then
       If MsgBox("Do you want to save this new log?", vbYesNo) = vbNo Then
           Me.Undo
           DoCmd.GoToRecord , , acNewRec
       End If
   End If

   If Me.NewRecord Then
       If MsgBox("Do you want to save this new log?", vbYesNo) = vbNo Then
           Me.Undo
           DoCmd.Close
       End If
   End If

The issue is that the Cancel only affects updating the record in the Before
Update, it does not cancel any other commands issused in any event.  So you
need to ask the question before you take the action.
Signature

Dave Hargis, Microsoft Access MVP

> I have a form with the below code in the BeforeUpdate event.
>
[quoted text clipped - 20 lines]
> End If
> End Sub
Alex - 30 May 2007 18:01 GMT
Perfect!  Thank you.

> Move this part of the code to your other events.
> > If Me.NewRecord Then
[quoted text clipped - 45 lines]
> > End If
> > End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.