• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1386
  • Last Modified:

DoCmd.Close error when closing form

DoCmd.Close acForm, Me.Name

The above line error works in one part of my code but NOT in another.

The intention of the code is simple to close the current form.
It is part of my overall error handling process which I am working on.

To demonstrate;
1. click on form frmWeeks.
2. Then make and entry in the "weeksdescription" contol.
3. Then click exit.
4. There will be a error because of division by zero.  (ignore this).
5. Then there will be an error on the line "DoCmd.Close acForm, Me.Name"

Why am I getting an error on this line.  (It is fine elsewhere).
EnnisBudgetActuals.accdb
0
Patrick O'Dea
Asked:
Patrick O'Dea
  • 4
  • 4
  • 3
2 Solutions
 
Dale FyeCommented:
I cannot download your file at work, but if this is in some type of global error handling procedure, then the ref to ME will not work, you need to identify the specific form that you want to close.

Something like:

Dim frm as Form
set frm = Screen.ActiveForm
docmd.close acForm, frm.name
0
 
mbizupCommented:
I can't open your file, but does the following work?

DoCmd.Close

Open in new window


WHat is the specific error message that you are seeing?
0
 
Patrick O'DeaAuthor Commented:
Error is 2501.
The me.Name does display the current form.

There is an irony here, as follows;
I have planted a line of code x=1/0.
This is intended to generate an error (and in turn test my new error loggin routine).

When I "return" from the error logging routine I want to close my current form.
This is when I get error 2501 (i.e. when trying to close form).

And NO, DoCMD.Close gives the same error.


(Also, this line DID seem to work elsewhere in my code.  But is is failing in both locations now).
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mbizupCommented:
Error 2501 is typically something along the lines of "OpenForm was cancelled"

Are you closing this form during  its open or similar event?  This will always cause error 2501.

If so, you need to include error handling in whatever code is opening your form to handle this error smoothly (in general this error can be ignored if you have conditional code closing the form during an open event).

Something along the lines of:

On Error Goto EH

Docmd.OpenForm "YourFormName"

Exit Sub

EH:
  if err.Number = 2501 then Resume Next
End Sub

Open in new window

0
 
Dale FyeCommented:
Miriam (mbizup) offers up a good point.  If you are testing x = 1/0 in the form open event, and trying to close the form in that event, then you should just be able to set the Cancel argument to True.

If you want to test your error handler, then put the x=1/0 code in the Click event of a command button.
0
 
Patrick O'DeaAuthor Commented:
Thanks all for advice which has helped me make progress.

It appears that my Docmd.Close is trying to close a form that is ALREADY closed.

So, by simply removing the Docmd.Close the error 2501 vanishes!

However, I cannot figure out why my form is closing in the first place.

In the meantime, I attach the latest version of the database.

Bottom Line: There is not a single "close" command in all my VBA.  So why is my form closing?

Reminder: click frmWeeks and change a description before exiting.

There is not much VBA and the three relevant bits of code a prefixed by "*****" (to make searching for text easy).
EnnisBudgetActuals.accdb
0
 
mbizupCommented:
Humm.  Again, can't open your database.  Can you post the relevant code in code snippets, including your test line with the Division by zero?

As an aside, ANY unhandled error in the open event of a form will cause it to not open, which in turn will raise error 2501 in the procedure that opens the form... so if your division by zero is in the Open Event, you will see error 2501 and the form will not open - even if you do not have an explicit close statement.
0
 
Patrick O'DeaAuthor Commented:
Thanks mbizup for your persistence!  Note lines 60 and 90 in the first section of code.
Bear in mind that everything works fine now ... I just cannot see where my form is being closed!

(Should I be concerned that you cannot open my database?)

Private Sub Form_BeforeUpdate(Cancel As Integer)
      Dim FieldNames()
          Dim TableName As String
          Dim UniqueID As Long
              
10    On Error GoTo Form_BeforeUpdate_Error

20        TableName = "Weeks Maintenance "
30        UniqueID = WeekNumber
          
40        FieldNames = Array(WeekDescription, ActiveFlag, YearPeriod, WeekInPeriod)
          
50        RecordAudit TableName, UniqueID, FieldNames

'******

60    x = x / 0

70    On Error GoTo 0
80       Exit Sub

Form_BeforeUpdate_Error:
'******
90    AuditMisc OpenArgs(), "ErrorFound"

End Sub

Open in new window





THE FOLLOWING WRITES TO AUDIT TABLE

Public Function AuditMisc(WhatHappened, SourceOfCall)

'******

Dim strSQL As String
Dim Dummy1 As String

If SourceOfCall = "ErrorFound" Then
    WhatHappened = "ATTENTION  Error:" & Err.Number & " " & "   Line:" & Erl & "-->" & Err.Description & " " & WhatHappened
    MsgBox "Check Audit : " & WhatHappened
End If



strSQL = "Insert into tblAuditDetail(ActionTime, TableName, FieldName,UniqueID,OldValue,NewValue,ActionUser) values ("
strSQL = strSQL & "#" & Format(Now(), "mm/dd/yyyy hh:mm:ss") & "#" & ","
strSQL = strSQL & "'" & WhatHappened & "'" & ","
strSQL = strSQL & "'" & Dummy1 & "'" & ","

strSQL = strSQL & "'" & Dummy1 & "'" & ","
strSQL = strSQL & "'" & Dummy1 & "'" & ","
strSQL = strSQL & "'" & Dummy1 & "'" & ","

strSQL = strSQL & "'" & getUserName & "'" & ")"

CurrentDb.Execute strSQL

End Function

Open in new window

0
 
mbizupCommented:
I cant spot anything obvious in the code you posted.  Is that all of the code you have in the form (no Open Event?) ?

-->> (Should I be concerned that you cannot open my database?)
Not really.  Was the database created in Access 2010?  Even though some Access 2010 databases claim they are in Access 2007 format, these databases cannot be opened in Access 2007 (unrecognized database format).

There are undoubtedly others who have Access 2010 who can look into this, or I can check in later today using A2010.
0
 
Dale FyeCommented:
You might want to change:

CurrentDb.Execute strSQL

to

CurrentDb.Execute strSQL, dbFailOnError

to see if the SQL string you are building is causing a problem.
0
 
Patrick O'DeaAuthor Commented:
Thats it!!

Thanks fyed, the SQL was slightly faulty and the dbFailOnError highlighted this.

(Although, the problem seemed to manifest itself in a peculiar way..).


Special thanks to mbizup for assistance too.

....I'm learning ....!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now