Stop in Close and Open userforms _VBA - excel

I created two forms. Pressing the button 1 opens the form number 2. By closing the form number 2, the form number 1 is displayed. But this action is only done once and it stops for the second time and almost does not work. Where does the code have a problem?
code Userform1:
Private Sub ShowUserform2_Click()
UserForm1.Hide
Unload UserForm1
UserForm2.Show
End Sub
Code userform2:
Private Sub UserForm_Terminate()
UserForm2.Hide
Unload UserForm2
UserForm1.Show
End Sub

Skip the formName.Hide lines. They are unnecessary.
After the Unload formName statements add:
Set formName = Nothing
Also, make the otherForm.Show line precede the above two lines.

Try this code:
code Userform1:
Private Sub ShowUserform2_Click()
UserForm1.Hide
UserForm2.Show
End Sub
Code userform2:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
UserForm1.Show
End Sub

Related

How to show a Userform after closing a second one with "X" button (Terminate Event breaks code)

I have 2 Userforms, Userform1 and Userform2. I would to like to code Userform2 so that when the user closes it using the X button on the top right corner, it closes and Userform1 is automatically opened. The user can go from Userform1 to Userform2 with a CommandButton that has the following code:
Private Sub CommandButton1_Click()
Unload Me
UserForm2.Show
End Sub
After closing Userform2, I managed to automatically open Userform1 with the following code in it:
Private Sub UserForm_Terminate()
UserForm1.Show
End Sub
The problem is that this only works once. The second time the user clicks CommandButton1, Userform1 is closed and Userform2 showed, but Excel hangs up. The only way to fix it is to stop all the code from the developer environment or force quit Excel.
Using the UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) event isn't possible because that way, Userform2 never reaches the Unload part and is just kept in the background while Userform1 launches again, like this:
There is no need to unload UserForm1. Simply hide it. Also there is no need to show UserForm1 from UserForm2. It will automatically get shown when UserForm2 unloads. Is this what you are trying?
In a module
Option Explicit
Sub Sample()
Dim frmOne As New UserForm1
frmOne.Show
End Sub
In Userform1
Option Explicit
Private Sub CommandButton1_Click()
Me.Hide '<~~ Hide the form
Dim frmTwo As New UserForm2
'~~> Show the form in Modal(default)
frmTwo.Show
'~~> Show UserForm1 when UserForm2 unloads
Me.Show
End Sub
EDIT
Thank you for taking the time to help me! Unfortunately this is not what I am looking for. I'm specifically trying to unload Userform1 when showing Userform2 and viceversa because I don't want/need to preserve any data. I know manually clearing the form would also work, but the other thing is that this is only an example. My real code has many forms and I don't want all of them to remain loaded in the memory. That could potentially cause some performance issues. – Fernanda 10 mins ago
This doesnt crash for me
In a module
Option Explicit
Sub Sample()
Dim frmOne As New UserForm1
frmOne.Show
End Sub
In Userform1
Option Explicit
Private Sub CommandButton1_Click()
Unload Me
Dim frmTwo As New UserForm2
frmTwo.Show
End Sub
In Userform2
Option Explicit
Private Sub UserForm_Terminate()
Dim frmOne As New UserForm1
frmOne.Show
End Sub

I created a "Back" button in Userform2 to display Userform1 but I get the error: Run Time Error 400 Form already displayed cannot modally

Workbook code:
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub
Userform1 "Next" button Code:
Private Sub CommandButton1_Click()
UserForm2.Show
Unload UserForm1
Exit Sub
Userform2 "Back" button Code:
Private Sub CommandButton2_Click()
UserForm2.Hide
UserForm1.Show
End Sub
Move the unload Userform1 above the Userform2.Show as displaying a new userform will pause the unload code form running. Alternatively add vbmodeless after Userform2.show to avoid this issue.

My excel userform becomes unresponsive the next time I load it

