Solved

DoCmd.Close error when closing form

Posted on 2013-01-28
11
866 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now