VBA disable Checkbox click event from firing inside Textbox change event - excel

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

Related

Userform - Textbox only accept number

I've been searching for a solution to change apperance of my textbox into red when the entered value is not a number.
Actually, I succeeded with a textbox only but I have 14 textboxes at total. I've tried with the codes below:
Private Sub Justi_num()
Dim i As Integer
For i = 2 To 14
If IsNumeric(Controls("TextBox" & i).Value) Then
Controls("TextBox" & i).BackColor = RGB(255, 255, 255) 'Blanc
Else 'Sinon
Controls("TextBox" & i).BackColor = RGB(247, 205, 201) 'Rouge clair
End If
Next i
End Sub
But it didnt work. Any help? Thanks in advance.
You first need to ensure that your code is called whenever the user enters something. This is done with Event-Routines. Controls (like buttons, checkboxes, textboxes and so on) have different events like change (something was changed), dblClick (there was a double click on the control) and so on.
If you want some code to be executed, you need to put code into that event routine. The name of the event routine is combined by the name of the control and the event name. If the name is not following that rule, the code is not called automatically.
In design mode, go to the form editor and double click onto one of your textboxes: The editor will open the text editor window and create a subroutine with the most common event of that control (for a textbox, that's the Change-event, for a button, it would be the Click-event):
Private Sub TextBox1_Change()
End Sub
You could call your routine Justi_num from this routine:
Private Sub TextBox1_Change()
Justi_num
End Sub
However, you will need to create such an event routine for every of your text boxes, there is no way around that (ok, yes, there is, but it is much too complicated for a beginner).
Private Sub TextBox2_Change()
Justi_num
End Sub
Private Sub TextBox3_Change()
Justi_num
End Sub
and so on...
One thing: Your routine Justi_num handles all textboxes, no matter which textbox was changed. You could change the code so that the event routine passes the information which control was changed and the color changing routine handles only that control:
Private Sub TextBox1_Change()
setTextBoxColor Me.TextBox1
End Sub
Private Sub TextBox2_Change()
setTextBoxColor Me.TextBox2
End Sub
(and so on...)
Private Sub setTextBoxColor(tbox As Control)
If IsNumeric(tbox.Value) Or tbox.Value = "" Then
tbox.BackColor = vbWhite
Else
tbox.BackColor = RGB(247, 205, 201)
End If
End Sub
I'll try to help:
One thing that may fail is that "IsNumeric" gives a boolean result. Try using If IsNumeric(Controls("TextBox" & i).Value) = True Then
It would be something like:
Private Sub Justi_num()
Dim i As Integer
Dim NA As Boolean
NA = False
For i = 2 To 14
NA = IsNumeric(Controls("TextBox" & i).Value)
If NA = True Then
Controls("TextBox" & i).BackColor = RGB(255, 255, 255) 'Blanc
Else 'Sinon
Controls("TextBox" & i).BackColor = RGB(247, 205, 201) 'Rouge clair
End If
Next i
End Sub
Hope it helps!

Masking Password using checkbox in VBA Excel

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

Checking and Unchecking a Checkbox

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).

excel macro pass data between two forms where focus is set

I have two forms.
form1 has four text boxes and a button for each textbox.
The button would setfocus on the textbox and bring up form2.
form2 is basicly a number keypad that also has a text box so that the user can select a number. That number will go in form2.textbox, which when changed will put the data in form1.textbox1.
The problem I'm having is how to tell form2.textbox to put data in form1.textbox2.
This is what my code looks like:
Public Sub textbox1_Click()
Me.textbox1.SetFocus
numbfrm.Show
End Sub
Private Sub textbox2_Click()
Me.textbox2.SetFocus
numbfrm.Show
End Sub
Private Sub textbox3_Click()
Me.txtactual.SetFocus
numbfrm.Show
End Sub
This is what is in the number form. It contains all of the numbers 1 to 10, but I just put the first three numbers here.
Private Sub Cmd1_Click()
TxtNumber.Value = TxtNumber.Value & "1"
End Sub
Private Sub Cmd2_Click()
TxtNumber.Value = TxtNumber.Value & "2"
End Sub
Public Sub TxtNumber_Change()
passnumber
End Sub
This is in a module:
Sub passnumber()
form1.textbox1.Value = numbfrm.TxtNumber
End Sub
I've been looking through the web to find an easy way to do that.
I tried puting in the module
Sub passnumber()
If form1.texbox1.foucs =true then
form1.textbox1.Value = numbfrm.TxtNumber
Else If form1.textbox2.foucs = true then
form1.texbox2.value =numbfrm.txtnumber
End sub
I have made a workaround for it, I put toggle buttons next to each box and when button is pressed it would mark it as true, and then I told it if that toggle is true it would use certain text box
Sub passnumber()
If form1.option1.value =true then
form1.textbox1.Value = numbfrm.TxtNumber
else
If form1.option2.value =true then
form1.textbox2.Value = numbfrm.TxtNumber
end if
end if
End sub

