Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

raise event in form and capture in class

Status
Not open for further replies.

johngrg

Programmer
Apr 19, 2007
17
US
In VB6,
I open a modal form from class mod. Declared an event in the form. When the user click on OK button in the form, I raise an event and want to capture this event in the class module from where the form was opened. In the class mod, I've implemented WithEvents to capture the raised event from the form, but the control doesn't flow to the class mod when the event is raised in form. What should I be doing to fix this problem?
Thanks.

'frmSelect
Option Explicit

Public Event SelectedItem()

Private Sub cmdOK_Click()
RaiseEvent SelectedItem
End Sub

'class
Private WithEvents mevtSelect As frmSelect

Private Sub DisplayForm()
Dim objfrmSelect As frmSelect
Set objfrmSelect = New frmSelect
objfrmSelect.Load orecordset
objfrmSelect.Show vbModal

Unload objfrmSelect
Set objfrmSelect = Nothing
End Sub

Private Sub mevtSelect_SelectedItem()
---do something
End Sub
 
You are declaring a variable mevtSelect but never initialize it. Instead, you create another form object called objfrmSelect that is local to the DisplayForm procedure.

Replace objfrmSelect with mevtSelect (without dimming it) and it should work.

Here's my code, for the first form:
Code:
Public WithEvents mFrmTwo As frmTwo

Private Sub cmdOpen_Click()
    Set mFrmTwo = New frmTwo
    mFrmTwo.Show vbModal
End Sub

Private Sub mFrmTwo_ButtonClicked()
    MsgBox "frmTwo's close button was clicked"
End Sub

And the code that has the event:
Code:
Public Event ButtonClicked()

Private Sub cmdClose_Click()
    Unload Me
    RaiseEvent ButtonClicked
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top