I have a form1, that has a button, upon that button click i want to open another
form (form2), thats easy enough.But when form2 opens, i want to terminate form1.
How do i do that? I've been stuck for a long time!
Karl, you would use
Unload Form1
for that. So, something along the lines of:
UserForm1.Show vbModeless
Then, in you button click event:
UserForm2.Show vbModeless
Unload UserForm1
Related
I have a submit button inside the subform. When I clicked my submit button then this will click on of my Navigation TAB on the Main Form. The code must be executed inside the submit button event in VBA. Please any help would be appreciated.
Your description is very unclear but if you want to run a code on Main Form than just create on it a public sub which would do your refresh:
Public Sub MyRefreshSub()
' refresh your main form here
End Sub
and on _Click event of the Submit button of subform call it like this:
Private Sub Submit_Click()
Me.Parent.MyRefreshSub
End Sub
or if you want just click the Tab:
Private Sub Submit_Click()
Me.Parent.TabCtl0_Click
End Sub
I hope I understood your question right.
I have a UserForm with a cancel button.
Sub DialogTest()
MyForm.Show
End Sub
Private Sub CancelButton_Click()
Unload Me
End
End Sub
I also tried MyForm.Hide, End by itself, cmdExit_Click.
The cancel button does not close the dialog nor does it cause the debugger to come up.
I was only able to replicate your issue when the Unload Me Sub was pasted in a Worksheet or Module. When the Sub is in the Userform, it works fine.
Here, the code is pasted in a module and does not close the Userform
Instead, from VBE, double click on your UserForm, then double click on your Cancel Button.
Then paste the code here
I have a dialog box pop up with radio buttons, the radio buttons currently run macros. However, once I have the dialog box pop I can't close it. I have added a cancel button. The code is below
Private Sub CommandButton1_Click()
Cmd_Cancel
End Sub
How can I get my dialog box to close by pressing the cancel button?
In my form I used a Cancel button with the below code for it
btnCancel is the (Name) of the button
Private Sub btnCancel_Click()
Unload Form_Form1
End Sub
Depends on what you want. The keyword "Me" is the actual instance of the Form-Object. Hide it and its still in the memory. Or unload it to kill it:
When it's only hidden you can show it again with .show (from another piece of code...)
Private Sub CommandButton1_Click()
Me.Hide
Unload Me
End Sub
There's a lot of information and a lot that you can do with userforms but I can't really find a standard way to use them. Let's say I have a userform with a standard dropdown list that asks someone to choose a fruit. In the userform code I will put the below code after adding a combo box called fruitcombo:
Private Sub UserForm_Initialize()
userform1.fruitcombo.AddItem "Peach"
userform1.fruitcombo.AddItem "Pear"
userform1.fruitcombo.AddItem "Grape"
End Sub
I will also add a commandbutton which will be labeled "Submit" and in that event:
Private Sub Submit_Click()
Me.Hide
End Sub
That's where it starts to get hazy. What's the best way to capture the answer that was selected? One way I can think would be to make a global variable called fruitanswer and then instead of the Me.Hide we can skip straight to Unload Me
Ex:
Private Sub Submit_Click()
fruitanswer = fruitcombo.value
Unload Me
End Sub
Or we can have fruitanswer as a private variable in the module where the userform is called and then unload it in there. There are also multiple ways to initialize the userform. I'm also wondering the best way to initialize it. The Show method will automatically initialize it, but the Hide method WON'T automatically de-initialize it. For that, the Unload statement is necessary. So does anyone initialize it before calling the Show method using the Load statement?
Ex:
Load userform1 'Any point to including this?
userform1.show
'user chooses a fruit and clicks submit button
'userform is hidden by commandbutton but not unloaded yet
fruitanswer = fruitcombo.value
Unload userform1
Out of these options, which is the best method? Is there anything to make it more efficient?
You can wrap the whole thing in a single function call:
strFruit = UserForm1.GetFruit()
Then, in your UserForm, have it do the work of displaying and unloading itself like so:
Private bOK As Boolean
Public Function GetFruit() As String
bOK = False
Me.Show vbModal
If bOK Then GetFruit = ComboBox1.Text
Unload Me
End Function
Private Sub cmdOK_Click()
bOK = True
Me.Hide
End Sub
Private Sub cmdCancel_Click()
Me.Hide
End Sub
This assumes you have buttons named cmdOK and cmdCancel and a combobox named ComboBox1.
Since Show() is being called modally, the code after it won't execute until the form is closed or hidden. When either button is clicked (or the form is closed by other means) then the code continues, the selected text is returned (if OK was clicked), and the form unloads itself.
The beauty of doing it this way is that your calling code doesn't need to worry about instantiating and destroying the form each time it's called. It's just a single statement to load the form and get the return value.
Of course, you'll need to add the code to populate the combobox with whatever items you wish to display.
Can anyone tell me How do i do certain task based on the command button options.
I have a Userform where user submits his data and it has 2 command buttons one is OK and the other is CANCEL. I have to exit when user clicks CANCEL and continue the Process when user clicks ok
file_name=userform1.textbox1.value
This is how we can get the data of the text box into our functions but what is the way to know which command button key is pressed ? Becuase if i press OK or CANCEL the operation is still being performed. I have tried like these
value=userform1.commandbutton1.value
value2=userform1.commandbutton2.value
not working. I even tried these
commandbutton1_click() \\ this is the OK command button
call main
end sub
commandbutton2_click() \\ this is the CANCEL command button
end sub
I have tried To call the main sub_routine from the OK sub routine But it says main routine not found becuase I have written these codes in the thisworkbook.and the commandbutton1_click routine is inside the Userform1 module
SO how do i make it work? I hope you understood where I'm Stucking up.Thank you in advance
Private Sub CommandButton1_Click() 'should be called cmdOk
main
End Sub
Private Sub CommandButton2_Click() 'should be called something like cmdCancel
Unload Me
End Sub
Private Sub main()
'DO PROCESSING
End Sub
Do you call main on Form_Load() or Form_Activate() or anything like this? When clicking Cancel, the above unloads the form. When clicking Ok, it starts the processing. There is not more to it.
If you want to keep the main method outside of your userform, you can try something like this, based on the example given by #cularis
' This way you can keep your code in ThisWorkbook
' I would recommend creating a new module.
' You can name your module whatever you'd like.
Private Sub CommandButton1_Click()
ThisWorkbook.main
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub