By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,057 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,057 IT Pros & Developers. It's quick & easy.

Access - Return to previous form

P: 4
Hello guys.
In my project when I open a form the previous form is closed..
So I want to make a button that returns me to the previous form and closing the opened form

Example: FormA is open than I clicked a button that opened FormB and closed FormA now I clicked the "Previous" button that brought me back to FormA and closed FormB

I need to make this because I don't have time of making alot of macros
Apr 27 '12 #1

✓ answered by Mihail

In the main form:
Expand|Select|Wrap|Line Numbers
  1. Me.Visible = False
  2. DoCmd.OpenForm "NewFormName", , , , , , Me.Name
In the second form:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim ParentFormName As String
  3.  
  4. Private Sub Form_Load()
  5.     ParentFormName = Me.OpenArgs
  6. End Sub
  7.  
  8. Private Sub Form_Close()
  9. Dim frm As Form
  10.     For Each frm In Application.Forms
  11.         If frm.Name = ParentFormName Then
  12.             frm.Visible = True
  13.             Exit For
  14.         End If
  15.     Next frm
  16. End Sub

Share this Question
Share on Google+
15 Replies


Rabbit
Expert Mod 10K+
P: 12,366
Pass the previous form name in the OpenArgs parameter when you open the form. Then you can use OpenArgs on the current form to open the previous form.
Apr 27 '12 #2

NeoPa
Expert Mod 15k+
P: 31,494
A good idea is to hide forms when they are used to open another form. The calling form being hidden instead of closed allows the called form to return to it exactly as it was left.

An alternative to using OpenArgs is to use Cascading Forms.
Apr 27 '12 #3

P: 4
@Rabbit Can you explain more? Cause I'm not very good with VBA so..
Apr 28 '12 #4

100+
P: 759
In the main form:
Expand|Select|Wrap|Line Numbers
  1. Me.Visible = False
  2. DoCmd.OpenForm "NewFormName", , , , , , Me.Name
In the second form:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim ParentFormName As String
  3.  
  4. Private Sub Form_Load()
  5.     ParentFormName = Me.OpenArgs
  6. End Sub
  7.  
  8. Private Sub Form_Close()
  9. Dim frm As Form
  10.     For Each frm In Application.Forms
  11.         If frm.Name = ParentFormName Then
  12.             frm.Visible = True
  13.             Exit For
  14.         End If
  15.     Next frm
  16. End Sub
Apr 28 '12 #5

NeoPa
Expert Mod 15k+
P: 31,494
Alternatively :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Close()
  2.     Forms(ParentFormName).Visible = True
  3.     Call DoCmd.Close
  4. End Sub
Apr 28 '12 #6

100+
P: 759
Really a much better approach, NeoPa.
But I think that line #3 is not necessary.
Apr 29 '12 #7

P: 4
Thanks guys, it worked fine for me!
Apr 29 '12 #8

NeoPa
Expert Mod 15k+
P: 31,494
Mihail:
But I think that line #3 is not necessary.
You're absolutely correct Mihail. I misread the procedure as a button click (I don't know how/why).
Apr 29 '12 #9

TheSmileyCoder
Expert Mod 100+
P: 2,321
Just adding this on for completeness, in case you like me use many instances of the same form, so that you can't simply rely on the forms name. That said, Mihails bit of code will be just fine for 95% of situations.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Dim frmPrevious As Form
  4.  
  5. Private Sub Form_Open(Cancel as Integer)
  6.   Set frmPrevious=Screen.ActiveForm
  7.   frmPrevious.Visible=False
  8. End Sub
  9.  
  10. Private Sub Form_Close()
  11.   frmPrevious.Visible=True
  12.   frmPrevious.setfocus
  13. End Sub
Apr 29 '12 #10

100+
P: 759
That's cool Smiley !!!!!
Until now I have write a lot of extra-code in order to obtain the same effect.
Thanks !
Apr 30 '12 #11

NeoPa
Expert Mod 15k+
P: 31,494
That's actually very interesting Smiley. I don't work much with multiple instances myself (as I rarely have the need), but I can see the benefit of handling them. I'm a little less convinced about getting this from the Screen object though (which is certainly clever, but not so logically robust). Do form instances not have unique values for their .Name parameters? I would tend to prefer code where the calling form identification was passed from the calling form itself. If that has to be the index value for Forms, then so be it, but if the .Name is unique then I'd go for that instead.

Does that make sense?
Apr 30 '12 #12

P: 4
I have a question..
There is a way I can let the program know which form was the last form? and then just put it on a variable or something?
and than I can return to any form
Apr 30 '12 #13

NeoPa
Expert Mod 15k+
P: 31,494
Yes. There are two methods already included for that in this thread :
  1. Post #5 Line #2 of the first block of code.
    This code passes the name of the calling form to the called form. Smiley has indicated this may not be reliable when multiple instances of the same form are opened concurrently (I cannot confirm this at this time).
  2. Post #10 Line #6.
    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.
omerdn1:
and than I can return to any form
You should appreciate that this is exactly what we've been discussing and suggesting solutions for all this time.
Apr 30 '12 #14

TheSmileyCoder
Expert Mod 100+
P: 2,321
@ NeoPa

No instanced forms do not have unique names. I started using instanced forms while doing a review tool, in which the user would often want to copy/paste comments from 1 review into another review, and thus would like to have multiple reviews open at the same time. It does complicate things a bit sometimes, but its a very nice and handy feature.

Whether you could do it through the form collection and index, I dont really know. I haven't used that so much. A simple bit of error handling to the open and close procedure should take care of most issues, and if you have no frmPrevious to return to, simply open a default menu form. Another issue is that with instanced forms you can't (to my knowledge) pass openargs.
Apr 30 '12 #15

NeoPa
Expert Mod 15k+
P: 31,494
Smiley:
Another issue is that with instanced forms you can't (to my knowledge) pass openargs.
Ah! That does rather put the kybosh on that approach then, for instanced forms at least. Thanks for the info.
May 1 '12 #16

Post your reply

Sign in to post your reply or Sign up for a free account.