populate values in to two textboxes from a single userform - vba

I am writing a code in Ms-Word vba.
I made two userform say userform_1 and userform_2
userform_1 contains two buttons and two textboxes.
userform_2 is a calendar userform.
I wrote a code for showing a calendar useform on each button click event.
Now, I want to write a code such that when user clicks on 1st button the selected calendar values will display in textbox1 and when user clicks on 2nd button the selected value displayed in the 2nd textbox. But it displays the same value in textboxes.
Please let me know this how could i achieve this...
Private Sub CmB_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Main.Event_DblClick = True
Dim SampleDate As Date
frmAE_Tool.TextBox1.Text = Me.Value
frmAE_Tool.TextBox2.Text = Me.Value
End Sub

What you can do is to create a Sub in your userform_2 which going to determine the output of the calendar.
Userform_2 :
Declare a Private variable like below :
Private mTextBox As Control
(This textbox would be the output textbox of your calendar)
Add the following Sub :
Public Sub SetOutput(pTextBox As Control)
Set mTextBox = pTextBox
End Sub
(This Sub would allow to decide which output you want to use)
Modify your CmB_DblClick Sub like this :
Private Sub CmB_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Others things that I didn't understand
mToolbox = Me.Value
End Sub
Userform_1 :
For the first button, modify the Click Sub like this :
Private Sub Btn1_Click
[...]
YourCalendarForm.SetOutput Textbox1
End Sub
And for the second, like this :
Private Sub Btn2_Click
[...]
YourCalendarForm.SetOutput Textbox2
End Sub

Related

If a text box contains text then display a second text box under it else hide second text box

I have multiple text boxes under each other. I would like them all to be hidden except the first one. If the first box has text in it then the second box should display. If the second box has text in it then the third box will display.
You can use the AfterUpdate event of the textbox, here for the first:
Me!TextBox2.Visible = Not IsNull(Me!TextBox1.Value)
and so on.
Given that your textboxes are named txtBox1 to txtBox6 you can place this procedure in the AfterUpdate event of each of them.
The procedure also sets the value of invisible textboxes to Null. If you don't want that, just comment the regarding line of code.
Private Sub SetTextBoxes(ByVal startWithTextBoxNr As Long)
Const TEXTBOX_COUNT As Long = 6
Const COMMON_NAME As String = "txtBox"
Dim index As Long
For index = startWithTextBoxNr + 1 To TEXTBOX_COUNT
With Me(COMMON_NAME & index)
.Visible = Not IsNull(Me(COMMON_NAME & index - 1).Value)
If Not .Visible Then .Value = Null
End With
Next index
End Sub
The event handlers would look like this:
Private Sub txtBox1_AfterUpdate()
SetTextBoxes 1
End Sub
Private Sub txtBox2_AfterUpdate()
SetTextBoxes 2
End Sub
Private Sub txtBox3_AfterUpdate()
SetTextBoxes 3
End Sub
Private Sub txtBox4_AfterUpdate()
SetTextBoxes 4
End Sub
Private Sub txtBox5_AfterUpdate()
SetTextBoxes 5
End Sub
Additionally you could add SetTextBoxes to the Load event of the form to initialize the controls once for the first textbox:
Private Sub Form_Load()
SetTextBoxes 1
End Sub

VBA multiple option dialog box output

I created a user form with multiple options and now I want that the option the user selects is shown in a label under the button that calls the user form.I changed the caption in the text box under the button to resemble what should happen
However my options aren't working. Should I save the output in a global variable and then call it back to change the label and if so how do I do that? Or is it possible to just call the selection within the user form?
The code I was trying to run was this one to call the message box and then change the text box which is actually a label called "labelpage"
Private Sub CommandButton1_Click()
UserForm1.Show
If UserForm1.OptionButton1 = True Then LabelPage.Caption = "Company Restricted"
If UserForm1.OptionButton2 = True Then LabelPage.Caption = "Strictly Confidential"
If UserForm1.OptionButton2 = True Then LabelPage.Caption = "Public Information (does not need to be marked)"
End Sub
I also had this for each button click just to close them after selection, within the user form code.
Private Sub OptionButton1_Click()
OptionButton1.Value = True
Unload Me
End Sub
Private Sub OptionButton2_Click()
OptionButton2.Value = True
Unload Me
End Sub
Private Sub OptionButton3_Click()
OptionButton3.Value = True
Unload Me
End Sub
Is there just a tiny mistake of syntax or something like that or is this just completely wrong? Thank you in advance for your help.
The issue is that you are unloading the UserForm, meaning the controls are not available to you. The solution is to just hide the UserForm:
Private Sub OptionButton1_Click()
Hide
End Sub
Private Sub OptionButton2_Click()
Hide
End Sub
Private Sub OptionButton3_Click()
Hide
End Sub