Excel VBA checkbox click and change events the same?

I have 5 checkboxes to set some options for a macro, one of them is a Select/Unselect All checkbox. I want to create something similar to what you have on web-based mailboxes when you select mails to delete or mark as read, etc.
When I check the Select/Unselect All checkbox, I turn the rest of the checkboxes's values to true and viceversa when I uncheck it. That's ok.
The problem comes when I also want to validate that if everything is unchecked and one by one I check the other checkboxes, if in the end I check all, then the Select/Unselect All checkbox turns to checked. And viceversa, meaning that if eveything is checked and then I uncheck one of the four others, then I turn the "All" checkbox to false (unchecked).
But it seems that even when I just set the value, for example Option1Checkbox.value = True, in the SelectAllCheckbox_Click event, it triggers both the Option1Checkbox_Click and Option1Checkbox_Change events.
Shouldn't it just trigger the Change event since I'm not actually clicking that checkbox?
What happens is that I check the SelectAll, so it turns Option1 to checked, but by doing this, Option1 triggers also the click event so it unchecks it which triggers the click event again and then unchecks again the All checkbox which in the end leaves everything unchecked, like at the beginning. Hope this part is clear enough.
How can I avoid this behavior? How can I make sure only the Change event is triggered and not the Click event?
Has anyone ever had such an arrangement of checkboxes and had a similar problem? Or how did you manage to do this without the behavior I'm getting?
The checkboxes are not on a form but simply on a worksheet. They are ActiveX controls. And what I have is nothing complicated:
Private Sub SelectAll_Click()
Option1Checkbox.Value = SelectAll.Value
Option2Checkbox.Value = SelectAll.Value
Option3Checkbox.Value = SelectAll.Value
Option4Checkbox.Value = SelectAll.Value
End Sub
Then the options checkboxes click events look like this:
Private Sub Option1Checkbox_Click()
If Option1Checkbox.Value = True And Option2Checkbox.Value = True And Option3Checkbox.Value = True And Option4Checkbox.Value = True Then
SelectAll.Value = True
Else
SelectAll.Value = False
End If
End Sub
It's quite simple, the biggest problem I see is the calls to the click events when the checkbox is not actually click.
Thanks for your help.
I would define a local variable (one defined outside the subs) and set/check that
Option Explicit
Dim ImChangingStuff As Boolean
Private Sub SelectAll_Click()
ImChangingStuff = True
Option1Checkbox.Value = SelectAll.Value
Option2Checkbox.Value = SelectAll.Value
Option3Checkbox.Value = SelectAll.Value
Option4Checkbox.Value = SelectAll.Value
ImChangingStuff = False
End Sub
then your click routines would look like this:
Private Sub Option1Checkbox_Click()
If ImChangingStuff Then Exit Sub
If Option1Checkbox.Value = True And Option2Checkbox.Value = True And Option3Checkbox.Value = True And Option4Checkbox.Value = True Then
SelectAll.Value = True
Else
SelectAll.Value = False
End If
End Sub
To solve this problem,
in your main userform (in this example it is called Mainform) add this code:
Private Sub Mainform_Initialize()
stateCheckbox = True
End Sub
Create a module to store your global variables (eg. globalVariables), and add the following, which will hold the state of the checkbox:
Public stateCheckbox as Boolean
In the _click event of your checkbox, wrap your code like this:
if stateCheckbox = false then
{ whatever your code is }
Else:
stateCheckbox = false
end if
I have had the same issue as described above. I recognized that each change of a Checkbox, no matter if by click or by changing Checkbox.Value, each time first fires the Checkbox_Change event and then the Checkbox_Click event , provided that any of these events are declared.
I would thus recommend to use only one event for each checkbox, p.e. the Checkbox_Change event, and to check if the checkbox has been changed by click or by declaration at the beginning of the Sub. This can be done by comparing the ActiveControl.Name with the Checkbox.Name:
Private Sub CheckBox1_Change()
On Error Goto Err:
If ActiveControl.Name = CheckBox1.Name Then
On Error Goto 0
'Commands for click
Exit Sub
Else
On Error Goto 0
'Commands for change from within the Userform
Exit Sub
Err:On Error Goto 0
'Commands for change from outside the Userform
End Sub
I would write a sub checking if Option1 to Option4 are all checked in an If statement. In a second if statemement, I'd check to see if they are all false:
Sub Are_O1_to_O4_All_Checked ()
If Option1Checkbox.Value = True And _
Option2Checkbox.Value = True And _
Option3Checkbox.Value = True And _
Option4Checkbox.Value = True Then
Option5Checkbox.Value = True ' select/unselect checkbox
End If
If Option1Checkbox.Value = False And _
Option2Checkbox.Value = False And _
Option3Checkbox.Value = False And _
Option4Checkbox.Value = False Then
Option5Checkbox.Value = False ' select/unselect checkbox
End If
End Sub
Then I would Call this sub at the end of each OnClick event for the check boxes.

Resources