Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Withevents question

Status
Not open for further replies.

MikeBronner

Programmer
May 9, 2001
756
US
What exactly does WithEvents doo when declaring a variable?
What would happen if I don't use WithEvents?
What are the benefits of using WithEvents?

Thanks!
 
you just will not have access to any of the events of the object you are creating.....there are some handy events for recordsets and such.
 
Could you give me a small example? What kind of events are these? Thanks a bunch.
 
First: You only use WithEvents when you declare an object of any kind. (Other variables doesn't have any events so the keyword would be meaningless.)

Second: The keyword is optional, wich means that you can declare any objects you like without catching the events it supports.

And so the example you wanted, it won't be in code, because I'm a bit short on time.
If you declare a recordset using the WithEvents keyword you will gain access to an number of cool events. For instance you could populate your recordset in the background, and continue to run your code while you wait for the recordset to be fully populated. When that happens the recordset FetchComplete event will fire.
You could use the FetchProgress event to control a progress bar.
The WillChangeField and WillChangeRecord events are very handy when you need to control that the user haven't entered any illegal data in a control.

Use the Object Browser and the objects documentation to determine witch events the object supports, and when to use them.

To sum up, the WithEvents keyword can be extremely handy in some situations, but don't use it if you won't handle any of the events the object supports as it is a (minor) performance drag.

Good Luck!
-Mats Hulten
 
Hi,

In relation to this:

You can declare arrays of objects, but not with the withevents keyword. Is there any way to get around that?

Sunaj
 
Not that I know of.
How would the compiler know wich event to call in an array?

-Mats
 
Thanks Mats!

You answered my question. Is there a resource page somewhere that describes all the functions that WithEvents enables for recordsets?

Best Regards,
Michael G. Bronner
 
Yup... check out MSDN under Platform SDK/Data Services/MDAC SDK/ADO/Programmers Reference

Good Luck!
-Mats Hulten
 
Michael, you may also like to see thread222-912603 in which I used WithEvents in the code I posted there.

In that code, WithEvents is used to capture the events of a control outside the form's code module, in an independent and separate class module. This is one of the useful applications of WithEvents, i.e., to capture the events of an object in an external class.

This code may give you some idea what you can do with WithEvents and what are its uses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top