Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

closing previous form

Status
Not open for further replies.

cvaccess

Technical User
Jun 26, 2002
55
US
What is the easiest way to close a previous form when opening a new form?

I have tried macro and coding and both do not work for me. In the macro I first close the form then open the new form. Why doesn't this work?

In coding/modules, this is my code:

Private Sub Check16_GotFocus()
On Error GoTo Err_Check16_GotFocus

Dim Form1Name As String
Dim form2name As String
Dim LinkCriteria As String

Form1Name = "Selecting TP Variations"
form2name = "TP Analysis Selection"
DoCmd.OpenForm Form1Name, , , LinkCriteria
DoCmd.Maximize
DoCmd.Close acForm, form2name

Exit_Check16_GotFocus:
Exit Sub

Err_Check16_GotFocus:
MsgBox Error$
Resume Exit_Check16_GotFocus
End Sub

Why doesn't this work?

Thank you for your help.

 
Hiya,

I think that this is down to where the focus is.

Look at the help on the .setfocus method (VBA).

Set focus to the form you want to close and say:

docmd.close

immediately after.

Regards,

Darrylle

"Never argue with an idiot, he'll bring you down to his level - then beat you with experience."
 
If you want to open any one of several forms, in no particular order, and you want the previous form to close each time a new form is opened add the current form's name in the OpenArgs position of the DoCmd.OpenForm method.

Next, in each form's Open event add the code:

If Not IsNull(OpenArgs) Then
DoCmd.Close acForm, OpenArgs
End if

This way the previous form will close what ever its name.

Rod
 
Hiya,

Thanks Rod - clarified the openargs argument for me.

Regards,

Darrylle "Never argue with an idiot, he'll bring you down to his level - then beat you with experience."
 
Seems to me like you misspelled form2name.
To make sure you didn't:
Go to Forms tab in Database window
Click the name of the form whose name you want to get.
Click it again (it will be in Edit/rename mode)
Press Ctrl+C
Go back to your module.
Place the cursor where you want the name pasted.
Press Ctrl+V

Dan

P.S. Darrylles: the syntax 'DoCmd.Close acForm, form2name' does not require the form to have the focus. What's funny is that it does not trigger any error if there is no form open :)
 
Rod - This didn't work. Please let me know if I did it wrong. The current form TP Analysis Selection remained open.

Private Sub EDIPendsCheckbox_GotFocus()

DoCmd.OpenForm "Selecting TP Variations", , , , , , "TP Analysis Selection"
If Not IsNull(OpenArgs) Then
DoCmd.Close acForm, OpenArgs
End If
End Sub

Any help would be appreciated.=)

Darrylle - I did verify that the form2name was not misspelled.Thanks.
 
Look into the VISBLE/INVISIBLE properties of a form. This is how I often do it:

Form X
Me.visible = false
DoCmd.OpenForm "frmY"

Form Y
Forms!FormX.Visible = true
DocCmd.Close , FormY

There are schools of thought that suggest you OPEN every form in a database right off the bat, in HIDDEN (invisible) mode, and visible/invisible them as needed...

Just remember that "CLOSING" a form immediately halts any code executing in it, and issuing an un-parameterized CLOSE command will close the current object, even if it's the form that just opened...

IOW, this doesn't work right:

DoCmd.OpenForm "formX"
DoCmd.Close

You think you'll be closing the "calling" form, but you'll close "FormX" instead.

Jim











Me? Ambivalent? Well, yes and no....
Another free Access forum:
More Access stuff at
 
Hi again,

Thats why I said 'set focus' to calling form and THEN use docmd.close

Regards,

Darrylle "Never argue with an idiot, he'll bring you down to his level - then beat you with experience."
 
Your problem is because you are ruuning your code from the "GotFocus" of a control on your FIRST form (why you are doing that I can't understand).

If you MUST (or want) to have this code run at the OnFocus of 'Check16', do it this way:

(i) Cut ALL of your existing code from behind the OnFocus event of Check16, and Paste it back into the OnTimer event of the (actual) Form itself

(ii) Back at the OnFocus event for Check16 (which should NOW be empty, simply put :

Me.TimerInterval = 500

Now, test your system. What now happens is that half a second (500) after your Check16 gets the focus, the code behind the OnTimer event for your form (i.e. your original code) runs and everything is fine.

No other change is necessary.
 
Archery,

Thank you. This worked. I have to use the got focus since it is a checkbox function I am using. I do not have the option to use on click like on the command function or other features. Also, Checkbox is preferred by the user.

Thanks for the help all.=)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top