How to dynamically edit a excel vba form label and tab label?

I have an excel vba form which has some tabs(pages).What I need is, if I double click on label, it should be editable. same is the case for the tabs.
I tried to add some things into the double click function, but not showing any change.
Labels are not editable by the end user
you may adopt this workaround
'change "Label1" occurrences to your actual label name
Private Sub Label1_DblClick()
Me.Label1.Caption = Application.InputBox("enter label text", "label editing", "")
End Sub
You can have a TextBox and disable it. The side effect is you will have textbox edit cursor over the label.
Have two subroutines, one called Labelize and another Textboxize. The former locks the textbox, makes it tranparent, and sets the 3D effect to flat. The latter unlocks it, makes it opaque, and sets the 3D to sunken (the default).
' Makes a textbox look like a label
Private Sub Labelize(txtbox As MSForms.TextBox)
txtbox.Locked = True
txtbox.BackStyle = fmBackStyleTransparent
txtbox.SpecialEffect = fmSpecialEffectFlat
End Sub
' Makes a textbox look like a textbox
Private Sub Textboxize(txtbox As MSForms.TextBox)
txtbox.Locked = False
txtbox.BackStyle = fmBackStyleOpaque
txtbox.SpecialEffect = fmSpecialEffectSunken
End Sub
In the form initialize event set call the Labelize method passing in your textboxes.
Private Sub UserForm_Initialize()
Labelize Me.txtExample
End Sub
In the double-click event per textbox, call the Textboxize method passing in your textbox again.
Private Sub txtExample_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Textboxize Me.txtExample
End Sub
Then in each KeyDown event, check if the key pressed was enter then call the Labelize method.
Private Sub txtExample_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then Labelize Me.txtExample
End Sub

Is it possible to enter text into an excel userform from a commandbutton on the same userform?

I am attempting to make an excel userform appear to behave more like an independent app than a userform. I would rather program an app but because of company IT rules that is not an option.
See image. Is it possible to enter data into an active textbox on the form by using a commandbutton on the included key pad?
Image of userform:
The form is not coded yet.
Thanks
Depending on what you want to do. Actually, once you click on the button to enter data on the button, the textbox would not be active any more. Anyhow, if you do not need the activetextbox, but another one, you may use the following code:
Let's say that you have a button btnOpenFile and a Label lblInformation.
You want, once you click on the button, to get "ALE ALE" in the lblInformation.
The most simple code to achieve this is probably this one:
Private Sub btnOpenFile_Click()
me.lblInformation = "ALE ALE"
End Sub
Put it inside the UserForm.
Something like this in support of my comment
Private tb As MSForms.TextBox
Private Sub CommandButton1_Click()
tb.Value = tb.Value & CommandButton1.Caption
End Sub
Private Sub CommandButton2_Click()
tb.Value = tb.Value & CommandButton2.Caption
End Sub
Private Sub CommandButton3_Click()
tb.Value = tb.Value & CommandButton3.Caption
End Sub
Private Sub CommandButton4_Click()
tb.Value = tb.Value & CommandButton4.Caption
End Sub
Private Sub TextBox1_Enter()
Set tb = Me.TextBox1
End Sub
Private Sub TextBox2_Enter()
Set tb = Me.TextBox2
End Sub

Show the selected value of a SpinButton on a label

I need to create a UserForm with a SpinButton on it. The user selects a value and when done, clicks OK (to pass the selected value to another procedure).
This is how it looks like:
Now, how do I display the selected value in the SpinButton on a Label or something else that shows the user which value she selected?
You need to define your SpinButton in the UserForm_Initialize and add a line in SpinButton_Change to update the Label :
Private Sub UserForm_Initialize()
With SpinButton1
.Min = 0 'Min Value
.Max = 100 'Max Value
'Specify the value of the change when the spin button is clicked
.SmallChange = 5 '(Default = 1)
End With
End Sub
Private Sub SpinButton1_Change()
Label1.Caption = SpinButton1.Value
End Sub
Simply use the SpinButton1_Change event. This will update the label realtime when the you click the spinbutton up/down arrow.
In the userform code area, simply paste this code
Private Sub SpinButton1_Change()
Label1.Caption = SpinButton1.Value
End Sub