Combine 2 Textboxes into another Textbox - vba

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.

Related

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.

Persistent values in ListBox in Excel VBA

This is my first question in this forum and I am very new to Excel VBA.
I have a text box that has an Exit event. My requirement is that when I fill that textbox and exit from there its values should be populated in ListBox. Now, when I am trying to insert the value again inside textbox, it overwrites previous value inside the ListBox. I want the values to be persistent. So the values entered in text box should come in sequence.
Here is what I tried :
ListBox1.ColumnCount = 2
ListBox1.AddItem
ListBox1.List(0) = TextBox2.Text
But then how can I take the next value from Text box and make it visible in List box...(so if I enter 3 values in text box it should show three rows in Listbox)
When you say ListBox1.List(0) = TextBox2.Text, you are telling the code to always add (replace on 2nd instance) to the first item in the listbox.
Is this what you are trying?
Private Sub UserForm_Initialize()
ListBox1.ColumnCount = 2
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ListBox1.AddItem
ListBox1.List(ListBox1.ListCount - 1, 0) = TextBox1.Text
End Sub
If you do not need a multi column listbox then the below will also do.
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ListBox1.AddItem TextBox1.Text
End Sub

How do I enter multiple characters in TextBox1_Change

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

VBA: Prevent Autocomplete in combobox while

I currently have a userform that has a combobox and a listbox. Both contain the same list of items (the combobox has an extra null value). If a user selects an item in the combobox, then the same item will be selected in the list box.
The problem that I am having is coming from the combobox autocompleting when the user tries typing a value instead of choosing one from the combobox. When the user types a value, it will autocomplete what they typed to a value within the combobox. (If I type "8" then the combobox will autocomplete that to "8184123".)
If I set MatchEntry to 2 - fmMatchEntryNone, then the combobox does not autocomplete. However, the combobox does not select a value based on what the user has typed.
Is there any way to stop the combobox from autocompleting while keeping letting MatchEntry stay at 1 - fmMatchEntryComplete? Or is there anyway to implement fmMatchEntryComplete only when the value that the user enters is exactly equal to a value in the list of the combobox?
you can have ComboBox methods and properties work for you
in the form calling sub place:
Sub main()
' code that preceeeds the userform loading...
With UserForm1
'other code to set some userform or userform controls properties...
.ComboBox1.MatchEntry = fmMatchEntryNone ' <--| set this just before showing userform
.Show
End With
Unload UserForm1
' code that follows the userform closing...
End Sub
in the userform code pane place this function:
Function CheckCB(cboBox As MSForms.ComboBox) As Boolean
With cboBox
.Text = .Value '<-- This is the "trick": "refresh" the combobox text
CheckCB = .MatchFound
If Not CheckCB Then
MsgBox "Invalid entry", vbCritical
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Function
despite MSDN online doc the refreshing of .Value actually has MatchEntry and MatchRequired work on it even if MatchEntry is set to fmMatchEntryNone
then you have to call CheckCB() function to prevent leaving userform until your combobox has been entered a valid value
for instance you could place it in any "exit" button click event handler
Private Sub CommandButton1_Click()
If Not CheckCB( ComboBox1 ) Then Exit Sub '<-- if ComboBox check failed then exit
' otherwise let code run ...
End Sub
or, if you wanted the user not to enter any other control until your combobox has a valid value, you must act similarly for every other userform control event handler, i.e. placing If Not CheckCB Then Exit Sub at their beginning
Try using Form1.ComboBox1.AutoWordSelect = True
Custom Autocomplete
Turn off match entry
ComboBox1.MatchEntry = fmMatchEntryNone
When the user types iterate over the combo's list. It there is only 1 possible match, select it.
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim i As Integer, matchIndex As Integer
matchIndex = -1
For i = 0 To ComboBox1.ListCount - 1
If InStr(1, ComboBox1.List(i), ComboBox1.Value) Then
If matchIndex = -1 Then
matchIndex = i
Else
Exit Sub
End If
End If
Next
If matchIndex > -1 Then ComboBox1.ListIndex = matchIndex
End Sub

vba: userform functions

I have a sub that is activated from a button on a userform. The basic procedure from the click is
1) Run my sub based off of user inputs
2) Select results sheet
3) Display my results
4) Unload my userform
I've run into a problem because I want to try and put bounds on an user input value and if the user inputs something out of the range a message box will pop up notifying them of the range. I've been able to accomplish this simple task through the use of an if/then loop. After the user exits out of the message box I want to keep the userform displayed along with the original user inputs and allow the user to change their input. But currently after the user clicks 'ok' on the message box, my click sub continues its procedure and unloads my userform and selects my results worksheets. Is there a simple one line code that I can put after my msgbox state to preserve the userform instead of making the user re-enter their values?
EDIT - The general gist of my code is as follows:
Private Sub CommandButton1_Click()
PropertySearch.Search
ActiveSheet.Name = "SearchResult"
Cells(1, 1).Select
Unload ILsearch
End Sub
Sub Search()
If (TextBox1 And TextBox2 <= 8) And (TextBox1 And TextBox2 > 0) Then
'
'Performs my desired function
'
Else: MsgBox ("Database only includes ionic liquids with a maximum of 8 carbons in cation")
End If
I would turn Search into a function which returns true or false if the input is within bounds or not:
Function Search() AS Boolean
If (TextBox1 And TextBox2 <= 8) And (TextBox1 And TextBox2 > 0) Then
Search = True
Else
Search = False
EndIf
End Function
then you just exit the sub if input does not meet your bounds:
Private Sub CommandButton1_Click()
If(Not PropertySearch.Search) Then
MsgBox("your error message here")
Exit Sub
EndIf
' rest of the routine
End Sub
There are about a thousand and one ways around this - the question is what behavior do you want the form to have?
Do you want it to self close after a certain amount of time?
Check out this example of timer 1
Or look into Application.Ontime
Application.OnTime Now + TimeValue("00:00:10"), "unloadForm"
Where "unloadForm" is a sub in a normal module
Sub unloadForm()
Unload ILsearch
End Sub
Do you want to add a close form button?
Private Sub CommandButton1_Click ()
Unload Me
End Sub
Do you want the user to close the form manually with the red X in the top corner? Simply delete the line with Unload
Do you want to display a modal popup window that freezes Excel until closed and then your form unloads? Try addging MsgBox "Hello" before you unload the form.
And many many many more!
For example, I have several forms that use keyboard events. Escape can clear all fields and hide/unload the form while Enter does the same thing but also write the values to a table, Delete only clears the active control and doesn't hide the form, up and down arrows cycle through forms hiding the current and showing the previous/next while left and right function like tab/shift + tab.