I have a Text box (PinEntry) and a Check box. The Check box will mask the password when checked. However, it only masks once. I understand the logic of the first subroutine that it should mask entries as long as I am still entering values in the Text box.
Private Sub CheckBox1_Click()
If AccessForm.PinEntry.Value = True Then
AccessForm.PinEntry.PasswordChar = ""
ElseIf AccessForm.PinEntry.Value = False Then
AccessForm.PinEntry.PasswordChar = "*"
Else
AccessForm.PinEntry.PasswordChar = "*"
End If
End Sub
Private Sub PinEntry_Change()
CheckBox1_Click
End Sub
The reason your code will only mask when clicked is because you will only ever enter your final Else statement. You are checking if the PinEntry (Textbox) has a boolean value of True or False, which it never will.
No need to call the checkbox click event when the text in the PinEntry Changes. Passwordchar is a property that only needs to be set on the textbox when you want to enable or disable it. You just need to act when the Checkbox is clicked.
This code will show the pin if the checkbox is checked, and apply the mask if it is unchecked.
Private Sub CheckBox1_Click()
If CheckBox1.Value Then
PinEntry.PasswordChar = ""
Else
PinEntry.PasswordChar = "*"
End If
End Sub
Related
Situation
I have two form control check boxes. I am trying to write a code that will allow only either of them to be true.
my code is
Sub CheckBox2_Click()
If CheckBox1.Enabled = True Then
CheckBox2.Enabled = False
Else
If CheckBox2.Enabled = True Then
CheckBox1.Enabled = False
End If
End If
End Sub
I have this code in module and have assigned the same macro for both the checkboxes. I get run-time error 424. I beleive this is very basic problem but I unable to dela with it.
Thank you
Please, test the following way. Form check boxes do not have a click event, as ActiveX ones have. You should associate the next sub to both of them. The check boxes I tested, have their names as "Check Box 1" and "Check Box 2". You have to change yours according to the reality in your case, Please, copy the next code in a standard module and then associate it to both used check boxes:
Option Explicit
Sub FormCheckBoxChange()
If ActiveSheet.CheckBoxes(Application.Caller).value = 1 Then
Select Case Application.Caller
Case "Check Box 1": ActiveSheet.CheckBoxes("Check Box 2").value = -4146
Case "Check Box 2": ActiveSheet.CheckBoxes("Check Box 1").value = -4146
End Select
End If
End Sub
Use instead of the used check box names, the appropriate ones for your case.
In case of Form text boxes, their value is not True and False as in case of ActiveX ones. It is 1 and -4146...
Are you sure you want to enable/disable the checkboxes.
Following code makes sure that either one of both boxes is checked.
Public Sub checkbox2_onClick()
Dim oCb1 As Object
Set oCb1 = Table1.Shapes("Checkbox1").OLEFormat.Object
Dim oCb2 As Object
Set oCb2 = Table1.Shapes("Checkbox2").OLEFormat.Object
If oCb2.Value = 1 Then oCb1.Value = 0
End Sub
Public Sub checkbox1_onClick()
Dim oCb1 As Object
Set oCb1 = Table1.Shapes("Checkbox1").OLEFormat.Object
Dim oCb2 As Object
Set oCb2 = Table1.Shapes("Checkbox2").OLEFormat.Object
If oCb1.Value = 1 Then oCb2.Value = 0
End Sub
I have two textboxes in a userform.
I need that if the user has entered some number in one textbox and then places the cursor in the other textbox, the first text box data is deleted and vice versa.
I was able to lock the other textbox if one has data in it, as a workaround.
I tried to replicate the lock method for deleting the value, but it does not work.
Sub checkTB()
If ConversionForm.Controls("UnitFromEntry").Text <> "" Then
ConversionForm.Controls("UnitToEntry").Locked = True
Else
ConversionForm.Controls("UnitFromEntry").Locked = True
End If
End Sub
I call this sub in the before update event of the text box.
Private Sub UnitFromEntry_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Call checkTB
End Sub
Try this
Private Sub UnitToEntry_Enter()
UnitToEntry.Locked = False
UnitFromEntry.Locked = True
UnitFromEntry.Text = ""
End Sub
Private Sub UnitFromEntry_Enter()
UnitFromEntry.Locked = False
UnitToEntry.Locked = True
UnitToEntry.Text = ""
End Sub
I am stuck with a simple checkbox on excel vba userform. I have the following code in its Click event:
Private Sub CheckBox1_Click()
Me.Label1.Caption = "Checked"
Me.Label1.ForeColor = vbRed
End Sub
When I click the checkbox1 in userform, the Label's caption changes and forecolor too. But when I uncheck it, the label caption does't disappear. Where is my code wrong or am I firing the click event again?
Try this :
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Label1.Caption = "Checked"
Label1.ForeColor = vbRed
Else
Label1.Caption = "UnChecked"
Label1.ForeColor = vbBlack
End If
End Sub
After you paint it red, it won't turn green unless you buy another pot of paint. Nor will it change the caption it has been given without another instruction. Try this code.
Private Sub CheckBox1_Click()
Dim Cap As String ' = "" at this time
With CheckBox1
If .Value Then Cap = "checked"
Me.Label1.Caption = Cap
Me.Label1.ForeColor = IIf(.Value, vbRed, vbGreen)
End With
End Sub
The code intentionally demonstrates 3 ways by which you might achieve the desired result. The smartest is the one which, at once, uses the least amount of code and is the easiest to read (for you).
I am trying to make a label visible if a Textbox has a value. So far I have this but its not working?
Private Sub Label1_Change()
If MailChannel.Value <> "" Then
Me.Label1.Caption = True
Else
Me.Label1.Caption = False
End If
End Sub
Your current code won't be triggered now, because it is linked to form's label. You should link it to textbox change event. It can be realized as follows (assuming that textbox name is txtMailChannel):
Private Sub txtMailChannel_Change()
If txtMailChannel.Value <> "" Then
lblMailChannel.Visible = True
Else
lblMailChannel.Visible = False
End If
End Sub
P.S. Try to introduce some order to your code, especially what concerns naming convention of variables. Please check-out the following link for references: https://github.com/spences10/VBA-Coding-Standards.
I want to do something like this. Don't know if it is possible in Excel VBA. On a user form, I have a checkbox and a text box.
When I check the checkbox, the checkbox's caption will be inserted into the text box, when I uncheck the checkbox, the checkbox's caption will be removed from the same text box.
When i write into the text box the caption of the checkbox, inside the text box change event, the checkbox status will be updated. And at the same moment, the checkbox click event is fired and the caption text will be doubled. How can I stop the checkbox click event from firing twice?
Checkbox's click event code:
Private Sub cbSelect_Click()
With TextBox1
.Value = IIf(cbSelect.Value, _
.Value & cbSelect.Caption, _
Replace(.Value, cbSelect.Caption, vbNullString))
End With
End Sub
Text box's change event code:
Private Sub TextBox1_Change()
If InStr(TextBox1.Value, cbSelect.Caption) Then
cbSelect.Value = 1
Else
cbSelect.Value = 0
End If
End Sub
Any idea?
When I tested your code, the checkbox status did not change which I believe is due to the use of binary values (1 / 0) instead of Boolean (True / False)
I changed your Textbox1_Changecode to the following and as soon as I delete part of the checkbox c
Private Sub TextBox1_Change()
If InStr(TextBox1.Value, cbSelect.Caption) Then
cbSelect.Value = True
Else
cbSelect.Value = False
End If
End Sub
I added a Public TextBoxFlag As Boolean at the User_Form module level, and this "flag" is being modifed and checked in both Control's events.
Once the code enters the TextBox1_Change event the flag gets a True value, and when the code reaches the cbSelect_Click event, it doesn't run the code inside if the value of TextBoxFlag = True, only if it's False (didn't came from the TextBox1_Change event, but directly from the CheckBox1 event).
Code
Option Explicit
Public TextBoxFlag As Boolean
Private Sub cbSelect_Click()
' check if event came from the value being modified in the TextBox1_Change event
If TextBoxFlag = False Then
With TextBox1
.Value = IIf(cbSelect.Value, _
.Value & cbSelect.Caption, _
Replace(.Value, cbSelect.Caption, vbNullString))
End With
End If
End Sub
Private Sub TextBox1_Change()
' raise the flag >> came from TextBox change event
TextBoxFlag = True
If InStr(TextBox1.Value, cbSelect.Caption) > 0 Then
cbSelect.Value = True
Else
cbSelect.Value = False
End If
' reset the flag
TextBoxFlag = False
End Sub
your code actually worked for me without the issue you described just substituting:
InStr(Me.TextBox1.Value, Me.cbSelect.Caption)
with :
InStr(Me.TextBox1.Value, Me.cbSelect.Caption) > 0
BTW here's some possibly useful coding advices :
I'd always use Me keyword and refer to Userform controls.
this will also have Intellisense help you in finding UserForm controls names
your cbSelect.Value setting code can be shortened to one line
as a result of all what above:
Private Sub TextBox1_Change()
Me.cbSelect= InStr(Me.TextBox1.Value, Me.cbSelect.Caption) > 0
End Sub
Private Sub cbSelect_Click()
With Me.TextBox1
.Value = IIf(Me.cbSelect, _
.Value & Me.cbSelect.Caption, _
Replace(.Value, Me.cbSelect.Caption, vbNullString))
End With
End Sub