Return to previously open form (1 Viewer)

dgreen

Member
Local time
Yesterday, 18:04
Joined
Sep 30, 2018
Messages
397
Due to the amount of data I'm opening on a single form, I must close forms before opening up the next form. The below code appears to require the form to be open, just not visible.

Does someone have a solution that captures the previous form, stores it in memory and makes it available from the active form to be used to return to the previous form?

Code:
Option Compare Database
Option Explicit
'https://bytes.com/topic/access/answers/939693-access-return-previous-form
'This code determines the active form (with the fairly reliable assumption
'that this will be the same as the form that called it) when the Form_Open()
'procedure is run. It can play tricks when the form is opened from the database
'window, and maybe in other scenarios I haven't considered, but is otherwise
'pretty reliable.
Dim frmPrevious As Form

Private Sub Form_Open(Cancel as Integer)
Set frmPrevious=Screen.ActiveForm
frmPrevious.Visible=False
End Sub

Private Sub Form_Close()
frmPrevious.Visible=True
frmPrevious.setfocus
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:04
Joined
Oct 29, 2018
Messages
21,469
Hi. Have you considered using TempVars?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 16:04
Joined
Aug 30, 2003
Messages
36,125
One option would be to pass the name of the current form to the new form in OpenArgs, then you can use that to open it again.

You might also consider a tab control and keep all the controls on one form.
 

isladogs

MVP / VIP
Local time
Today, 00:04
Joined
Jan 14, 2017
Messages
18,217
As already suggested, in this situation I use OpenArgs to open the second form.
Then when closing the second form, I use a Select Case statement similar to that below

Code:
Select Case Me.OpenArgs

Case "Form1"
Do.Cmd OpenForm "Form1"

Case "Form2"
DoCmd OpenForm "Form2"

End Select

DoCmd.Close acForm, Me.Name
 

Users who are viewing this thread

Top Bottom