Textbox event fired in class module

Discussion in 'Microsoft Access VBA Modules' started by Jess, Apr 13, 2009.

  1. Jess

    Jess Guest

    How can I get a given control event -let say the textbox keypress event- to
    be fired in a class module? I believe this is called multicasting in VB6.

    The below code is the one I am unsuccessfully using: the TextBox_KeyPress is
    not fired in the class module.

    Form:

    Dim amount As CTextBxN

    Private Sub Form_Load()

    Set amount = New CTextBxN

    ‘text1 is a textbox control on this form
    Set amount.TextBox = Text1

    End Sub

    CTextBxN Class:


    Public WithEvents TextBox as TextBox


    public Sub TextBox_KeyPress(KeyAscii As Integer)

    ….event code…

    End Sub

    I have noticed that if the form has a declaration for the keypress event,
    the latter is fired first in the form, and then in the class module.

    In other words. I the below snippet of code is added to the form, the
    keypress event is fired first in the form, and second in the class module.

    public Sub TextBox_KeyPress(KeyAscii As Integer)


    End Sub

    Can anybody help me with this?

    Thanks in advance
     
    Jess, Apr 13, 2009
    #1
    1. Advertisements

  2. hi Jess,
    Jess wrote:
    > How can I get a given control event -let say the textbox keypress event- to
    > be fired in a class module? I believe this is called multicasting in VB6.

    Start with this:

    The forms code:
    ---
    Option Compare Database
    Option Explicit

    Private m_TextBox As clsTextBox

    Private Sub Form_Close()

    Set m_TextBox = Nothing

    End Sub

    Private Sub Form_Load()

    Set m_TextBox = New clsTextBox
    Set m_TextBox.TextBox = Text0

    End Sub

    Private Sub Text0_KeyPress(KeyAscii As Integer)

    MsgBox KeyAscii & " from form."

    End Sub
    ---

    The class' (clsTextBox) code:
    ---
    Option Compare Database
    Option Explicit

    Private WithEvents m_TextBox As Access.TextBox

    Public Property Set TextBox(AValue As Access.TextBox)

    Set m_TextBox = AValue

    End Property

    Private Sub m_TextBox_KeyPress(KeyAscii As Integer)

    MsgBox KeyAscii & " from class."

    End Sub
    ---


    mfG
    --> stefan <--
     
    Stefan Hoffmann, Apr 13, 2009
    #2
    1. Advertisements

  3. Jess

    Jess Guest

    Stephan,

    Thanks for your answer. I have a question:

    Is there a way to accomplish this without declaring the keypress event in
    the form?

    I would like the keypress event to be trapped in the class module for every
    textbox. Do I have to declare the keypress event for every textbox in the
    form?

    Thanks

    "Stefan Hoffmann" wrote:

    > hi Jess,
    > Jess wrote:
    > > How can I get a given control event -let say the textbox keypress event- to
    > > be fired in a class module? I believe this is called multicasting in VB6.

    > Start with this:
    >
    > The forms code:
    > ---
    > Option Compare Database
    > Option Explicit
    >
    > Private m_TextBox As clsTextBox
    >
    > Private Sub Form_Close()
    >
    > Set m_TextBox = Nothing
    >
    > End Sub
    >
    > Private Sub Form_Load()
    >
    > Set m_TextBox = New clsTextBox
    > Set m_TextBox.TextBox = Text0
    >
    > End Sub
    >
    > Private Sub Text0_KeyPress(KeyAscii As Integer)
    >
    > MsgBox KeyAscii & " from form."
    >
    > End Sub
    > ---
    >
    > The class' (clsTextBox) code:
    > ---
    > Option Compare Database
    > Option Explicit
    >
    > Private WithEvents m_TextBox As Access.TextBox
    >
    > Public Property Set TextBox(AValue As Access.TextBox)
    >
    > Set m_TextBox = AValue
    >
    > End Property
    >
    > Private Sub m_TextBox_KeyPress(KeyAscii As Integer)
    >
    > MsgBox KeyAscii & " from class."
    >
    > End Sub
    > ---
    >
    >
    > mfG
    > --> stefan <--
    >
     
    Jess, Apr 13, 2009
    #3
  4. hi Jess,

    Jess wrote:
    > I would like the keypress event to be trapped in the class module for every
    > textbox. Do I have to declare the keypress event for every textbox in the
    > form?

    Ah, okay. I didn't understand it the first time. Afaik you need this
    modification (in my example):

    clsTextBox:
    ---
    Public Property Set TextBox(ATextBox As Access.TextBox)

    Set m_TextBox = ATextBox

    If m_TextBox.OnKeyPress = "" Then
    m_TextBox.OnKeyPress = "[Event Procedure]"
    End If

    End Property
    ---

    Caveat: If a matching event procedure exists in the code behind the
    form, then it is called also.


    mfG
    --> stefan <--
     
    Stefan Hoffmann, Apr 14, 2009
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    Convert Class Module to Standard Module

    Guest, Apr 26, 2005, in forum: Microsoft Access VBA Modules
    Replies:
    2
    Views:
    258
    Guest
    Apr 26, 2005
  2. Guest

    Class Module vs. Module - Philosophical Differences

    Guest, Aug 9, 2005, in forum: Microsoft Access VBA Modules
    Replies:
    1
    Views:
    354
    Marshall Barton
    Aug 10, 2005
  3. tobesurveyor via AccessMonster.com

    Difference between Form's Class Module and Class Module

    tobesurveyor via AccessMonster.com, Apr 14, 2006, in forum: Microsoft Access VBA Modules
    Replies:
    4
    Views:
    1,393
    Nikos Yannacopoulos
    Apr 14, 2006
  4. Guest

    Module or Class Module

    Guest, Sep 5, 2006, in forum: Microsoft Access VBA Modules
    Replies:
    4
    Views:
    175
    Jesper
    Sep 6, 2006
  5. Jess

    Event fired in class module (revisisted)

    Jess, Apr 14, 2009, in forum: Microsoft Access VBA Modules
    Replies:
    0
    Views:
    215
Loading...

Share This Page