Close form; open a new form (1 Viewer)

ClaraBarton

Registered User.
Local time
Yesterday, 19:56
Joined
Oct 14, 2019
Messages
479
Everything I've read says to close the calling form before opening the new form.
Code:
Private Sub btnEdit_Click()
10    On Error GoTo btnEdit_Click_Error
      Dim strWhere As String
20    strWhere = "[RecipeID] = " & Nz(Me.recipeid, 0)
30    DoCmd.Close acForm, Me.name
            
40          DoCmd.OpenForm "frmAddEdit", _
            View:=acNormal, _
            WhereCondition:=strWhere, _
            OpenArgs:=Me.name
50    On Error GoTo 0
60    Exit Sub

btnEdit_Click_Error:
70        MsgBox "Error " & Err.Number & " (" & Err.description & ") " & _
              " in procedure btnEdit_Click, line " & Erl & "."

End Sub
If I don't dim and set the strWhere and the OpenArgs before the form closes I lose them. I can do that or I can move the DoCmd.Close to after the DoCmd.OpenForm. What is the proper way and why?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 19:56
Joined
Oct 29, 2018
Messages
21,581
Depends on what you need to accomplish. If you need information from the first form to use in the second form, it's okay to close the first form after opening the second form.
 

ClaraBarton

Registered User.
Local time
Yesterday, 19:56
Joined
Oct 14, 2019
Messages
479
But sometimes it seems like the opening of the second form bypasses the following code so the first form doesn't get closed.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 19:56
Joined
Oct 29, 2018
Messages
21,581
But sometimes it seems like the opening of the second form bypasses the following code so the first form doesn't get closed.
I don't how that is happening, but you could also put the closing of the first form in the second form's code.
 

Josef P.

Well-known member
Local time
Today, 04:56
Joined
Feb 2, 2023
Messages
866
Code:
Private Sub btnEdit_Click()
   ...
   DoCmd.Close acForm, Me.name        
   msgbox Me.Name   '<--- /Me/ is closed! => Me.Name can no longer be evaluated.
  ...

suggestion:
Code:
Private Sub btnEdit_Click()
10    On Error GoTo btnEdit_Click_Error
      Dim strWhere As String
20    strWhere = "[RecipeID] = " & Nz(Me.recipeid, 0)

        Me.Visible = False

40          DoCmd.OpenForm "frmAddEdit", _
            View:=acNormal, _
            WhereCondition:=strWhere, _
            OpenArgs:=Me.name

     DoCmd.Close acForm, Me.name

50    On Error GoTo 0
60    Exit Sub

btnEdit_Click_Error:
70        MsgBox "Error " & Err.Number & " (" & Err.description & ") " & _
              " in procedure btnEdit_Click, line " & Erl & "."

End Sub
 

ClaraBarton

Registered User.
Local time
Yesterday, 19:56
Joined
Oct 14, 2019
Messages
479
No... I get that. Will all code in the sub complete on the closing form once it's closed? Or does it not close immediately? Well, it does close immediately because the code cannot pick up any info from it after Close.
 

isladogs

MVP / VIP
Local time
Today, 03:56
Joined
Jan 14, 2017
Messages
18,269
But sometimes it seems like the opening of the second form bypasses the following code so the first form doesn't get closed.
That should never be the case. Easily tested as in attached test DB
 

Attachments

  • FormOpenCloseTest.zip
    22.2 KB · Views: 67

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:56
Joined
Feb 19, 2002
Messages
43,587
Everything I've read says to close the calling form before opening the new form.
Depends on your objective and whether or not you are opening the new form as a dialog. So, an arbitrary "rule" is just plain wrong.

There is a very important distinction when the second form is opened as a dialog and when it isn't.

When you open the second form as a dialog, the code running in the first form stops at the OpenForm method and then PICKS UP AGAIN when the dialog form closes. However, if the second form is NOT opened as a dialog, then whatever code is running in the procedure that opens the non-dialog form, continues until the end of the procedure. The first form does not "wake up" once the non-dialog form closes. It remains quiet. The user has to take some other action to make the first form run code.
 

ClaraBarton

Registered User.
Local time
Yesterday, 19:56
Joined
Oct 14, 2019
Messages
479
Thank you, Pat. That is exactly what I wanted to know.
Thanks for the sample, Isladogs. I added it to my stash. :giggle:
 

isladogs

MVP / VIP
Local time
Today, 03:56
Joined
Jan 14, 2017
Messages
18,269
Oops. I completely forgot about dialog forms.
Updated example attached so you can easily compare the behaviour (event sequence and position) when the second form is opened as dialog or as a standard form.
 

Attachments

  • FormOpenCloseTest_v1.3.zip
    23.1 KB · Views: 54

Users who are viewing this thread

Top Bottom