Userform .setfocus and _Change events - vba

I created a Userform consisting of a textbox and drop-down menu. The idea is for users to complete the textbox, and hit "save", which pastes the value entered onto a corresponding cell in the excel workbook.
Since the information entered in the drop-down menu can change over time, I formatted the textbox to display when last the field was changed by a user.
To do this, I created a _CHANGE event on the drop-down menu, and specified that if the selection changes, the textbox must apply = NOW, which consequently shows the date and time. This works fine in itself.
The problem arises when the USERFORM is initialized. Its code contains a .SETFOCUS for the drop-down menu to display its existing value. It appears as if the .SETFOCUS on the USERFORM_INITIALISE act as _CHANGE event on the drop-down menu, which causes it to edit the date and time before a user actually changes the field.
How can I go about to change this?
Private Sub drop_down_menu_change()
textbox.Value = Now
End Sub
Private Sub UserForm_Initialize()
drop_down_menu.AddItem "1"
drop_down_menu.AddItem "2"
drop_down_menu.AddItem "3"
drop_down_menu.AddItem "4"
drop_down_menu.AddItem "5"
drop_down_menu.SetFocus
End Sub
Below is a screenshot of the userform as well as the code:

During the Initialize event, it is normal to trigger the Change event when the code adds items...
You can overpass the problem creating a Boolean variable, able to make the event to work only after form initialization. Something like this:
Declare a variable in the top form code module area (declarations area):
Private noEvent As Boolean
In the initialization event use it in this way:
Private Sub UserForm_Initialize()
noEvent = True
drop_down_menu.AddItem "1"
drop_down_menu.AddItem "2"
drop_down_menu.AddItem "3"
drop_down_menu.AddItem "4"
drop_down_menu.AddItem "5"
drop_down_menu.SetFocus
noEvent = False
End Sub
And the event should be transformed in:
Private Sub drop_down_menu_change()
if not noEvent Then textbox.Value = Now
End Sub
Unfortunately, EventsEnabled = False does not work as it should, for the form controls...

Related

Macro Freezes when ListBox is clicked

I'm using Office Professional Plus 2019. I have a ListBox that sometimes freezes the macro. The UserForm (AffDateSelector) has a ListBox (DateSelectionList) and a Button (Continue). The UserForm is called from the driving Sub with AffDateSelector.show. The code for the form is below:
Option Explicit
Private Sub Continue_Click()
Dim lngCurrItem As Long
'Assign the selected value to the public variable strClassDate.
For lngCurrItem = 0 To DateSelectionList.ListCount - 1
If DateSelectionList.Selected(lngCurrItem) = True Then
strClassDate = DateSelectionList.List(lngCurrItem)
End If
Next lngCurrItem
'Unload the form and return to the calling Sub.
Unload Me
End Sub
Private Sub DateSelectionList_Click()
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_initialize()
'Load ListBoxAccounts with the public variable strDateArray
With DateSelectionList
.List = strDateArray
End With
'Default the first row in DateSelectionList to be selected.
DateSelectionList.Selected(0) = True
End Sub
I've been using F8 to step through the code. When the form is shown, the UserForm_initialize() sub is executed and the data display properly. If the user does nothing else except click the Continue button, the Continue_Click() sub gets executed, the variable strClassDate is populated, control is returned to the calling sub and all is well.
The problem comes when the user selects an item other than the one defaulted in the list. Once the user clicks on the ListForm, the sub DateSelectionList_Click() is executed, but there's nothing in that sub, so the macro freezes. If I put in "dummy code" into DateSelectionlist_Click() (e.g. strClassDate = strClassDate) that line is executed and then the macro freezes when the End Sub statement is executed.
How can I allow the user to click on a non-defaulted item in the list, keep the form displayed, and wait for the Continue button to be pressed?
The code was fine. I had a lot of windows open and the pop-up didn't appear I was running the code from the VB editor but the pop-up never appeared. When I pressed Alt + Tab it didn't appear in that list either.

Combine 2 Textboxes into another Textbox

so i am still new to access, but am trying to auto populate a third textbox based on two other textboxes. i have one textbox asking for the DOB (mm/dd/yyyy) and the second text box for their last name. so the third textbox would auto generate a combined value. i know my basic equation would be
=Left([Last Name],3)+Right([DOB],7)
Example: Birthday - 10/01/1978
Last Name - Smith
Result - Smi01/1978
any help would be great
Here is the code you might paste to the code sheet of your user form.
Option Explicit
Private Sub TextBox1_Change()
' runs after every character typed
SetTbx3 False
End Sub
Private Sub TextBox1_AfterUpdate()
' runs when the focus leaves TextBox1
SetTbx3 True
End Sub
Private Sub TextBox2_Change()
' runs after every character typed
SetTbx3 False
End Sub
Private Sub TextBox2_AfterUpdate()
' runs when the focus leaves TextBox2
SetTbx3 True
End Sub
Private Sub SetTbx3(ByVal Complete As Boolean)
If IsDate(TextBox2.Value) And (Complete = True) Then
TextBox3.Value = UCase(Left(TextBox1.Value, 3)) & Format(CDate(TextBox2.Value), "mm/yyyy")
Else
TextBox3.Value = Left(TextBox1.Value, 3) & Right(TextBox2.Value, 7)
End If
End Sub
There are 3 text boxes, TextBox1 (Name), TextBox2 (DOB) and TextBox3 (File name).
There are two event procedures for each of TextBox1 and TextBox2, Change and AfterUpdate. The Change event occurs when you type a character in the textbox. The Update event occurs when you move the cursor out of the text box after editing. This is for demonstration only. You may not need both.
All of these events call the Sub SetTbx3 which requires one argument, "Complete" which, if True, signifies that the call came from the AfterUpdate event. SetTbx3 will produce a different string for TextBox3 depending upon whether the arguments are complete or not but still provide a string based on the entered string, instead of the entered DOB date, if the date entered isn't recognized as a date.

