Solved

Raise Event problem

Posted on 1999-01-07
23
186 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
ID: 1454579
Code seems ok to me
0
 

Author Comment

by:mrc121197
ID: 1454580
Yes but what is wrong?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1454581
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mrc121197
ID: 1454582
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
ID: 1454583
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
ID: 1454584
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
ID: 1454585
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
ID: 1454586
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
ID: 1454587
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
ID: 1454588
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
ID: 1454589
Sorry, the Class ought to respond. (not mittObjekt)
0
 

Author Comment

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

Author Comment

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

Expert Comment

by:caraf_g
ID: 1454592
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
ID: 1454593
What do you mean by:
"Sorry, the Class ought to respond. (not mittObjekt)"
0
 

Author Comment

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

Expert Comment

by:caraf_g
ID: 1454595
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
ID: 1454596
Sorry!

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1454597
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
ID: 1454598
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
ID: 1454599
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
ID: 1454600
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
ID: 1454601
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

820 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