Close form; open a new form (1 Viewer)

ClaraBarton

Registered User.
Local time
Today, 02:30
Joined
Oct 14, 2019
Messages
463
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
Today, 02:30
Joined
Oct 29, 2018
Messages
21,473
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
Today, 02:30
Joined
Oct 14, 2019
Messages
463
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
Today, 02:30
Joined
Oct 29, 2018
Messages
21,473
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, 11:30
Joined
Feb 2, 2023
Messages
826
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
Today, 02:30
Joined
Oct 14, 2019
Messages
463
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, 10:30
Joined
Jan 14, 2017
Messages
18,224
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: 50

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:30
Joined
Feb 19, 2002
Messages
43,275
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
Today, 02:30
Joined
Oct 14, 2019
Messages
463
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, 10:30
Joined
Jan 14, 2017
Messages
18,224
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: 39

Users who are viewing this thread

Top Bottom