?
Solved

Closing Form from After Update Event

Posted on 2008-06-17
3
Medium Priority
?
227 Views
Last Modified: 2013-11-28
I have a form that is to close after collecting data in a single field.  I put a DoCmd.Close command in the after update event which works well if the field is updated and the enter button is pressed, however if the field is updated and the form close button is selected I get the error message 'Error 2501: The Close action was canceled'.  There is code running in the close event to update the parent form.  How do I correct this error message?  Attached is the code related to this form.
Private Sub FMS_Oblig_AfterUpdate()
'Close Form
DoCmd.Close acForm, "Change Obligation"
End Sub
 
Private Sub Form_Close()
Dim rst, rst2 As Recordset
Dim strBookmark As Variant
Dim strFCPParameter As String
 
'Move active record back to same location with updated data
If Me.OpenArgs Then
    Forms("Multi Year End").List_of_Year_End_Multi_Year_Obligations.Requery
    Set rst = Forms("Multi Year End").List_of_Year_End_Multi_Year_Obligations.Form.RecordsetClone
    Set rst2 = Me.RecordsetClone
    strFCPParameter = IIf(IsNull(rst2.Fields("FCP")), " AND (FCP) Is Null", " AND (FCP='" & rst2.Fields("FCP").Value & "')")
    rst.FindFirst "(ACC='" & rst2.Fields("ACC").Value & "') AND (Appropriation='" & rst2.Fields("Appropriation").Value & "') AND ([As Of]=#" & rst2.Fields("As Of").Value & "#)" & strFCPParameter
    Forms("Multi Year End").List_of_Year_End_Multi_Year_Obligations.Form.Bookmark = rst.Bookmark
Else
    Forms("Last Year End").Last_Year_End_Obligations.Requery
    Set rst = Forms("Last Year End").Last_Year_End_Obligations.Form.RecordsetClone
    Set rst2 = Me.RecordsetClone
    strFCPParameter = IIf(IsNull(rst2.Fields("FCP")), " AND (FCP) Is Null", " AND (FCP='" & rst2.Fields("FCP").Value & "')")
    rst.FindFirst "(ACC='" & rst2.Fields("ACC").Value & "') AND (Appropriation='" & rst2.Fields("Appropriation").Value & "') AND ([As Of]=#" & rst2.Fields("As Of").Value & "#)" & strFCPParameter
    Forms("Last Year End").Last_Year_End_Obligations.Form.Bookmark = rst.Bookmark
End If
Set rst = Nothing
End Sub

Open in new window

0
Comment
Question by:vincem1099
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 21804637
Why is there an Enter button if you always want to close the form after the entry is made?
0
 
LVL 7

Author Comment

by:vincem1099
ID: 21804835
I was referring to the enter button on the keyboard.  Pressing the enter button on the keyboard lets access know that the field has been updated so the after update event will trigger.
0
 
LVL 77

Accepted Solution

by:
peter57r earned 1000 total points
ID: 21805407
OK I see.  That makes the events processing tricky.

Change the process as follows.

Remove the X close form button.
Add your own form close button using the button wizard.
Modify the code for this button as follows..
if me.dirty = false then
Docmd.close acform, me.name
else
me.dirty=false ' saves record and executes form Afterupdate event - do not close the form here
end if

Modify your existing afterupdate procedure to save the record.
Private Sub FMS_Oblig_AfterUpdate()
me.dirty = false . saves and forces Form-afterupdate event to fire.
End Sub

In the form_afterupdate event..
Docmd.Close acform, me.name

This means that if the record is changed only one form close event will fire.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question