Run Time Error 2585 - Can't Close Form (1 Viewer)

Steve R.

Retired
Local time
Today, 16:56
Joined
Jul 5, 2006
Messages
4,617
I am testing for the highly improbable situation where I have ZERO records to display. I have a pending form that lists all PENDING PROJECTS. When clicking on a project number an edit form pops-up. One of the things that the edit form allows is project completion. When the edit form completes it returns the user to the PENDING PROJECTS form. In the event that the user has just completed the last project the PENDING PROJECTS form would have no records to display and should be automatically closed.

When returning to the PENDING PROJECTS form, the activate event is triggered. However, I get the "Run time Error 2585" message that the closing the form can't be carried while processing the form. I tried
Code:
DoCmd.Close acForm, Me.Name, acSaveNo
but that did not work.

I tested for the "BeforeUpdate" event, but that was not triggered since the "Activate" event appears to be the only event being triggered.

Two options seem to present themselves, one is to trap for this "error" on the Forms "OnError" event. The other would be to count the number of records remaining when the edit form is closed and if the record count is zero to close the PENDING PROJECTS form from the edit form rather than returning to the PENDING PROJECT form.

Any thoughts on the "best" way to do this?
 

Steve R.

Retired
Local time
Today, 16:56
Joined
Jul 5, 2006
Messages
4,617
Didn't work. I also tried "On Error", but that didn't work either. The VBA code posted by Thackray is beyond me at the moment.

For now, I will take the approach of using the edit form's "Close Event" to test the number of records and to test if the PROJECT PENDING form is open. If there are zero pending records, I will close the PROJECT PENDING form from the edit form.

Usually there are always 10+ projects working so the possibility of having zero is very remote, but I just happened to have some free time to look into this potential problem.
 

wazz

Super Moderator
Local time
Tomorrow, 04:56
Joined
Jun 29, 2004
Messages
1,711
When returning to the PENDING PROJECTS form, the activate event is triggered.
maybe this is just too soon, ie. the update from the 'edit' form isn't finished. the link i posted suggested adding DoEvents before carrying on with the rest of the code (closing). it didn't work for him, he had to work around something else but maybe it would work here?

otherwise, i'm wondering how you open the edit form. maybe try something like this:

- on the pending form:
'open the edit form in dialog mode. (this will suspend the code here in the pending form. code will resume when the edit form is done (closed)).
'code resumes ...
'requery (me.requery)
'count records and close if necessary.

not sure this will get around the onactivate problem.
 

Steve R.

Retired
Local time
Today, 16:56
Joined
Jul 5, 2006
Messages
4,617
This appears to do the trick. The edit form's "CLOSE" event closes the PENDING PROJECT form and returns the user directly to the main menu.

Code:
Private Sub Form_Close()
    Dim RecordCountInt As Integer
    RecordCountInt = DCount("[projectnum]", "projectnumqry", "[status] < 10 ")
    If RecordCountInt = 0 And IsLoaded(Me.CallingFormNamestr) Then
            DoCmd.Close acForm, Me.CallingFormNamestr, acSaveNo
        Else
            Rem Do nothing
        End If
    Exit Sub
End Sub

Thanks to DCrake for the post on how to use "IsLoaded".

I used "OpenArgs" to pass the name of the calling form to the edit form to eliminate the need to hard code the form name.

Wazz: Thanks for the advice, I had tried "DoEvents" in various places, but it didn't help. I had not heard of "DoEvents" before. The post you referred me to, is going in the correct direction, I just don't understand it for now. But it looks to be very valuable advice as I have run into issues such as this one because I don't fully understand when certain events are left open or are completed.
 
Last edited:

Users who are viewing this thread

Top Bottom