Events trapped in class module

J

Jess

hI!

The below code traps the keypress event in a class module for two given
textboxes -text1 & text2-.


How can I modify the below code to get the keypress event trapped in this
class module for every textbox on this form? I have a form with 30 textboxes

I would like the CTextBxN class to raise events in my form. Therefore an
array of classes may not acceptable since the class has been declared with
the withevents keyword.

Thanks for your help




'*************************************************************
'this code can trap events in a class module for two textboxes
'form code
'text1 is a textbox on this form

Option Compare Database
Option Explicit


'class declared with withevents keyword since events will be raised in this
form

Dim withevents amount As CTextBxN




Private Sub Form_Close()



End Sub

Private Sub Form_Load()


Set amount = New CTextBxN


Set amount.AddTextBox = Text1
Set amount.AddTextBox = Text2


End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

End Sub


Private Sub Text2_KeyPress(KeyAscii As Integer)


End Sub



'class code


Option Compare Database
Option Explicit


'Event dummy(dummy As String)

Private counter As Integer

Private WithEvents TextBox1 As TextBox
Private WithEvents TextBox2 As TextBox

Public Property Set AddTextBox(avalue As TextBox)

counter = counter + 1

MsgBox (counter)

If counter = 1 Then Set TextBox1 = avalue
If counter = 2 Then Set TextBox2 = avalue



End Property


Private Sub TextBox1_KeyPress(KeyAscii As Integer)



Select Case KeyAscii

Case 0 To 31
Case 48 To 57
Case Else
KeyAscii = 0
End Select

End Sub

Private Sub TextBox2_KeyPress(KeyAscii As Integer)



Select Case KeyAscii

Case 0 To 31
Case 48 To 57
Case Else
KeyAscii = 0
End Select

End Sub

Private Sub Class_Initialize()

counter = 0

End Sub
 
S

Stefan Hoffmann

hi Jess,
I would like the CTextBxN class to raise events in my form. Therefore an
array of classes may not acceptable since the class has been declared with
the withevents keyword.
Create a wrapper class using a collection:

---
Option Compare Database
Option Explicit

Private m_Collection As VBA.Collection

Public Sub Bind(AForm As Access.Form)

Dim Control As Access.Control

For Each Control In AForm.Controls
If TypeOf Control Is Access.TextBox Then
Hook Control
End If
Next Control

End Sub

Private Sub Unbind()

'Clear the collection and drop the hooks.

End Sub

Private Sub Class_Initialize()

Set m_Collection = New VBA.Collection

End Sub

Private Sub Class_Terminate()

' Loop over the collection and
' clear the objects before.
Set m_Collection = Nothing

End Sub

Private Sub Hook(ATextBox As Access.TextBox)

Dim HookedTextBox As CTextBxN

Set HookedTextBox = New CTextBxN
HookedTextBox.TextBox = ATextBox
m_Collection.Add HookedTextBox

End Sub
---

And call it in your form only once:

Private m_WrapperCollection As WrapperCollection

Private Sub Form_Close()

WrapperCollection.Unbind

End Sub

Private Sub Form_Load()

Set WrapperCollection = New WrapperCollection

WrapperCollection.Bind Me.Forn

End Sub


mfG
--> stefan <--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top