How do I enter multiple characters in TextBox1_Change - vba

I need to enter multiple characters into an ActiveX textbox in PowerPoint before the marco fires. Current macro fires as each character is typed.
Private Sub TextBox1_Change()
IF TextBox1.Value = "18" then
MsgBox "You have entered 18"
End If
End Sub
Previous attempts had failed so I had to add a CommandButton that would trigger the actions after all the characters were typed in the text box, however it is cumbersome to type and click to perform tasks. So it would be even better if the Macro would be triggered after hitting the Enter/Return key after the data required was entered. I have tried KeyPress and KeyDown and i.e.
Private Sub TextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
MsgBox "hello"
End If
End Sub
but that code gave me a compile error:
Procedure declaration does not match description of event or procedure having the same name.

Perhaps the LostFocus event would be more useful. Assuming that TextBox1 is on Slide 1:
Private Sub TextBox1_LostFocus()
MsgBox ActivePresentation.Slides(1).Shapes("TextBox1").OLEFormat.Object.Text
End Sub

Related

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.

Return value of triggered event into triggering box in Excel VB Macro

I have 2 userforms, Userform_1 contains many TextBoxes (TextBox1, TextBox2, TextBox3, .....) & in Userform_2 I have 1 TextBox where user can enter value. Now I need to pass the User entered value in Userform_2 to be shown/stored in its respective triggering TextBox event in Userform_1. So when User want to pass in TextBox3 (Userform_1) then he/she will just use double_click trigger (activate Userform_2) & pass value which should be stored in TextBox3 only.
I tried this:
In Userform_1
Private Sub TextBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
On Error Resume Next
UserForm2.Show
End Sub
In Userform_2
Private Sub CommandButton1_Click()
On Error Resume Next
If UserForm1.TextBox1.Value = "" Then UserForm1.TextBox1.Value = ComboBox1.Value
If UserForm1.TextBox2.Value = "" Then UserForm1.TextBox2.Value = ComboBox1.Value
If UserForm1.TextBox3.Value = "" Then UserForm1.TextBox3.Value = ComboBox1.Value
Unload Me
End Sub
Problem is that it will display entered value in any TextBox which is empty & not specifically in TextBox3. Any insight would be helpful.
Your code is doing exactly what you have asked it to do (not what you want it to do). Based on your description above, I offer the following (not tested).
In Userform_1
Private Sub TextBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
UserForm2.Show
TextBox3.Value = UserForm2.ComboBox1.Value
End Sub
In Userform_2
Private Sub CommandButton1_Click()
Me.Hide
'Note not unloading, this means that the userform is still in memory and you can access the values after it is hidden.
' Also, hiding the form will return control back to the UserForm1 for further processing.
End Sub
And avoid using On Error, especially On Error Resume Next, all you are doing is hiding the bugs, not dealing with them. If you can anticipate that your code is going to cause an error, deal with it before that piece of code.
Thanks ADJ...
Your concept is good but it doesn't quite fulfill the requirement, just hiding the Userform doesn't remove/clear the entered value for next entry & reopening it again shows last entered value. I can write a code to blank it but then I have too many textboxes.
I used your concept of only calling required Userform from a specific TextBox...
Wrote below code, fits my requirement:
UserForm_1:
created hidden TextBox1 in UserForm_2 for identifier
UserForm2.TextBox1.Value = "<random identifier value>"
UserForm_2:
If TextBox1.Value = "<random identifier value>" Then
UserForm1.TextBox3.Value = "<User Input>"
End If
So this way I get to assign & call only identified TextBoxes.

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

Userform called Userform TextBox Control behavior

