Using Userform Commandbutton Caption as Public Variable - excel

I have a userform with several commandbuttons on it with unique captions on each button. I intend to use the caption as a variable within code in a module.
The 1st button (commandbutton13) clicked works fine. But when I click the 2nd button (commandbutton14), it still maintains the caption from the 1st button (commandbutton13) as the variable.
Private Sub CommandButton13_Click() 'PRIC Button userform
HoldType = UF_Dashboard.CommandButton13.Caption 'caption is PRIC
HoldText = UF_Dashboard.TextBox5.Value
sCell = UF_Dashboard.TextBox4.Value
'Check for sales order and hold text
If TextBox4.Text = "" Or TextBox5.Text = "" Then
MsgBox ("You must enter a Sales Order and Hold Text to continue.")
Exit Sub
End If
Hold
End Sub
Private Sub CommandButton14_Click() 'CUST Button on userform
HoldType = UF_Dashboard.CommandButton14.Caption 'caption is CUST
HoldText = UF_Dashboard.TextBox5.Value
sCell = UF_Dashboard.TextBox4.Value
'Check for sales order and hold text
If TextBox4.Text = "" Or TextBox5.Text = "" Then
MsgBox ("You must enter a Sales Order and Hold Text to continue.")
Exit Sub
End If
Hold
End Sub
Within the module I have the following:
Public HoldType as String
I need help understanding why HoldType is showing as PRIC after clicking commandbutton14. Even in developer mode I cannot select the line so I assume it's locking in on the 1st definition of HoldType in commandbutton13 code.

Related

After setting ActiveX textbox to empty value, previous text briefly appears in box before disappearing again

