Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Raise Event problem

Hi!
We cannot find any errors in the code. The event IDVikt raises but it never calls the code for the event in the module.

This is the class module code:
Option Explicit

Private WithEvents mittObjekt As Matmodul
Private ID, Vikt As Long

Private Sub Class_Initialize()

Set mittObjekt = New Matmodul
End Sub

Private Sub mittObjekt_IDVikt(ByVal lngID As Long, ByVal lngVikt As Long)
ID = lngID
Vikt = lngVikt
End Sub
-----------------------------------------------------------
Here comes the form Matmodul code:
Option Explicit

Public Event IDVikt(ByVal lngID As Long, ByVal lngVikt As Long)

Private Sub cmdNyaVarden_Click()
RaiseEvent IDVikt(10, 10)
End Sub

Please help me!
/mrc



0
mrc121197
Asked:
mrc121197
1 Solution
 
MirkwoodCommented:
Code seems ok to me
0
 
mrc121197Author Commented:
Yes but what is wrong?
0
 
caraf_gCommented:
Are you running the code through the IDE or compiled?

I have had problems with running Event code through the IDE but found that when compiled the problems usually went away.


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mrc121197Author Commented:
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
0
 
mrc121197Author Commented:
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
0
 
mrc121197Author Commented:
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
0
 
caraf_gCommented:
IDE is what you're running in when you are developing VB and you hit the "Play" button to test your application, Compile is when you actually Make the .exe (or whatever) and run it as a real application.
0
 
mrc121197Author Commented:
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
0
 
levazCommented:
You seem to have your code reversed (The way it is currently, no event is being raized as your class does know anything about your command button). Are you trying to raize an event when the command button is clicked ???? If so, statements and routines need to be reversed. You will also need to pass your class your command button so it knows about it.
0
 
caraf_gCommented:
Levaz,

There's nothing wrong with the code. As far as I can see, Matmodul is a Form, and it contains a button. It also has defined an event IDVikt. When the button is clicked, the Form Matmodul raises the event and mittObjekt ought to respond.

The question is, why doesn't it?
0
 
caraf_gCommented:
Sorry, the Class ought to respond. (not mittObjekt)
0
 
mrc121197Author Commented:
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
0
 
mrc121197Author Commented:
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
0
 
caraf_gCommented:
Your Class responds to the Event that is raised by mittObjekt; I erroneously stated that mittObjekt ought to respond to its own event...

Anyway - what I am saying is that as far as I can see, your code is correct and therefore I would expect it to work.
0
 
mrc121197Author Commented:
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
0
 
mrc121197Author Commented:
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
0
 
caraf_gCommented:
See my previous comment
See my previous comment
See my previous comment
See my previous comment
See my previous comment

mrc, your comment is coming through loud and clear; if you are not sure whether your comment has gone through, reload the question (Top right hand corner, link to Reload ?), to check.

I guess you'll have seen my response by now?
0
 
mrc121197Author Commented:
Sorry!

0
 
caraf_gCommented:
No problem.

But - did you try out my suggestion to see whether it's any different between IDE and compiled?
0
 
caraf_gCommented:
mrc,

There is another possibility

Could it be that you are looking at a different instance of the form Matmodul?

For example, if you defined Matmodul as the start up form of your VB application, VB will automatically load an instance of Matmodul into memory and show it to you.

Let's call this "Instance 1"

Then, your class creates a second instance of form Matmodul when you execute
Set mittObjekt = New Matmodul

Your class will respond when mittObjekt raises event IDVikt, but when you click the button on the form that you SEE, you are actually clicking the button on "Instance 1" of the form.

Therefore, "Instance 1" of the form raises the event IDVikt, not mittObjekt

Your class has not been programmed to respond to events from "Instance 1", only to events from mittObjekt

And that could possibly be why nothing happens.

Good luck!!
0
 
mrc121197Author Commented:
caraf_g

Yes that seems to be the problem. :-)
I have tried to make my class module as a form instead and set it as the startup object.
I use the same code in the form as in the class and then use mittObjekt.Show to show the
form. It's working! But I have not figured out how to do the same thing if I use my class
module instead. I guess I have to use a Sub Main() as start up object but I dont know how??
/mrc
0
 
caraf_gCommented:
Good!

OK, reject levaz' answer, and tell me exactly what you're trying to achieve and I'll give you some sample code in an answer.
0
 
ryanvsCommented:
Actually levaz is very close, but didn't tell you exactly what to do...

The problem is in mittObjekt Class_Initialize and how
Matmodul is shown.  You didn't specify, but I suspect
Matmodul is the StartUp form.  This is a problem because
the statement: Set mittObjekt = New Matmodul
Only allocates a New Matmodul, but does not reference
the Matmodul Form currently shown.  To fix this, you need
to change how the Matmodul is displayed.  


Option 1:
Change Project StartUp to Sub Main (just
do not Matmodul.Show except through the Class
as I'll show just below)
In a separate module put the Main --

Option Explicit
Private oMat As ClassMat  ' You didn't name the Class, so I guessed
Sub Main()
  Set oMat = New CMatModule
End Sub

In the Class (I just called it ClassMat) --
Change the Class_Initialize to:
Private Sub Class_Initialize()
  Set mittObjekt = New Matmodul
  Load mittObjekt
  mittObjekt.Show
End Sub



Option 2:
I usually raise events from the Class and not the Form,
although it works either way.  Your particular example would
be better to just use a property, sub, or function.

In Class ClassMat --
Private Sub IDVikt(ByVal lngID As Long, ByVal lngVikt As Long)
  ID = lngID
  Vikt = lngVikt
End Sub


In Form MatModule --
Option Explicit
Dim oMat as New ClassMat
Private Sub cmdNyaVarden_Click()
    oMat.IDVikt(10, 10)
End Sub



Option 1 is more like the example you want, but I thought I'd give you both...

Hope this clears things up...  Good luck.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now