Unable to close VBA form - vba

Real simple.
I have excel vba code which launches a form.
I have a close button on the form.
What would be the code to close the form?
Me.close doesn't work.
Indellisense doesn't seem to be giving me the right choice....

Unload Me is what I use. But you dont really need a close button. The user can just click the X in the top right corner.

if you wish to close the whole program then you should use
mainformname.close()
If you want to keep the main form open but hide the new form simply type
me.hide()

Related

Close form on "lose focus" in VBA?

I have a VBA application that shows a form with controls on it.
I'm trying to find a way to close the form if the user clicks anywhere outside of it.
My VBA application is not in Excel, so I can't use Worksheet.SelectionChange Event
I found a brilliant solution to this dilemma here:
https://sites.google.com/site/msaccesscode/forms-1/howtoclosepopupnonmodalformwhenitlosesfocus
This method uses the pop-up form's Timer event to keep checking whether the pop-up form is the active form. If so, the form stays open. If not, it closes.

How to close form without closing the whole Excel?

I've created a Form in excel 2017 with a "Cancel" button, but I don't know how to code it so that it closes itself when clicked. I've found things like "Application.Quit" but it closes everything.
There are two ways to achieve that. Both will only close your form and keep Excel opened.
I've asked a question about it a while ago maybe this can help you.
1. Hide Method
Your userForm has a Hide method, you can call it to hide your form.
Example:
Private Sub btnCancel_Click()
yourFormName.Hide
End Sub
Using the Hide method will only hide the form, it will be totally closed when you close your Excel file.
If you make changes in the form, hide it and then show it again all changes will be kept (ex: change the value of a textbox before hiding it and it will stay the next time you show your form).
The Activate event won't be triggered next time you show the form since the form was still active but hidden.
2. Unload Method
You can call the Unload method to unload your form.
Example:
Private Sub btnCancel_Click()
Unload me
End Sub
The form is unloaded from the memory. If you make changes in your form and then unload it, it won't be kept next time you show your form unlike with the Hide method.
You can use Unload Me to close the Userform.

Closing a Dialog Form and Open another form

I've got (what I believe to be) a weird issue with closing a form and opening another.
I have a button on a particular form (that is opened using the .ShowDialog if that makes any difference), when the button is clicked, the following code runs:
Me.Close()
LC.ShowDialog()
I would expect that the form containing the button should close and the LC form should open as a dialog form. What actually happens is that the Me form stays open and the LC form appears behind it with main focus.
Why would this be?
UPDATE 1
Just to clarify the set up of the forms:
Form1 opens the me form as a Dialog (Where Form1 is the main form that is launched on startup)
The me form opens the LC form and should close in the process
Ok it seems you might have a conception issue...
Let's get this through, if you call me.close(), you are asking your form to terminate itself, and to kill all forms he generated.
However, it is not direct, a windows Message is posted to your application that will be treated whenever you function is done.
Then right after that, you create a new form and you say you want to wait for it to close to continue.
I don't know what your purpose is but you have a few solutions :
If you want to go back to your main form (Form1) when done with LC :
Me.Hide()
LC.ShowDialog()
=>Your code will pick up here when LC will be closed
Me.Show()
If you don't want your Form1 to open ever again, then don't make it your starting form, or start working in the Sub Main()
make the form1 a flowmanager of the forms. I mean, form1 should opens the lc and close the me.
You must call both Dialog Forms from a parent form.
You may do something like this:
PARENT FORM
FirstDialog.Showdialog
(the app will open this dialog and wait it close)
SecondDialog.Showdialog
Doing this way the second dialog will open ONLY when the first one be closed.
You cannot start another dialog from a dialog being closed.
Good luck

Program is running even after the close button pressed

I have a 2 form program,the first form's button click will open the second form when clicked. I tried using a .close() to hide the first form, but that ended the whole program. Then I tried using a .hide(). Now, when I click the X button in the top right of the second form, the program closes, but is still running in the background. How do I get around this?
By default, a VB.NET application will exit when you close the startup form. If you're hiding the startup form and closing the second form then the startup form is still open, so the app won't exit.
If you want to be able to close the first form without exiting the application then go into the project properties and set the shutdown mode to when all forms close instead of when the startup form closes. That way, you can show your second form and then close your first form. Closing the second form will then exit the app.
Simply close the main Form from inside the second form's FormClosed event.
Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs)
Form1.Close()
End Sub
You should use Me.Close() just before the ShowDialog() function for the second window.
if you want the 1st form to be visible again, add a form1.show() to the form_closing event of the 2nd form
if you want the program to end, add an Application.Exit to the form_closing event of the 2nd form

Keeping one form on top of the calling form

I'm using VB.NET (VS2008) and have a form (frmMain) that calls another form (frmCleanUp) but when frmCleanUp shows it can be hidden behind frmMain.
What I'd like to have is the new form be locked and have to close to get rid of it?
Thanks..
Show the Form with .ShowDialog() instead of .Show().
This way, the form is shown as modal dialog box and will stay on top until you close it.
when you show the form, do it with ShowDialog
in frmMain do:
frmCleanUp.ShowDialog