I created a data entry form on a spreadsheet using ActiveX TextBoxes. These are coupled with a submit details button and a clear data button. Both buttons at the end of execution set the text boxes back to blank to be refilled by the next user.
After the boxes are set to "", when the user begins filling the form each time the user selects one of the text boxes the previous entry details briefly appear in the text box then disappear.
This is a quite frustrating issue as I am trying to make it so the previous users details are hidden.
I searched many articles on the best way to set text boxes to empty and it seems that the main way is to set the .text property to "". There seems to be no mention of this behavior.
Sub clearForm()
' Worksheets("Student Data").userName.Text = ""
' Worksheets("Student Data").emailAddr.Text = ""
' Worksheets("Student Data").contactNum.Text = ""
' Worksheets("Student Data").Course.Text = ""
' Worksheets("Student Data").gradDate.Text = ""
' Worksheets("Student Data").addComment.Text = ""
textbox1clear
End Sub
Sub submitData()
'Code here that takes users entered data and outputs to hidden and protected sheet
'After data copied text boxes are cleared of users information, same as above sub
Worksheets("Student Data").userName.Text = ""
Worksheets("Student Data").emailAddr.Text = ""
Worksheets("Student Data").contactNum.Text = ""
Worksheets("Student Data").Course.Text = ""
Worksheets("Student Data").gradDate.Text = ""
Worksheets("Student Data").addComment.Text = ""
End Sub
Sub textbox1clear()
Dim sel
sel = Selection.Address
Sheet1.userName.Activate
Application.SendKeys ("Test ")
Sheet1.userName.Value = ""
Range(sel).Activate
End Sub
Expected - Text boxes should be clear of data ready for next user.
Actual - Text boxes are clear until user selects them to begin entering data at which point previous entry briefly appears in the box and disappears again.
This is common textbox behaviour. Unless the textbox is selected again, the changes aren't actually completed. I can see how this is inconvenient if used by other users and data entered is confidential. After numerous tests, the only working way I found around this is to trigger this flicker after clearing by making VBA select and deselect the input box, and simulating new user input with sendkeys.
Sub textbox1clear()
Dim sel
sel = Selection.Address
Sheet1.TextBox1.Activate
Application.Sendkeys(" ")
Sheet1.TextBox1.Value = ""
Range(sel).Activate
End Sub
I have built in a selection recall to make it (slightly) more seamless if the sub is called with a button. However if this is omitted and no selection is made after this sub is ran, the text will flicker again when the textbox is deselected.
Also note this should always be called manually, if it is called from a Textbox_LostFocus like I did, it will enter a continuous loop as long as the textbox isn't selected.
It's not a very elegant solution, but if data protection is priority, it will do the trick.
If this is not sufficient, you might want to hide your input altogether if this is an option.
you could use a quite long string made of "spaces" and then substitute them with a single "space"
Public Sub clearForm()
Worksheets("Student Data").userName.Text = " " 'vbNullString
Worksheets("Student Data").userName.Text = vbNullString
End Sub
make sure that quite long is enough for your textbox to be covered
Or
you could use a "dummy/informative" text to be removed right after entering the textbox for a subsequent input
Public Sub clearForm()
Worksheets("Student Data").userName.Text = "cleared"
…. (rest of your code
End Sub
Private Sub userName_GotFocus()
With Worksheets("Student Data").userName
If .Text = "cleared" Then .Text = vbNullString
End With
End Sub
Private Sub emailAddr_GotFocus()
With Worksheets("Student Data").emailAddr
If .Text = "cleared" Then .Text = vbNullString
End With
End Sub
… (and so on for all your textboxes)

How to generate an error when important input text boxes are empty

I am creating a data entry userform that calculates loads (e.g.pipeloads, wind loads etc..). There are important text boxes that cannot be left unfilled. If the user clicks the command "Add Input" and there are some text boxes left unfilled, I want an error to be generated that forces the user to enter a value.
I've got as far as generating an error for one text box. This code will trigger an error alert when you try to leave the text box without filling it first.
Private Sub txtdist_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Trim(txtdist.Value) = "" And Me.Visible Then
MsgBox "Required Entry!", vbCritical, "Error"
Cancel = True
txtdist.BackColor = vbRed
Else
txtdist.BackColor = &HC0FFFF
End If
End Sub
What I'm trying to achieve is that when I click "Continue" command button, then the program will see if any textbox or combobox is left empty to force the user to enter a value. (Similar to when you try to buy online with credit card, the page will not submit if there is not name or credit card number etc...). Thank you.
Maybe you can try this
Private Sub Continue_click()
Dim c as Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
If Trim(c.Value) = "" Then
MsgBox "Please fill out all required information", vbInformation
Exit Sub
End If
End If
Next c
End Sub
Since you want the code to check for empty textboxes when you click "Continue" Command Button, you should add your check code in that Sub.
Here's an example on how you can do it:
Sub Continue_click()
'...
If Trim(txtdist.Value) = "" Then
MsgBox "Please fill out all required information", vbInformation
Exit Sub
End If
'...
End Sub
Hope this helps.

Excel VBA | Userform that updates an selected row on an Listbox

I'm working on a basic userform project to learn and use it on my actual database for business.
a. I have created two userforms;
UserForm1 = Adding data to last row, show data on a listbox and deletes a row
UserForm2 = You can open it from on UserForm1 (I named "Edit")
b. The data that I have basically 4 columns and values;
ID || Name || Last Name || Date
When you click the "Edit" button on that and the row that you want to update data on listbox, it will open 3 textboxes with which you can change ID, Name, and Last Name. Additionally there is a "Save" button
I have a code that I made with an youtuber but what he did was he remains the ID column with Label and you can not change it, just shows the value.
What I want to do is I want to change all values (ID, Name, Last Name) not just Name and Last Name?
I tried and searched a lot but what all I did had not worked. Here is my code;
The Edit Button's code on UserForm1
Private Sub CommandButton4_Click()
UserForm2.Label4.Caption = ListBox1.List(ListBox1.ListIndex) ' I wanted to update this data also
UserForm2.TextBox1.Text = ListBox1.Column(1, ListBox1.ListIndex)
UserForm2.TextBox2.Text = ListBox1.Column(2, ListBox1.ListIndex)
UserForm2.Show
End Sub
Save Button Code on UserForm2
'When I make changes on this, It updates all rows with the entered values. This is original code that I want to make changes.
Private Sub CommandButton2_Click()
Dim i As Integer
For i = 2 To Range("A10000").End(xlUp).Row
If Cells(i, 1) = Label4.Caption Then
Cells(i, 2) = TextBox1.Text
Cells(i, 3) = TextBox2.Text
End If
Next i
MsgBox "Saved!", vbInformation
Unload Me
End Sub

set value for other userform controls on userform close

really struggling with this one, found a solution for the time being but it's not really a satisfactory one.
I have a workbook with two user forms, one to input data and one to search for previously entered data and a third user form designed as a date picker form which is set to activate on entry of a designated date text box.
Individually I have no problem, the user clicks the text box and the date picker form appears, they make their selection which closes the date picker form and adds the date to the text box. On the "input data" form I have a multi page with a single date selection text box on each of the two pages, on the "find data" form I have an option to search a single date or a range, 3 date selection text boxes in total.
now I am noticing that userform1 (input data) is prompting a run time error 91 - object variable or With block variable not set and is flagging the first line for userform2 in the code whenever I select a date from the date picker form.
what I did notice however was that the correct date was still being entered into the textbox so as a fix I added an 'On error goto' line above the highlighted line which is allowing the action to take place without interruption. What I did notice was that now if I enter a date anywhere on userform2 (find data) and then close the form and decide I want to enter data instead, the text box on userform1 will contain whatever the previous selection was on userform2. What is especially puzzlng about this to me is that the initialize event for UF1 enters the current date in the date text box.
see code below, is there a much better way to write this? I hope I explained well enough, please let me know if I can provide additional details to help.
Sub CloseDatePicker(save As Boolean)
If UserForm1.MultiPage1.Value = 0 Then
UserForm1.tbDate.Text = Calendar1.Value
UserForm1.cbMember.SetFocus
ElseIf UserForm1.MultiPage1.Value = 1 Then
UserForm1.tbDate2.Text = Calendar1.Value
UserForm1.cbMember2.SetFocus
End If
On Error GoTo dpexit
If UserForm2.ActiveControl.Name = "TextBox1" Then
UserForm2.TextBox1.Text = Calendar1.Value
End If
If UserForm2.ActiveControl.Name = "TextBox2" Then
UserForm2.TextBox2.Text = Calendar1.Value
ElseIf UserForm2.ActiveControl.Name = "TextBox3" Then
UserForm2.TextBox3.Text = Calendar1.Value
End If
dpexit:
Me.Hide
End Sub
Right out the gate, the first problem I see with your code is you're trying to change the value of a text box with the .Text property. I've personally never had luck with using .Text to set value as it consitently throws Err:91. Instead I've found it's better to use .Value.
Sub CloseDatePicker(save As Boolean)
If UserForm1.MultiPage1.Value = 0 Then
UserForm1.tbDate.Value = Calendar1.Value
UserForm1.cbMember.SetFocus
ElseIf UserForm1.MultiPage1.Value = 1 Then
UserForm1.tbDate2.Value = Calendar1.Value
UserForm1.cbMember2.SetFocus
End If
' On Error GoTo dpexit 'disabled this for you to test your code
If UserForm2.ActiveControl.Name = "TextBox1" Then
UserForm2.TextBox1.Value = Calendar1.Value
End If
If UserForm2.ActiveControl.Name = "TextBox2" Then
UserForm2.TextBox2.Value = Calendar1.Value
ElseIf UserForm2.ActiveControl.Name = "TextBox3" Then
UserForm2.TextBox3.Value = Calendar1.Value
End If
dpexit:
Me.Hide
End Sub

Dynamically added button, how to get some sort of value in the function

I dynamically add a button to each row I create in a table. Each button has a unique name and calls the same function BtnGoogle. This function should return a value in the same row as the button was placed in. Since the buttons are not actually part of the row, I need some way to transport the row number to the function, but I'm running out of ideas:
Set btn = sheet.Buttons.Add(targetCell.Left, targetCell.Top, targetCell.Width, targetCell.RowHeight)
With btn
.OnAction = "BtnGoogle"
.Caption = "Google Tag"
.Name = "Btn" & CStr(lastRow(columns("Button1").Index).Address)
End With
Sub BtnGoogle()
MsgBox Application.Caller
End Sub
I'm able to show the button name in the messagebox this way, but I can't do anything with that Caller and get the row number I passed in from the string. Any ideas?
Sub BtnGoogle()
Dim addr As String
addr = Replace(Application.Caller,"Btn","")
MsgBox ActiveSheet.Range(addr).Value ' for example
End Sub

Resources