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?

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

    module calling another module

    SharonInGa, Aug 23, 2004, in forum: Microsoft Access VBA Modules
    Replies:
    2
    Views:
    666
    John Vinson
    Aug 23, 2004
  2. Guest

    Convert Class Module to Standard Module

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

    Class Module vs. Module - Philosophical Differences

    Guest, Aug 9, 2005, in forum: Microsoft Access VBA Modules
    Replies:
    1
    Views:
    661
    Marshall Barton
    Aug 10, 2005
  4. 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,810
    Nikos Yannacopoulos
    Apr 14, 2006
  5. Guest

    Module or Class Module

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

    Can't "Call" Error Handler in Standard Module from Form Module

    Guest, Apr 20, 2007, in forum: Microsoft Access VBA Modules
    Replies:
    8
    Views:
    678
    Guest
    Apr 21, 2007
  7. C Hayes

    HDI: from module, open form, pass value back into module?

    C Hayes, Feb 14, 2008, in forum: Microsoft Access VBA Modules
    Replies:
    3
    Views:
    439
    C Hayes
    Feb 15, 2008
  8. Jess

    Event fired in class module (revisisted)

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