MetaPro Systems Inc.

Home Services Software Approach Who We Are
Resources Testimonials Contact Us Products

VB Tips & Tricks 31 + 

Visual Basic Tips & Tricks #31 - Creating An Active-X Control

The ability to create a custom OCX can be a very useful feature and not as hard to do as you might think. For example, for one application I created a control array so I did not have to create it for each form. Since the buttons on most of the forms are very similar I can encapsulate those features within the control.  I also established properties that allow the user of the control, me in this case, to override these properties.

I will give you the steps to create a simple control in this tip. Suppose you are writing an application and your users want all the command buttons to be light blue. You could create buttons using the standard Microsoft command button. You can then change the properties each time or cut and paste the completed button. This gets old pretty quickly.

  1. Create a directory to store this tip.
  2. Create a new VB project and choose Active-X control.
  3. Save the project as BlueButton.vbp and the control as BlueButton.ctl.Change the name of the project and control as well.
  4. Add a command button to the form.
  5. Change the properties of the button:
  1. Style – Graphical
  2. Name - MyBlueButton
  3. Caption – “Blue Button”
  4. Back Color – Choose light blue from the palette
  1. Make it just slightly large than the button
  2. Change Back Style to Transparent
  3. Change Name to “BlueButton”
  4. Save project as MyBlueButton.vbp
  5. Name Project MyBlueButton
  6. Close BlueButton control

That’s it for now.There will be more on creating an Active-X control in the next tip.Be sure to save everything before exiting.

Visual Basic Tips & Tricks #32-Adding Properties to your Active-X Control

Open the project group that you created for Tip #31 called BlueButton.vbg.Set the Project “TestButton” as the default project.To do this right-click on “TestButton” within Project Explorer then choose “Set as Startup”. If you look at the button on the form, you will see that there are very few properties.You cannot change the caption on the button; or the width or height of the button.This limitation can be solved by adding properties to the custom control that you created.

For example, here is how to add a caption property to the button.

1)Double click on the “BlueButton” control in the “MyBlueButton” project.Add this code.

Public Property Get Caption() As String

msCaption = MyBlueButton.Caption

End Property

Public Property Let Caption(ByVal psCaption As String)

MyBlueButton.Caption = psCaption

End Property

Also add

Dim msCaption As String

To the Declaratives.

2)Now return to the form in the TestButton project, you will see that there is now a Caption properties on the BlueButton1 control. It can be change at design or runtime just like a normal VB button.

The next tip will show you how to register you custom control.

Visual Basic Tips & Tricks #33-Registering your Active-X Control.

  1. Open the project MyBlueButton.
  2. Under File, choose MyBlueButton.ocx.
  3. Determine where you want to place the OCX.Use the Explorer-like function to find the directory. Usually, it is placed in the Windows/System or Winnt/System directory.
  4. Register the OCX by entering the command Regsrv32 MyBlueButton.ocx. I find it easier to associate RegSRV32 with the OCX suffix.That way, I just click on the OCX to register it.

Now you can use the OCX just like any Microsoft or Third-Party Control.

  1. Open the Project TestButton.vbp.
  2. Save Project TestButton as TestButtonAlone.This is so that you can still use TestButton to test with, if you change the OCX.
  3. Under Components, check MyBlueButton.
  4. Save Project TestButtonAlone.
  5. Exit, do not save frmTest.frm.
  6. Open Project TestButtonAlone.

One final point, if you change your OCX after you compile it, be sure to use Binary Compatibility with the prior version.This option can be found under Project, Properties, Component. Click on binary compatibility and use the Explorer-like process to find your prior version.If you do not do this, you may have to re-link all projects that use the OCX.Note: binary compatibility is not possible if any Public Properties or Methods have been added or changed parameters.

MetaPro Systems Inc. Visual Basic Tips & Tricks #34 - Avoid Using Integer

Although the Visual Basic (VB) documentation indicates that the integer data type is 16-bit (2-byte) for numbers ranging in value from -32,768 to 32,767, you should almost always avoid using Integer and use Long instead. Long is more efficient. All variables in VB6.0 and earlier versions are stored on a full word boundary. So, if you defined two variables as follows:

Dim iNumber1 as Integer
Dim iNumber2 as Integer

The definition will require 8 bytes. Likewise, defining the two variables using Long will require the same amount of space:

Dim lNumber1 as Long
Dim lNumber2 as Long

So, Integer does not save you any space. Additionally, VB is forced to mask the second two bytes, which means that more overhead is required to do arithmetic with integers than with longs. The only case where Integer can take less space is when the Type construct is applied. For example: Type MyRecord Field1 as Integer Field2 as Integer End Type This will all change in VB.NET. In that new release Integer will be 4 bytes and Long will be 8 bytes. There will be a wizard that will convert your VB6.0 Longs to VB.NET Integers.

MetaPro Systems Inc. Visual Basic Tips & Tricks #35 - ASP for VB Developers

While there is still plenty of work for Visual Basic developers in desktop and client-server applications, the wave of the future is Web based applications. Accordingly, many VB developers would like to reinvent themselves as Web developers. I recommend learning ASP as the logical step towards achieving this goal.

ASP is like HTML on steroids. It utilizes VBScript, a subset of VB, to generate HTML depending on user responses and database information. The main distinction between VBScript and native VB is that VBScript does not allow typing of variables. For example you type "Dim iNumber" instead of "Dim iNumber as Integer". ASP has a few constructs of its own, but for the most part it is derived from VBScript and HTML. If you know VB and HTML you're well on your way to learning ASP.

If you have Studio 6.0, you can use Visual Interdev to develop your ASP pages. While ASP development is more cumbersome than developing in native VB, Microsoft is promising a big improvement with Studio.Net, which will include VB.NET and ASP.NET.

MetaPro Systems Inc. Visual Basic Tips & Tricks #36 - Hints on ASP for VB Developers

When you begin using ASP you should leverage your Visual Basic skills as much as possible. Here's one way to do this:

  1. Put as much of the functionality that is not screen related in custom DLLs.
  2. Test the custom DLLs in VB by creating an executable driver.
  3. Test them in ASP. An example of code to define a DLL within ASP is:
Set Session("gcloAPICalls") = Server.CreateObject("msiAPICalls.clsAPICalls")
Set mcloAPICalls = Session("gcloAPICalls")
msExecuteDir = mcloAPICalls.sGetEnvironmentVariable(msEnvironmentVariable)