Dynamic Behaviors in VB6

To most VB6 programmers, how the application behaves is based on how it was written — prior to compiling, that is.

There’s just no way you can make it behave differently or dynamically once you compile it, right?

Well, you’re right…. meaning, whether your VB6 code is dynamic or not depends on how you wrote it 🙂

I’ll show you how to write VB6 codes that has dynamic behavior — meaning, it can do different things at runtime.

Setup

For this demo, we’ll create a solution (or group) with a Standard EXE and an ActiveX DLL.

Standard EXE

Fire up your VB6 IDE and create a new Standard EXE project. This will give you with a default project and form (the usual).

We’ll add 2 option buttons, 1 textbox, 1 command button and 1 listbox. I’ve designed my form like this:

Feel free to design your form differently 🙂

Now, before we write any code to it, let’s add an ActiveX DLL project to it (go to File menu then Add Project).

ActiveX DLL

Once this is added, rename the default “Class1” to “Boogle”. Then add another class module and name it “Ging”.

Lastly, rename the ActiveX DLL project as “SearchEngines”.

Your project tree should look like this:

Next, we’ll just write a dummy search routine for both classes. Don’t worry about the codes, I’ll post the link to my GitHub page for this blog so you can just clone it and run it on your PC 🙂

For the sake of brevity, we simply populated a collection with some string. The point of this blog is to show you how to write dynamic VB6 codes, not to write a search engine 🙂

Back to Standard EXE

Now that we’ve written the DLL, we can now write the dynamic codes that will consume the classes.

CreateObject

We’re going to use the CreateObject function to dynamically load or instantiate a class from our search engine. This is how you dynamically create an object in VB6.

Notice how we never referenced the DLL in our project? Yep, that’s the cool thing about this 🙂

CallByName

We’ll use the CallByName function to dynamically execute a method of an object using a string. This is how you can dynamically define what method to invoke on any object. Cool, right? 🙂

Key Points

The option buttons only do two(2) things:

  • It sets the class ID of the class, which is just a string. At this point, we’re not creating an object yet.
  • It sets the method name for the particular class that is selected. At this point, we’re not invoking it yet.

The command button puts them all together. This is where the selected (search engine) class is created, and its method invoked. We used the “vbMethod” constant to tell VB6 that we’re invoking a method. We also pass the search string to it as the method parameter.

Here’s what the full code looks like:

Testing

Okay, nothing else to do but test the code! 🙂

Boogle

Here’s the result of selecting Boogle:

Ging

And here’s the result for Ging:

Other use cases

Even on just Standard EXE projects, you can use these methods.

For example, you can use CallByName to change the Forms caption:

A very useful use case is launching an external application. For example, you can create and launch an Excel application using CreateObject method:

This is what you’ll need to do if you want to avoid Office version issues. If you reference an Excel 97 in your project and the target machine is Excel 2000, your code will probably break since the Excel versions (DLL) won’t match.

However, the CreateObject uses the version on the machine. So it’d run Excel 97 on your machine and Excel 2000 on the target machine (as described in our scenario). Try it, it’s cool 🙂

Source Codes

https://github.com/vegitz/codes/tree/master/0031%20VB6%20Dynamic%20Behaviors

Leave a Comment