Withevents Textbox (1 Viewer)

Mario R. Perez

New member
Local time
Today, 08:59
Joined
May 14, 2020
Messages
12
Hi All, could you help me where is my mistake.
1: In the form call to a Class

Option Compare Database
Dim textmayusculas As ClassTextos

Private Sub Form_Load()

set textmayusculas = New ClassTextos
Set textmayusculas.Movimiento = Me
end sub

2: class: ClassTextos
Option Compare Database

Private WithEvents Letracapital As Access.TextBox
Dim mcol As Collection


Public Property Set Movimiento(ByRef FRM As Form)
Dim ctl As Control
For Each ctl In FRM.Controls
Select Case ctl.Name
Case "FldName", "FldAddress", "FlsRep"
Set Letracapital = ctl
Letracapital.AfterUpdate = "[Event Procedure]"
mcol.Add Letracapital, ctl.Name


Set Letracapital = Nothing
End Select
Next ctl
End Property


Private Sub Letracapital_AfterUpdate()
'On Error Resume Next
Letracapital.value = StrConv(Letracapital.Text, vbProperCase)
End Sub

When I run it, only the "FldName" textbox is executed, the other "FldAddress", "FlsRep", are not executed.

thanks for advance
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 08:59
Joined
May 21, 2018
Messages
8,529
That cannot work as written. As it loops through the controls the first control is set to Letracapital, then the next over writes it and is set to letracapital, and so on. The last control read will be set to letracapital. That is the only ctrl whose event will be trapped. Also this can be done very simply without any classes.
Simply build a public function
Code:
Public Function Capitalize()
  me.activeControl.value = strConv(nz(activeControl.text,""),vbProperCase)
End Function

In design view select all the pertinent controls. In the AfterUpdate event type in
=Capitalize()
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 08:59
Joined
May 21, 2018
Messages
8,529
To do this with a class you need two classes. In Access you see many classes and their collection class. Form, Forms: Control, Controls, Field, Fields etc.
I made TextBoxCapital and TextBoxCapitals. (Actually to be grammatically correct it should be TextBoxesCapital)

TextBoxCapital
Code:
Private WithEvents m_txtCapital As Access.TextBox
Public Sub Initialize(TheTextBox As Access.TextBox)
  Set m_txtCapital = TheTextBox
  m_txtCapital.AfterUpdate = "[Event Procedure]"
End Sub


Private Sub m_txtCapital_AfterUpdate()
  m_txtCapital.Value = StrConv(Nz(m_txtCapital, ""), vbProperCase)
End Sub

Now the custom Collection
TextBoxCapitals
Code:
Private TCs As New Collection

Public Function add(TheTextBox As Access.TextBox) As TextBoxCapital
  Dim TC As New TextBoxCapital
  TC.Initialize TheTextBox
  TCs.add TC, TheTextBox.Name
  Set add = TC
End Function
Public Function Item(Index As Variant) As TextBoxCapital
  Set Item = TCs(Index)
End Function
Public Sub Remove(Index As Variant)
  TCs.Remove (Index)
End Sub

I would never do this
Code:
Select Case ctl.Name
Case "FldName", "FldAddress", "FlsRep"
Too much work, hard to debug, spelling has to be perfeot, not flexible. You would never do this in a class module anyways. A class module should be universal. Instead you tied the class to one and only one form.

I would tag each control. "ProperCase". Then in the form.
Code:
Private TCs As New TextBoxCapitals

Private Sub Form_Load()
  Dim ctrl As Access.Control
  For Each ctrl In Me.Controls
    If ctrl.Tag = "ProperCase" Then
      TCs.add ctrl
    End If
  Next ctrl
End Sub

Although this is overkill for what you wanted to do (you could do that in a single line of code as shown above), this custom collection class is an extremely important concept and if your class trapped many events and was more robust this would be the way to accomplish this.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 13:59
Joined
Sep 21, 2011
Messages
14,306
Might want to start using Option Explicit as well ?
 

Mario R. Perez

New member
Local time
Today, 08:59
Joined
May 14, 2020
Messages
12
To do this with a class you need two classes. In Access you see many classes and their collection class. Form, Forms: Control, Controls, Field, Fields etc.
I made TextBoxCapital and TextBoxCapitals. (Actually to be grammatically correct it should be TextBoxesCapital)

TextBoxCapital
Code:
Private WithEvents m_txtCapital As Access.TextBox
Public Sub Initialize(TheTextBox As Access.TextBox)
  Set m_txtCapital = TheTextBox
  m_txtCapital.AfterUpdate = "[Event Procedure]"
End Sub


Private Sub m_txtCapital_AfterUpdate()
  m_txtCapital.Value = StrConv(Nz(m_txtCapital, ""), vbProperCase)
End Sub

Now the custom Collection
TextBoxCapitals
Code:
Private TCs As New Collection

Public Function add(TheTextBox As Access.TextBox) As TextBoxCapital
  Dim TC As New TextBoxCapital
  TC.Initialize TheTextBox
  TCs.add TC, TheTextBox.Name
  Set add = TC
End Function
Public Function Item(Index As Variant) As TextBoxCapital
  Set Item = TCs(Index)
End Function
Public Sub Remove(Index As Variant)
  TCs.Remove (Index)
End Sub

I would never do this
Code:
Select Case ctl.Name
Case "FldName", "FldAddress", "FlsRep"
Too much work, hard to debug, spelling has to be perfeot, not flexible. You would never do this in a class module anyways. A class module should be universal. Instead you tied the class to one and only one form.

I would tag each control. "ProperCase". Then in the form.
Code:
Private TCs As New TextBoxCapitals

Private Sub Form_Load()
  Dim ctrl As Access.Control
  For Each ctrl In Me.Controls
    If ctrl.Tag = "ProperCase" Then
      TCs.add ctrl
    End If
  Next ctrl
End Sub

Although this is overkill for what you wanted to do (you could do that in a single line of code as shown above), this custom collection class is an extremely important concept and if your class trapped many events and was more robust this would be the way to accomplish this.
Thanks Majp, You know that I follow your code through your comments, I'm just starting to understand the classes and objects.
 

Mario R. Perez

New member
Local time
Today, 08:59
Joined
May 14, 2020
Messages
12
Thanks Majp, You know that I follow your code through your comments, I'm just starting to understand the classes and objects.
Hi MaJP, In which event I should put "Remove" to empty the item from the collection. Thanks again!
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 08:59
Joined
May 21, 2018
Messages
8,529
Here is a more detail discussion on custom collections

I do not understand your question. But on your main form to remove an item
TCs.remove(0)
Would remove first item
TCs.remove("control name")
Would remove by name.
 

Users who are viewing this thread

Top Bottom