Close current form & Open another Forms on button click

djossh

Registered User.
Local time
Today, 20:20
Joined
Oct 19, 2011
Messages
89
I have a Form (Form1) with 5 buttons (Button A, B, C, D & E) which opens Form A, B, C, D & E Respectively... I want the current Form to close everytime I click on the other button..

TAKE NOTE: Form1 should be visible at all times only the form A,B,C,D & E should close and open on button click.

Thanks in advance for any help...
 
Code:
If CurrentProject.AllForms("FormToClose").IsLoaded = True then
  DoCmd.Close acForm, "FormToClose", acSaveNo
end if
 
The current form is Me.Name, so:
Code:
DoCmd.Close acForm, [B]Me.Name[/B], acSaveNo
DoCmd.OpenForm ... etc
 
@vbaInet - I guess he don't know which forms are open, as he don't know which button was clicked before the current one.

If you don't want to look for all forms to check which ones are open you can have an hidden text box on the form (Or private variable) that will save the form that was opened before so you know which form to close.
 
The current form is Me.Name, so:
Code:
DoCmd.Close acForm, [B]Me.Name[/B], acSaveNo
DoCmd.OpenForm ... etc

I tried it, but it is also closing my Form1.. I was actually creating a navigation style forms that's why I wanted my Form1 to be visible at all times since all buttons are located in Form1.

Form1 houses my buttons to open Forms.. any selection from this should close the Current form EXCEPT the Form1. and Opens my new forms which is selected from the buttons in Form1. thanks
 
@vbaInet - I guess he don't know which forms are open, as he don't know which button was clicked before the current one.
I see. So the OP can save the name of the form just opened in the Tag property of Form1 and close that form if the property has a value. E.g.
Code:
If Len(Me.Tag) <> 0 Then
    DoCmd.Close acForm, Me.Tag, acSaveNop
End If
DoCmd.OpenForm "[COLOR=Blue]FormName[/COLOR]", ... etc
Me.Tag  = "[COLOR=Blue]FormName[/COLOR]"
 
I see. So the OP can save the name of the form just opened in the Tag property of Form1 and close that form if the property has a value. E.g.
Code:
If Len(Me.Tag) <> 0 Then
    DoCmd.Close acForm, Me.Tag, acSaveNop
End If
DoCmd.OpenForm "[COLOR=blue]FormName[/COLOR]", ... etc
Me.Tag  = "[COLOR=blue]FormName[/COLOR]"

I tried it.. it was working fine.. but I think it is not closing the Current open form, instead it is overlaping the current open forms. can you please check the code? THANK YOU for the help.. I Really appreciate it..
 
Empty what's in the Tag property of Form1 and save. Close Form1 and all the other forms and start again.
 
Glad we could help!

Hi vbaInet.. I have one more favor to ask hope you could help me.. I have this code below which is working fine.. Can you please help me how to add code that will close the current open form when I select another value in my combo box.. I want to get same results as we did in my previous post but now using combobox instead of a button.. Thanks..

Here's my code: (Sorry I really don't know how to write codes, I just copied it from other forums.. Thanks again)

Private Sub Addcombo_AfterUpdate()
If Addcombo.Value = "employee" Then
DoCmd.OpenForm "FrmEmployee"
ElseIf Addcombo.Value = "Passport" Then
DoCmd.OpenForm "FrmPassport"
ElseIf Addcombo.Value = "ID" Then
DoCmd.OpenForm "FrmID"
ElseIf Addcombo.Value = "Salary" Then
DoCmd.OpenForm "FrmSalary"
End If
End Sub
 
So copy the code we gave you and use it as the substitution for the DoCmd.OpenForm code line.
 
So copy the code we gave you and use it as the substitution for the DoCmd.OpenForm code line.


Sorry, I'm really confused how would I do that.. can you please give me a sample code.. Thanks
 
Substitution of code doesn't require any sample code to be given. It is a simple matter of replacing one line of code by the block of code I gave earlier (as explained in my last post).

I will re-iterate, replace the DoCmd.OpenForm line in the code you found online with the block of code I gave. After doing that, replace the form name with the appropriate form name for each section.
 

Users who are viewing this thread

Back
Top Bottom