I have two forms, a login form to authenticate users to enter the system and a main form to work with if the authentication is successful. I work with an access database to search for the valid users and also to populate lists in the main form.
And here is the code for that:
Private Sub CancelCommandButton_Click()
CloseDatabase
Unload Me
End Sub
Private Sub ConfirmCommandButton_Click()
...
End Sub
Private Sub UserForm_Initialize()
ConnectDatabase
End Sub
Private Sub OpenMainForm()
Unload Me
MainForm.Show
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
End If
End Sub
And here is the code for managing main form load-up and close:
Public Sub UpdateControls()
PopulateUserList
End Sub
Private Sub UserForm_Activate()
sTag = Split(Me.Tag, "|")
If sTag(0) <> "1" Then
Me.MainFormMultiPage.Pages(0).Enabled = False
Me.MainFormMultiPage.Value = 1
Else
Me.MainFormMultiPage.Pages(0).Enabled = True
Me.MainFormMultiPage.Value = 0
UpdateControls
End If
UserLabel1.Caption = sTag(1)
UserLabel2.Caption = sTag(1)
UserLabel3.Caption = sTag(1)
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
' Tip: If you want to prevent closing UserForm by Close (×) button in the right-top corner of the UserForm, just uncomment the following line:
' Cancel = True
Dim answer As Integer
answer = MsgBox("ÂíÇ ãØãÆäíÏ ˜å ãí ÎæÇåíÏ ÇÒ ÓÇãÇäå ÎÇÑÌ ÔæíÏ¿", vbYesNo + vbQuestion, "ÎÑæÌ ÇÒ ÓÇãÇäå")
If answer = vbYes Then
CloseDatabase
Else
Cancel = True
End If
End If
End Sub
Private Sub UserForm_Terminate()
loginForm.Show
End Sub
When I close the main form by clicking 'X' button, the login form reappears and the main form is closed; But when I login again (preferably using the same credentials) the main form is displayed but totally unresponsive (No list population, 'X' button doesn't work or any other controls in the form).
What should I do? Does the code in the UserForm_CloseQuery() unload the main form and all of its macros and I can't get the required events back to function or am I missing something?
It's not a time I started coding VBA and I can't make the head or tail of it easily when new problems arise. Any help would be appreciated. Thanks
Assuming your forms are modal, change the login form code to:
Private Sub OpenMainForm()
Me.Hide
MainForm.Show
Me.Show
End Sub
and remove the Userform_Terminate code from the main form. That will mean that the login form automatically shows when the main form is closed.

Unload me or formname.hide doesnt work excel-vba

In excel-vba i have a form named frmTemplateSelector when I select one of the two options and click on "done", the form doesn't hide!
I am calling frm.show from another module, where in depending on choice, other pop ups will appear.
Private Sub btnDone_Click()
frmTemplateSelector.Hide
Exit Sub
End Sub
Private Sub btn1_Click()
name = "XYZ"
End Sub
Private Sub btn2_Click()
name = "ABC"
End Sub
You can either Unload or Hide an UserForm.
Hide literally only hide the UF
Unload hide and reset all variables in the UF
Private Sub btnDone_Click()
Unload frmTemplateSelector 'or Unload Me
Exit Sub
End Sub
You can also change the UF's name to Me in this code, as it is the UF's code module, like this :
Private Sub btnDone_Click()
Me.Hide
Exit Sub
End Sub

How to refresh a form?

I have a userform with a button on it. I want the button to "refresh" the form, in other words I want the form to end, and then reopen.
The code below won't work because the form/macro has already ended, but I want the button to perform this task or similar.
sub command1.click()
end
userform1.show
end sub
I have checked and tried most or all the options I can choose from for a userform to "refresh". Is this even possible?
Here is a very simple way. Simply Re-Initialze the Userform using the UserForm_Initialize
Private Sub UserForm_Initialize()
TextBox1.Text = "" '<~~ Just an Example
'
'~~> Put here the code to re-initialize the userform which will refresh it
'
End Sub
Private Sub CommandButton1_Click()
TextBox1.Text = "Sid"
MsgBox "Re-Initialzing the Userform"
UserForm_Initialize
End Sub
Private Sub CommandButton1_Click()
Unload Me
UserForm1.Show
End Sub

Resources