How to close a userform when another userform or a blank space is selected in excel VBA? - vba

Whenever people clicks on the Debit Account or Credit Account fields on the userform, another userform with a Treeview will pop-out. However, the user will always have to press cancel in order to close the userform. Is there a way for the userform to automatically close when the Voucher Entry Form userform's area is selected?

If the question is "How to unload a user-form if it is not more the active object?" then the user-form in question must be modeless. Because else it cannot be deactivated without closing it's window. And because it is not possible opening a modeless user-form from a modal userform, the main user-form also must be modeless.
Example:
Do having two user-forms:
First user-form is named "MainForm" and has one button control and the following code applied:
Private Sub CommandButton1_Click()
Load SubForm
SubForm.Show vbModeless
SubForm.Left = Me.Left + 100
SubForm.Top = Me.Top + 100
End Sub
Second user-form is named "SubForm" and can be empty but has following code applied:
Private Sub UserForm_Deactivate()
Unload Me
End Sub
Then following Sub within a default module shows the main form:
Sub test()
MainForm.Show vbModeless
End Sub
Now after MainForm is shown, SubForm can be opened by button click. And if MainForm get the active form again (gets the focus again), the SubForm will unload.

Related

How can I simulate a keyPress event on a combo box when I click a buton on the same form im MS Access

I'm creating a form in MS Access with 10 butons simulatin a numeric keypad (only the values 0..9).
I would like to change the value of a combobox control in a subform each time I click on one of these butons. The combo box control is named "projectID"
I tried this but it is not the same as pressing the same key on a keypad.
Private Sub Buton3_Click()
Call Me.frmServDedicacion_Subformulario.Form.projectID_KeyPress(51) 'Ansii code for 3
end sub
I put this procedure as keypress event in order to verifiy the combobox method is executed, and it does (msgbox is ok) but the combobox doesn't receive the KeyAscii value.
Public Sub projectID_KeyPress(KeyAscii As Integer)
MsgBox Chr(KeyAscii)
End Sub
This looks like a problem referring to a control on a sub form from the main form. I still have trouble getting this right so I use a cheat sheet:
http://access.mvps.org/access/forms/frm0031.htm
I created a main form called main and put 10 buttons named button0 - button9 on the form and I dragged a form called mysubform onto the main form to create a subform. mysubform has a textbox named projectID. Then just set the click event to button0 to:
Private Sub button0_Click()
Me!mysubform.Form!projectID = 0
End Sub
Don't forget similar click events for buttons 1-9
some things that may be helpful:
! is the bang operator see:
Bang Notation and Dot Notation in VBA and MS-Access
by default, when you drag a form to create a subform control on another form, access gives the subform control the same name as the dragged form. so here mysubform refers to the subform control and not the original form used to make the subform.
Then .Form gets the form wrapped by the subform control.
I hope this answers your question

Reset a userform to blank on button click

I have a dynamic Userform. When loaded only a Combobox is visible, and user selects one option and accordingly some Labels and Textboxes with values are populated. But when I choose another option from Combobox and click on the button, all labels disappears but Textboxes remains there. Is there any way to clear or reset the Userform completely before loading?
I am not entirely sure how you set up your routines, I just based this answer on assumption. From what I understand, you want to reload the Userform using the same CommandButton in it. If that is the case, you can try this:
In a module, write a procedure that loads your form. Something like:
Sub marine()
Load Userform1
Userform1.Show
End Sub
In your CommandButton, create a routine that will call the load procedure and at the same time unloads the form. Something like:
Private Sub CommandButton1_Click()
'/* call the marine procedure after 0.5s */
Application.OnTime DateAdd("s", 0.5, Now), "marine"
Me.Hide
Unload Me '/* unloads the userform
End Sub
Your Userform will be invisible momentarily (like it flickers) but it will be reloaded just like the way you want it to be. HTH.

Create radio buttons in word VBA

Is there a way to create a macro that, when activated, will present the user with some options, then call some other macro depends on what the user selected?
For example:
Message box: Are you a male or female?
Option 1: Male
Option 2: Female
If the user selects "Male," execute macro A. If "Female," macro B.
while in Word UI press ALT F11 to open VBA IDE
in main ribbon click Insert -> Userform
and there you have a toolbox (if not, click View->ToolBox) and a Userform canvas
from toolbox drag the option button icon and drop it into desired position of Userform. repeat it twice
select first option button in the userform, click on its caption (some "OptionButton1" is shown as default) and edit it to "Male"
do the same with second option button and edit its caption to "Female"
click twice on the first option button on the userform
it will show you
Private Sub OptionButton1_Click()
End Sub
then fill it as follows:
Private Sub OptionButton1_Click()
macroA
End Sub
click twice on the second option button on the userform
it will show you
Private Sub OptionButton2_Click()
End Sub
then fill it as follows:
Private Sub OptionButton2_Click()
macroB
End Sub
this will get you started

Refresh All Subform from Another Form

I have two main forms. One is the Dashboard and another is Edit.
After Editing, I have a button to reopen the Dashboard.
It can open the dashboard but I want it to refresh the subforms within that Dashboard.
This module will refresh the form you pass it, as well as all subforms. Requery will normally reset the selected item so before we requery we save the current record. If the record doesn't exist after requery it will gracefully go back to the top without error.
Public Sub RefreshForm(ByRef theForm As Form)
On Error GoTo ErrorHandling
Dim thisRecord As Long
thisRecord = theForm.CurrentRecord
Echo False
Dim childForm As Control
For Each childForm In theForm.Controls
If TypeOf childForm Is SubForm Then
childForm.Requery
End If
Next
With theForm
.Requery
.Recordset.Move thisRecord
End With
ErrorHandling:
Echo True
End Sub
To use it you simply drop this wherever you need a refresh:
RefreshForm Me
You shouldn't need to close and reopen anything. Any opened form and the controls on it can be requeried from VBA code running anywhere. It gets a little convoluted with subforms.
To requery a subform control on the current form: [SubformControlName].Requery
To requery the form in a subform control on the current form: [SubformControlName].Form.Requery
To requery a control in a subform on the current form: [SubformControlName].Form.Controls("ControlName").Requery
To do any of the above operations on a different form in the application, prefix with Forms![FormName].
Example: Forms![FormName].[SubformControlName].Form.Controls("ControlName").Requery

Determining when the user clicks the x - VBA

I would like to programmatically decide if the user has clicked the top right "x" button while using a form in visual basic.
I have tried:
Private Sub nameOfForm_Exit()
'code goes here
End Sub
Which has been unsuccessful. Any help would be much appreciated.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
MsgBox "Good bye"
'/ To prevent user from closing the form
'/ Set cancel to True
Cancel = True
MsgBox "You can't close me!"
End Sub
You can't invent your own terms like "Exit". You have to take them in the combo boxes above (known as Events). Since your event pertains to the Userform itself, you need to choose Userform in the left combo box. You will also see all the controls your userform has, they each have a set of events.
As has been said QueryClose will refer to the red X in the top right corner. You also have Deactivate, which will happen whenever the UserForm loses focus (if it is ModeLess, meaning that a user can click the sheet behind the userform without closing it, this will trigger Deactivate) and Terminate, which occurs after the Userform closes with QueryClose.