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

Returning to all previous forms

P: 12
Hello guys,
In my project, I have a form named frmDetails which can be accessed by a number of forms like frmAdmin, frmStaff etc. My problem is I want to return to the previous form (may be frmAdmin or frmStaff) from which I accessed frmDetails.
For this, I want to put a button btnBack on the form frmDetails.
Thank you in advance :)
Jun 5 '17 #1

✓ answered by PhilOfWalton

I presume you have a command button on your frmAdmin, frmStaff etc. to open FrmDetails.

So the code for the command button should be
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdOpenDetails_Click()
  2.  
  3.     DoCmd.OpenForm "FrmDetails", , , , , , Me.Name
  4.  
  5. End Sub
  6.  
This passes the name of the calling form (FrmAdmin or FrmStaff) to the FrmDetail.

The relevant code in the FrmDetails is

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Dim Args As String
  5.  
  6. Private Sub Form_Open(Cancel As Integer)
  7.  
  8.     Args = Me.OpenArgs
  9.  
  10. End Sub
  11.  
  12. Private Sub Form_Close()
  13.  
  14.     DoCmd.OpenForm Args
  15.  
  16. End Sub
  17.  
So on opening the FrmDetails, it gets passed the information of the calling form, and on closing the FrmDetails it opens the name it was passed.

Phil

Share this Question
Share on Google+
5 Replies


PhilOfWalton
Expert 100+
P: 1,430
I presume you have a command button on your frmAdmin, frmStaff etc. to open FrmDetails.

So the code for the command button should be
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdOpenDetails_Click()
  2.  
  3.     DoCmd.OpenForm "FrmDetails", , , , , , Me.Name
  4.  
  5. End Sub
  6.  
This passes the name of the calling form (FrmAdmin or FrmStaff) to the FrmDetail.

The relevant code in the FrmDetails is

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Dim Args As String
  5.  
  6. Private Sub Form_Open(Cancel As Integer)
  7.  
  8.     Args = Me.OpenArgs
  9.  
  10. End Sub
  11.  
  12. Private Sub Form_Close()
  13.  
  14.     DoCmd.OpenForm Args
  15.  
  16. End Sub
  17.  
So on opening the FrmDetails, it gets passed the information of the calling form, and on closing the FrmDetails it opens the name it was passed.

Phil
Jun 5 '17 #2

P: 12
Thank you. That was short and to the point.
It works fantastic with the scenario I provided earlier. But, when I want to link frmAdmin and frmStaff (going back to each other), it always tells me "Invalid use of null" and the statement Args = Me.OpenArgs is highlighted. I wrote the following code. Please correct me where I am going the wrong way.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Dim Args As String
  4.  
  5. Private Sub goToStaff_Click()
  6. DoCmd.OpenForm "FrmStaff", , , , , , Me.Name
  7.  
  8. End Sub
  9.  
  10. Private Sub goTodetai_Click()
  11.  DoCmd.OpenForm "FrmDetails", , , , , , Me.Name
  12. End Sub
  13.  
  14. Private Sub Form_Open(Cancel As Integer)
  15.  
  16.     Args = Me.OpenArgs
  17.  
  18. End Sub
  19.  
  20. Private Sub Form_Close()
  21.  
  22.     DoCmd.OpenForm Args
  23.  
  24. End Sub
Jun 8 '17 #3

PhilOfWalton
Expert 100+
P: 1,430
Very odd

I must confess that it should say

Expand|Select|Wrap|Line Numbers
  1. Args = Nz(Me.OpenArgs)
  2.  
I am at a loss to know why it isn't working......

I Know

The
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.  
  3.     Args = Nz(Me.OpenArgs)
  4.  
  5. End Sub
  6.  
Should be the open event on both the FrmDetails and FrmStaff. That way, you "Pass" the name of the calling form to te form being opened.

Equally the Close event should be on both the FrmDetails and FrmStaff. Here you open the form whose name has been passed to it when you opened the form.

I have a feeling that all the code is on your main form

Phil
Jun 8 '17 #4

NeoPa
Expert Mod 15k+
P: 31,494
In short, all Forms that you open with this method should have the code in their Form_Open() & Form_Close() event procedures to handle the Args string value and any form that is opening another form (or report indeed) should have the code that passes its name in the OpenArgs parameter.

I would actually do it slightly differently, as I tend simply to hide and unhide the form that opens another one. So, if frmA is the form that's open to start with, and it calls frmB, then the code would be as below :
frmA
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cmdOpenB_Click()
  5.     Call DoCmd.OpenForm(FormName:="frmB", OpenArgs:=Me.Name)
  6. End Sub
frmB
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private strCaller As String
  5.  
  6. Private Sub Form_Open(Cancel As Integer)
  7.     strCaller = Nz(Me.OpenArgs, "")
  8.     If strCaller > "" Then Forms(strCaller).Visible = False
  9. End Sub
  10.  
  11. Private Sub Form_Close()
  12.     If strCaller > "" Then Forms(strCaller).Visible = True
  13. End Sub
Notice, with the hiding code being in the called form, the calling form doesn't have to know if the called form has been coded to handle hiding or not. As long as each form has either no code or both sets of code (In Form_Open() as well as Form_Close()) then it will work consistently. Equally, when a called form is opened directly from the database window, it won't have any issues trying to make the calling form visible again.

NB. Hiding is preferable in most cases as it doesn't lose any of its settings and won't need to get the data again if it has any. It also won't need to go through the opening sequence. That last bit's usually insignificant but unnecessary is unnecessary ;-) Also consider a form used for filtering that opens a selected item. When you return to that form you generally prefer the same criteria to be set in order to allow you to continue seamlessly.
Jun 8 '17 #5

P: 12
Thank you so much guys. my problem is solved with the Nz command. thank you for your time and effort
Jun 11 '17 #6

Post your reply

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