Solved

Raise Event problem

Posted on 1999-01-07
23
185 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

809 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