mrc121197
asked on
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
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
Code seems ok to me
ASKER
Yes but what is wrong?
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.
I have had problems with running Event code through the IDE but found that when compiled the problems usually went away.
ASKER
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
Does it matter if I use a standard EXE or an ActiveX EXE?
ASKER
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
Does it matter if I use a standard EXE or an ActiveX EXE?
ASKER
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
Does it matter if I use a standard EXE or an ActiveX EXE?
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.
ASKER
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
Does it matter if I use a standard EXE or an ActiveX EXE?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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?
Sorry, the Class ought to respond. (not mittObjekt)
ASKER
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
"Sorry, the Class ought to respond. (not mittObjekt)"
ASKER
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
"Sorry, the Class ought to respond. (not mittObjekt)"
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.
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.
ASKER
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
"Sorry, the Class ought to respond. (not mittObjekt)"
ASKER
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
"Sorry, the Class ought to respond. (not mittObjekt)"
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?
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?
ASKER
Sorry!
No problem.
But - did you try out my suggestion to see whether it's any different between IDE and compiled?
But - did you try out my suggestion to see whether it's any different between IDE and compiled?
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!!
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!!
ASKER
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
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
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.
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.
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.
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.