So I have an excel workbook that opens userform1 on the workbook open event.
Once certain conditions are met on userform1 it calls a procedure that loads and shows userform2. Userform2 has a label, a textbox and a button. This issue I have is that the events for the textbox on userform2 don't all fire correctly. The KeyUp & KeyDown events fire as expected, but the Change and AfterUpdate event never get called.
I've been able to replicate the behavior in another workbook that is greatly simplified. If you key or paste a text string longer than 7 characters in the textbox on userform1 it loads userform2. The desired behavior is to key in a 3 digit numeric value in the textbox on userform2 and call a sub from userform1. This never happens. Has anyone else seen this behavior before? How did you work around it?
USERFORM1: (excuse the Ascii art, evidently I don't have enough reputation to post images) :/
Label1: [ Textbox1 ]
Label2:
[ CommandButton1 ]
Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub TextBox1_Change()
If Me.TextBox1.TextLength >= 7 Then
Load UserForm2
UserForm2.Label1.Caption = "Value for " & Me.TextBox1.Text & "?"
UserForm2.Show
End If
End Sub
Public Sub Form2(data As Variant)
On Error GoTo eh
Me.Label2.Caption = Me.Label2.Caption & vbCrLf & Me.TextBox1.Text & vbTab & CStr(CLng(data))
eh:
If Err.Number = 13 Then
MsgBox "Please input a #"
UserForm2.TextBox1.SetFocus
ElseIf Err.Number = 0 Then
Unload UserForm2
End If
End Sub
USERFORM2:
label1: [ Textbox1 ]
[ CommandButton1 ]
Option Explicit
Private Sub CommandButton1_Click()
Call UserForm1.Form2(Me.TextBox1.Text)
End Sub
Private Sub TextBox1_Change()
'This event never fires
If Me.TextBox1.TextLength >= 3 Then
Call UserForm1.Form2(Me.TextBox1.Text)
Else
Debug.Print "here"
End If
End Sub
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'This event fires
Debug.Print KeyCode
End Sub
I am not sure why this happens as you can run userform2 by itself and it works. However to get around this to start with it might be best to put the code in keyUp event. That should work
EDIT
I have found a slight work around. It is not perfect as I cannot find out why it is not firing. However the up and down key press works fine. I have added this code and now the event should fire. But it is a semi-manual event.
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Call TextBox2_Change
End Sub
So when a key is pressed down. It will check for any changes.
I ended up making use of the KeyUp and BeforeDropOrPaste.
Sam, thanks for the inspiration. I was trying to replicate the code of the Change event into the actual procedure. I ended up calling the event directly from the KeyUp procedure and using Application.OnTime "UserForm2.TextBox1_Change" in the BeforeDropOrPaste event to get around the missing data.
The problem is that your code is showing the 2nd userform modally. In this situation, the TextBox1_Change() procedure in the 1st form is not allowed to complete while the 2nd userform is displayed. This interferes with the event messaging.
To resolve the issue, simply show the 2nd userform modelessly:
UserForm2.Show vbModeless
Please do not resort to the suggested hack.
Excel Hero is correct, the only point is to load a modeless userform next to a modal userform you have to .hide the modal userform so it will not interfere with the modeless userform.
see code below:
Private Sub TextBox1_Change()
If Me.TextBox1.TextLength >= 7 Then
Me.hide
Load UserForm2 vbModeless
UserForm2.Label1.Caption = "Value for " & Me.TextBox1.Text & "?"
UserForm2.Show
End If
End Sub
you can load UserForm2 as vbModeless or make the userform by default modeless.

Disable _Exit event when quitting userform using "Cancel" or "X-button"

I've got a code in a dropdown box on my userform. Every time the user moves away from the drop down box, the code checks whether the value put by the user is correct (i.e. matches a list). If it doesn't, it triggers a message box. Here is my code:
Private Sub CmboxModifyRoute_Exit(ByVal Cancel As MSForms.ReturnBoolean)
UserValue = CmboxModifyRoute.Value
counter = 0
Cell = Range("C15").Value
If UserValue = "" Then Exit Sub
Do While (counter < 35 And Cell <> UserValue) 'checking if UserValue is valid
counter = counter + 1
Cell = Range("C15").Offset(counter, 0).Value
Loop
If counter > 34 Then 'if invalid, then display message box
MsgBox "Invalid", vbExclamation
End If
End Sub
The problem occurs when I quit the userform with the "X" button or "Cancel" button. If the UserValue is invalid, it still shows me the "Invalid" message box after I have already quit the userform. I don't want it, I just want the userform to unload. How can I deal with this? Many thanks!
Change your condition to this:
If Me.Visible And counter > 34 Then
MsgBox "Invalid", vbExclamation
End If
Then the message will not be displayed if the form isn't visible.
Data Validation should go in the BeforeUpdate event of the combo box. Before Update won't trigger prior to the User Form's Terminate event. Add UserForm_Terminate and CmboxModifyRoute_BeforeUpdate events to your code, set breakpoints on the declaration of each, and watch the order of events happen in debug mode.
Private Sub CmboxModifyRoute_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
'data validation goes here
'doesn't fire when the form is closed
End Sub
Private Sub CmboxModifyRoute_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'this triggers before Terminate
End Sub
Private Sub UserForm_Terminate()
End Sub