January 27, 2021
Hot Topics:

Referencing an Object

  • By Neil Avent
  • Send Email »
  • More Articles »

Now we can have a go at instancing this class which will give us access to the items contained in it at both design-time and run-time. If you have ever used a class module to do this then you will know what we are going to do next as it is exactly the same procedure.

Close the Object Browser. In the General Declarations for the form add this line.

Dim Withevents IE as InternetExplorer

As you start to type "InternetExplorer" a dropdown box should appear to signify that the object is available for use in your application. This tells you that the object is correctly referenced to your project. This is usually how you would reference any class to your project as it demonstrates Early Binding. I will be explaining Early vs. Late Binding fully in another section. The Withevents keyword in this statement is optional - I have included it here to signify how we can use it, even though it isn't necessary in this demonstration.

If you want to see what events this will possibly generate for this project, then you can look in the drop-down menu as you would for a textbox or any other control on your form. You will notice that all the new events are under IE which is the name we have given to our InternetExplorer Class.

Note: Modules do not handle events, so this code must be used in a form.

Let's see what this InternetExplorer class does for us. Add 3 command buttons to the form. Call them cmdStart, cmdVisible and cmdStop. Then give them suitable captions.

Under cmdStart add the following code for the Click Event.

Set IE = New InternetExplorer
Cmdstart.enabled = False
CmdStop.enabled = True

Under cmdvisible add the following code to the Click Event.

ie.Visible = Not (ie.Visible)

Under cmdQuit add the following code for the Click Event.

Set ie = Nothing
cmdStart.Enabled = True
cmdVisible.Enabled = False

And finally in the form load event add this:

cmdStop.Enabled = False
cmdVisible.Enabled = False

Then run the project and see what happens. Your form comes up on the screen. Click the start button. You may see your mouse pointer may show the busy icon for a second, then your form loses the focus. Nothing really noticeable has happened though. Now try the visible button, Internet Explorer appears, you can toggle it by repeatedly clicking on the Visible button. You can also use Internet Explorer as you usually would. Clicking on Stop closes it down.

That's not bad for a few lines of code is it? Especially as more than half of your code was making buttons enabled at various times. You have just created your first object! In the next section, I will explain the code in more detail to help you understand what is going on.

Page 3 of 6

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date