corentint
New Member
- Joined
- Jan 31, 2022
- Messages
- 32
- Office Version
- 365
- Platform
- Windows
Hello all,
I am trying to educate myself about class modules. I have this exemple from John Walkenbach (the code in blue - works fine).
I am trying to do this for a group of textboxes. But somehow, it does not work for me (code in red below).
Read on, if anyone has a solution, I would appreciated very much.
Thanks in advance.
I am trying to educate myself about class modules. I have this exemple from John Walkenbach (the code in blue - works fine).
I am trying to do this for a group of textboxes. But somehow, it does not work for me (code in red below).
Read on, if anyone has a solution, I would appreciated very much.
Thanks in advance.
From userform2 (just a userform with 7 textboxes and an Exit command: | From userform1 (just a userform with 16 command buttons | |
Dim Tboxes() As New TxtClass | Dim Buttons() As New BtnClass | |
Private Sub Exit2_Click() | Private Sub OKButton_Click() | |
Unload Me | Unload Me | |
End Sub | End Sub | |
Private Sub UserForm_Initialize() | Private Sub UserForm_Initialize() | |
Dim TboxCount As Integer | Dim ButtonCount As Integer | |
Dim ctl As Control | Dim ctl As Control | |
' Create the Textbox objects | ' Create the Button objects | |
TboxCount = 0 | ButtonCount = 0 | |
For Each ctl In UserForm2.Controls | For Each ctl In UserForm1.Controls | |
If TypeName(ctl) = "TextBox" Then | If TypeName(ctl) = "CommandButton" Then | |
TboxCount = TboxCount + 1 | If ctl.Name <> "OKButton" Then 'Skip the OKButton | |
ButtonCount = ButtonCount + 1 | ||
ReDim Preserve Tboxes(1 To TboxCount) | ReDim Preserve Buttons(1 To ButtonCount) | |
Set Tboxes(TboxCount).TextboxGroupe = ctl | Set Buttons(ButtonCount).ButtonGroup = ctl | |
End If | ||
End If | End If | |
Next ctl | Next ctl | |
End Sub | End Sub | |
From the Class module (named: TxtClass): | From the Class module (named : BtnClass): | |
Public WithEvents TextboxGroupe As msforms.textbox this statement looks amiss w/b: MSForms,TextBox as per VBE | Public WithEvents ButtonGroup As CommandButton | |
Private Sub TextboxGroupe_BeforeUpdate(ByVal cancel As msforms.ReturnBoolean) | Private Sub ButtonGroup_Click() | |
Dim Msg As String | Dim Msg As String | |
Msg = TextboxGroupe.Name & " was updated by the user" | Msg = "You clicked " & ButtonGroup.Name & vbCrLf & vbCrLf | |
Msg = Msg & "Caption: " & TextboxGroupe.Name & vbCrLf | Msg = Msg & "Caption: " & ButtonGroup.Caption & vbCrLf | |
Msg = Msg & "Left Position: " & TextboxGroupe.Left & vbCrLf | Msg = Msg & "Left Position: " & ButtonGroup.Left & vbCrLf | |
Msg = Msg & "Top Position: " & TextboxGroupe.Top | Msg = Msg & "Top Position: " & ButtonGroup.Top | |
MsgBox Msg, vbInformation, TextboxGroupe.Name | MsgBox Msg, vbInformation, ButtonGroup.Name | |
End Sub | End Sub | |
The only other procedure in one normal module is a call to display the Userform1. | ditto with userform2 | |
This programme does nothing. Not error. | ||
But I found out that the events BeforeUpdate or AfterUpdate simply don't fire up; the event "change" does work, but that is hardly usable. | ||
Why? | The above procedure works fine... | |
'The text in blue, parallel to this red one, is the same procedure to group command buttons. |