Dynamically moving from one form to another

C

Cevin

I'm trying to build a module that when a user closes a form (say form 2), it
will take them back to the form that called form 2 via a close button on form
2. I want it to work on all my forms, so that no matter what form gets
closed, it will take them back to the original form then close form 2.
I set up two public string variables in the forms. One that contains the
caller form and one that contains the current form. I figgured that when I
pass both of those variables to the module, it will move to the caller form
then close the current form.
It maintains the variables thru the passing from the form to the module, but
when it runs it gives me a 424 Object required error.
How can I use the variables to return to the calling form and then close the
current form?

Thank you in advance for your assistance.
Cevin
 
D

Daryl S

Cevin -

The normal way to do this is to pass in the name of the calling form as the
OpenArgs parameter to the second form. Then when the second form closes, use
the OpenArgs to know which form to open back up. Look up help on OpenArgs
for this.
 
A

Albert D. Kallal

While we wait for some others to perhaps come up with some code examples,
I'm going to suggest that you solve this problem without any code at all.

What I do in my applications is simply set the form's property as modal,
what this means then that if you have one form and it launches another form,
the user must close the current form (just launched) to get back to the
previous calling form. They will be unable to get back to the previous form
until they close the existing form they're working on.

Because my applications are quite rich from a user interface point of view,
I tend to have quite a few forms, and therefore users can get lost if I
don't control their pathway that they go down when they launch form after
form.

So it's very common to want to control and make sure the user returns from
the calling form. If you set " modal" in the other tab of the property sheet
for a form correctly, then you'll not need any code at all.

Hence, a form as modal, then the only choice the user has is to launch more
forms from that form, or close the current form to return back to the
previous calling form. Users will be unable to jump or side tracked or skip
out of the current form unless they close it. Keep in mind that modal forms
behave significantly different than that of dialog forms.

So perhaps in place of writing all that code, consider using the modal
property of the form to correctly control the form flow in your application:
that's exactly why that feature is available in Access.

I would say perhaps about 80 perhaps even 90% of all my forms are modal for
the above reason.

And once again, I stress don't confuse these modal forms with that of
dialogue forms.
 
C

Cevin

Daryl & Albert, thank you for your response.

I can get the name of the first form to the second form using OpenArgs. The
problem I'm having is getting the first form visible and having the focus. I
can't get the OpenArgs variable to be recognized in the code line to switch
back to the first form.

I agree that there are many uses for a modal form, but not for every form in
the application I'm building.
I don't always want to bring the application to a halt when a form is open.
For instance, the user may be using a form to do some data entry which may
take 10 to 15 minutes to enter and verify the data before saving it to a
table. In that time say one of our road techs calls for a phone number for a
vendor. I'd like the user to be able to switch over to the applications
'switchboard', taking them to another form/report to look up the phone number
without having to lose the data entry info by closing a modal form. I
figgured that by keeping the calling form with the open form, if the user has
a couple forms open, it will always guide them back up the same path that a
particular form started from, and still let them be able to multi-task.

My ultimate goal (maybe a bit lofty) is have the application able to do many
things like above. Utilizing tabs the 'switchboard' tab would always be
available so the user could multi-task to look up a price, a phone number,
etc.
Although, as Albert said, I'd also like to sometimes use modal form(s) to
steer a user down a pathway for other tasks so they MUST complete the entire
path before ending (to maintain referential integrity).
If there is a way to do both modal forms and multitask ability, it would
certainly be helpful. Also if there is a better way than what I'm thinking,
I'd like to hear it.

Thanx again,
Cevin
 
B

BruceM via AccessMonster.com

Please post the code. Specifically, how are you trying to reopen the first
form?
 
C

Cevin

Thanx for all the help I received. Due to your suggestions, I was able to
get the application to do what I wanted it to do.
Below is the code that is a seperate module (basCloseWindow) and is called
whenever the user closes a window. It does 3 things:
-It tests to be sure the first object is still loaded.
-It takes them back to the first object (report or form) that they opened
the second
object from.
-It then closes the second object.

The 2 variables are sent to the module from the closing form. They are
arrived by:
-The strCallerWindow is passed to the second object, from the first object,
when
opened via OpenArgs.
-The strCurrentWindow is set when the second object opens by a hard coded line
with the objects name.

During the development, I realized I also had to allow for the chance that
the user may have closed the first form, which would leave them with a blank
window. So there is code to test to be sure the first object is still loaded.

Thanx again for all your help. I know I'll be reading this group a lot and
sure I'll be posting more questions.

Cevin

'*********************************************************
'* Procedure to close the current window and return user to previous window *
'*********************************************************
Option Compare Database
Option Explicit

Public Sub CloseWindow(strCallerWindow, strCurrentWindow)

On Error GoTo Err_CloseWindow
'Determine if the object to return to is loaded
' Returns True if the specified form is open in Form view or Datasheet view.
Dim oAccessObject As AccessObject
'Determine if the object to return to is a form or report
If Left(strCallerWindow, 3) = "frm" Then 'it is a form
Set oAccessObject = CurrentProject.AllForms(strCallerWindow)
Else 'it is a report
Set oAccessObject = CurrentProject.AllReports(strCallerWindow)
End If
If oAccessObject.IsLoaded Then 'calling object is loaded
'Object is loaded - do nothing
Else
'Object is NOT loaded - load it
If Left(strCallerWindow, 3) = "frm" Then 'it is a form
DoCmd.OpenForm strCallerWindow, acNormal, , , _
acFormPropertySettings, acWindowNormal
Else 'it is a report
DoCmd.OpenReport strCallerWindow, acViewNormal, , ,
acWindowNormal
End If
End If
'Move focus to the calling object
'Determine if the object to return to is a form or report
If Left(strCallerWindow, 3) = "frm" Then 'it is a form
DoCmd.SelectObject acForm, strCallerWindow, True
Else 'it is a report
DoCmd.SelectObject acReport, strCallerWindow, True
End If
'Close the current form
'Determine if the object to close is a form or report
If Left(strCurrentWindow, 3) = "frm" Then 'it is a form
DoCmd.Close acForm, strCurrentWindow, acSaveNo
Else 'it is a report
DoCmd.Close acReport, strCurrentWindow, acSaveNo
End If
'No errors so exit the subroutine
Exit Sub

'Error Handling
Err_CloseWindow:
'Display error message for any errors encountered
MsgBox Err.Number & ", " & Err.Description, vbCritical + vbOKOnly, "Notify
Administrator"
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top