Solved

DoCmd.Close error when closing form

Posted on 2013-01-28
11
912 Views
Last Modified: 2013-01-28
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
Comment
Question by:Patrick O'Dea
  • 4
  • 4
  • 3
11 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38826514
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
 
LVL 61

Expert Comment

by:mbizup
ID: 38826526
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
 

Author Comment

by:Patrick O'Dea
ID: 38826561
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 38826573
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
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38826597
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
 

Author Comment

by:Patrick O'Dea
ID: 38826720
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
 
LVL 61

Expert Comment

by:mbizup
ID: 38826744
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
 

Author Comment

by:Patrick O'Dea
ID: 38826769
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
 
LVL 61

Expert Comment

by:mbizup
ID: 38826838
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
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 250 total points
ID: 38826864
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
 

Author Closing Comment

by:Patrick O'Dea
ID: 38826935
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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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.

803 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