Send keystroke to caret

I have an onscreen number pad 0-9, I'm trying to send a keystroke to a checkbox when a user clicks the button of that number (essentially a touchscreen keyboard). There are several textbox's and I want to send it to the one that currently has the caret in it. I've tried SendKeys.Send("1") but that doesn't send it. What is the best way of doing this?
The issue you are encountering is because when the user clicks on any of your buttons, the button takes focus and the textbox loses focus, and therefore any keystrokes are sent to the control with focus, i.e. the button.
One possible way around this might be to use a global variable to store a reference to the last textbox to have lost focus on the form (via the On Exit or On Lost Focus events of each textbox), and then populate the content of this stored textbox with an appropriate value as part of the On Click event of each button.
A very simple example of this might be something along the lines of:
Dim LastTextBox As TextBox
Private Sub TextBox1_Exit(Cancel As Integer)
Set LastTextBox = TextBox1
End Sub
Private Sub TextBox2_Exit(Cancel As Integer)
Set LastTextBox = TextBox2
End Sub
Private Sub Button1_Click()
If Not LastTextBox Is Nothing Then LastTextBox = "1"
End Sub
Private Sub Button2_Click()
If Not LastTextBox Is Nothing Then LastTextBox = "2"
End Sub

vba userform checking the GroupName instead of an individual button

I'm using a VBA Userform. The MassPrompt userform has a set of six GroupNames and some text boxes. Each GroupName contains two or more radio buttons.
I'd like the following code to be triggered anytime any element within the GroupName "GROnly" changes. If the user made an inappropriate button choice in "GROnly" based on the choice in another group, I'd like to display a warning message and present the MassPrompt userform again.
Right now, I've assigned the code to the one button "GROnly_yes". It works, but only when that one button is clicked. How do I position this within the UserForm code to trigger anytime a button with the GroupName "GROnly" is clicked? Thanks for looking at this.
Private Sub GROnly_yes_Click()
'Prompt if the GROnly is true and it's inappropriate for the GSetting choice
If GROnly_yes = True And GSetting_renewal = True _
Then
GROnly_yes = False
GROnly_no = True
MsgBox ("The GROnly can't be chosen with a Renewal." & vbNewLine & _
"The GROnly button has been changed to 'NO'.")
UserForm_Initialize
End If
'Other IF statements here.
End Sub
If I understood well, GRonly is the GroupBox that contains (let's say) the radio_button_1 and radio_button_2.
The reason why the code doesn't trigger is that when he/she changes the value of one radio-button is not clicking on the GroupBox, but rather changing the value of that single radio-button.
You will have to add the code to the _Change event of the radio button objects. This is an example:
Sub myFunctionalCode()
'your code here
End Sub
Private Sub radio_button_1_Change()
myFunctionalCode
End Sub
Private Sub radio_button_2_Change()
myFunctionalCode
End Sub
Thanks for the reply. That would work.
In the meantime, I came up with another alternative as well. I removed the code from:
Private Sub GROnly_yes_Click()
And I moved it to the button that the user clicks to submit the form:
Private Sub ContinueButton_Click()
In my original code I had to change UserForm_Initialize to Exit Sub to make the code work in the "submit" button.
Thanks again for your help.
Phil

Initializing a userform with preset values?

Working in VBA for excel 2010. This is my first time working with VBA and userforms. Right now I have a barebones userform "UserForm1" trying to sort this issue out. It consists of two radio buttons "OptionButton1" and "OptionButton2" belonging to GroupName "WK" and two textboxes "TextBox1" and "TextBox2".
When I run the userform, I want "OptionButton1" to be selected and the subsequent if/then statements to be run. However, right now I cannot get it to do this. My code:
Public Sub UserForm1_Initialize()
UserForm1.Show
Me.OptionButton1.Value = False
Me.OptionButton1.Value = True
MsgBox ("dia locked")
Me.TextBox1.Value = "blah"
End Sub
Public Sub UserForm1_Activate()
End Sub
Public Sub OptionButton1_Click()
If Me.OptionButton1.Value = True Then
MsgBox ("dia locked")
Me.TextBox1.Value = "blah"
End If
End Sub
Private Sub TextBox1_Change()
End Sub
When I run the form, nothing happens and "OptionButton1" is false. If I click it, the message box displays and the textbox displays the text. It just won't do it on startup. I have tried going into the optionbutton properties and setting 'value' to true, which makes it true on startup, but the messagebox still doesn't display and the textbox is blank until I click it.
...please help. Thank you.
I figured it out.
I suddenly found the dropdowns. Apparently I should have put Userform_Initialize() instead of UserForm1_Initialize(), and instead of OptionButton1_Click() I put the code into OptionButton1_Change() which ran the subsequent initialization sequence.
You guys/gals are awesomesauce. I have learned everything from reading your threads. Thank you!