Solved

Raise Event problem

Posted on 1999-01-07
23
183 Views
Last Modified: 2010-05-03
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
Comment
Question by:mrc121197
23 Comments
 
LVL 13

Expert Comment

by:Mirkwood
Comment Utility
Code seems ok to me
0
 

Author Comment

by:mrc121197
Comment Utility
Yes but what is wrong?
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
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
 

Author Comment

by:mrc121197
Comment Utility
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
0
 

Author Comment

by:mrc121197
Comment Utility
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
0
 

Author Comment

by:mrc121197
Comment Utility
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
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
 

Author Comment

by:mrc121197
Comment Utility
I dont know what you mean by IDE and Compile.
Does it matter if I use a standard EXE or an ActiveX EXE?
0
 

Accepted Solution

by:
levaz earned 100 total points
Comment Utility
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
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
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
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
Sorry, the Class ought to respond. (not mittObjekt)
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mrc121197
Comment Utility
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
0
 

Author Comment

by:mrc121197
Comment Utility
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
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
 

Author Comment

by:mrc121197
Comment Utility
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
0
 

Author Comment

by:mrc121197
Comment Utility
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
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
 

Author Comment

by:mrc121197
Comment Utility
Sorry!

0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
No problem.

But - did you try out my suggestion to see whether it's any different between IDE and compiled?
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
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
 

Author Comment

by:mrc121197
Comment Utility
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
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
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
 
LVL 2

Expert Comment

by:ryanvs
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now