March 4, 2021
Hot Topics:

The COM Course - Part 2

  • By Karl Moore
  • Send Email »
  • More Articles »

Let's dive straight into the deep end and start creating our first real COM object, neatly sitting inside one of those ActiveX programs. Here goes:

  • Launch Visual Basic

Observe the icons currently on that 'New Project' screen. You should have a few that begin with ActiveX. Let's explain each of those now:

  • ActiveX DLL  A .DLL program that contains classes. This is our stop!
  • ActiveX EXE  A .EXE program that contains classes. This will be our stop later!
  • ActiveX Control  A project that allows you to create controls, those widgets in your toolbox. Not our stop. To find out more, click here for our ActiveX Control tutorial.
  • ActiveX Document EXE  A program that runs on a Web page, in .EXE format. Certainly not our stop.
  • ActiveX Document DLL  As above, but .DLL format. Not even remotely near our stop.

Incidentally, if you don't have all of these items in your list, don't worry. You've probably just got a cheap, substandard version of Visual Basic. Ho-humm.

Anyway, we're really only interested in those first two options  the ActiveX DLLs and Active EXEs. We'll come back to the latter later but deal with the former foremost.

  • Click 'ActiveX DLL'
  • Hit OK

Great! Now let me briefly explain what this project I fancy knocking up is all about. I want to create an ActiveX DLL that serves as a front-end to the Customers table in the Northwind database (ships with Visual Basic, usually found in the VB98 folder).

This will mean that in future when I want to access Customers information, I'll only need to use my class  and not mess around with tonnes of sticky data access code.

Heck, you've still gotta create the darn class  but as I said earlier, COM makes your life a lil' bit more difficult  just before making it one heckuva lot easier.

Before dipping into database whatnots, let's first christen our ActiveX program:

  • Change the Name property of your class to 'Customers'
  • Click 'Project', 'Project Properties' and change the Project Name to 'Northwind'

Brilliant! Now let's get our class connected to that database:

  • Click 'Project', 'References'
  • Select 'Microsoft ActiveX Data Objects 2.1 Library'

This reference will allow us to access a database. And in the real world, that's all most COM objects do all day  sit around opening recordsets. It's an exciting life.

  • Add the following code to your class:
Dim rs As Recordset

This is the recordset object that will access our database.

Now, when someone starts the class, we want to kick start that recordset object and open a connection to the database. And when they stop using it, we want to close that connection. Let's code this now:

  • In the 'Object' combo, change the current '(General)' to 'Class'
  • Ensure 'Initialize' is selected in the adjacent combo

Your code window should currently say something like:

Private Sub Class_Initialize()End Sub

Any code you enter here fires up when the class first starts. It's a lot like the Form_Load event.

  • Enter the following code in the 'Initialize' event:
Set rs = New Recordset    rs.ActiveConnection = "Provider=Microsoft." & _	"Jet.OLEDB.4.0;Data Source=C:\Program Files\" & _	"Microsoft Visual Studio\VB98\Nwind.mdb;" & _	"Persist Security Info=False"rs.Open "select * from customers", , adOpenKeyset, adLockOptimistic

It's worth noting here that this isn't special class code. It's just regular ADO database access code that you could use anywhere in Visual Basic. We're just using it here to demonstrate how to open a database connection inside a class.

Top Tip: If your Northwind database isn't in C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb  you'll have to change the above ActiveConnection string!

So, when this class fires up, the rs object will have a link to the database. Now when the class object ends due to the program using it closing or whatever, we want to close that connection.

We code this in the Terminate event, which is pretty similar to Form_Unload. For a start, they both have an 'n' in their names. But more relevantly, both events fire when that particular object closes.

Let's enter our database close code now:

  • Select 'Class' from the Object combo and 'Terminate' from the Procedure combo
  • In the 'Terminate' event, enter the following code:
rs.CloseSet rs = Nothing

Once again, this is just regular stuff. Here we're simply closing the database, then being all good and setting rs to nothing, which calls in the heavies to effectively erase the fact that rs ever existed.

Great! Let's continue on the next page, where we'll be entering code to access records in our database!

Page 3 of 9

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