Solved

DoCmd.Close error when closing form

Posted on 2013-01-28
11
1,008 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
[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
  • 4
  • 4
  • 3
11 Comments
 
LVL 48

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 48

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 48

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

707 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