Click here to Skip to main content
15,867,568 members
Articles / Web Development / HTML
Article

How to write an updatable ActiveX control for web page in VB6

Rate me:
Please Sign up or sign in to vote.
4.25/5 (18 votes)
10 Sep 20043 min read 171.3K   3.2K   39   24
This article will explain how to write an embeddable ActiveX control to use in a web page, that can be updated and reinstalled from the web page.

Introduction

Many times, you may need to use a powerful COM-based control in your web application that might need to be modified at a later date. In some cases, it is sufficient to simply reference the .ocx file (<OBJECT CODEBASE="myActiveXcontrol.ocx" />). However, this method will not work if the code for the control needs to be modified and reinstalled on the user's machine.

Instead, a better method will utilize Visual Studio's Package and Deployment Wizard to create a CAB file that serves as a direct installation (or reinstallation) of the .ocx which can be referenced in the <object> tag.

Step 1: Create the Control in Visual Basic

As an example, we will first create a simple control in VB 6.0.

Open MS Visual Basic, and choose "ActiveX" control:

Main Screen

I then placed two components on my control- a Command button, called cmdTest, and a text box, called txtTest:

dev Screen

(This a very simple example, just to show how a web client script interacts with an embedded ActiveX control).

Next, we will write some code to test our functionality. Our goal is to pass a value to the text box from our web page, then use the command button to display that text in a message box. We will need the code for the _click() and a Public function to populate the textbox (we need it public so that it can be accessed outside of the scope of the VB control, i.e., the web page code). I use the function setText() to accomplish this:

VB
Public Function setText(txt As String)
    txtTest.Text = txt
End Function
VB
Private Sub cmdTest_Click()
    MsgBox txtTest.Text
End Sub

Step 2: Package the Project

Next, we will need to save, make, and package our project. My project is called vbActiveX, and my control is called ctlVBAX. Click on Tools-->Package and Deployment Wizard. Click Package at the main screen. (It may ask you to re-compile and/or save your project. Just click Yes.)

Follow the screen shots as so:

Sample screenshot

Image 4

Sample screenshot

Sample screenshot

Image 7

Image 8

Image 9

Your package should exist now in /Package within your project folder.

Step 3: Write the web page code

Our last step involves adding the client side script on our web page, as well as some HTML to interact with the embedded ActiveX control.

I simply created a dropdown called "drpValues", and gave the options "value1"-"vaue5". Our goal is to populate the textbox on the AX control (txtTest) with the value selected from the dropdown. We will need to add a JavaScript function to accomplish this:

HTML
<select id="drpValues" onclick="send_value(this)">
 <option value=""></option>
 <option value="value1">value1</option>
 <option value="value2">value2</option>
 <option value="value3">value3</option>
 <option value="value4">value4</option>
 <option value="value5">value5</option>
</select>
JavaScript
<--this goes into the <head></head> section-->
<script>
function send_value(obj){
 document.all('ctlVBAX').setText(obj.options[obj.selectedIndex].value);
}
</script>

Notice the <object> tag, and what it holds:

HTML
<OBJECT ID="ctlVBAX"
CLASSID="CLSID:748FEF73-28D1-4889-A582-E5F8F526CDD1"
CODEBASE="vbActiveX.CAB#version=1,0,0,0">
</OBJECT>

As you can see, the object ID is ctlVBAX, and this is what we refer to when using in our script. Also notice that the CODEBASE portion includes the .CAB file, not the .ocx, so the system looks to the version of the CAB to initialize the ActiveX control. If it is a newer version, it will update the control, almost like a re-installation.

Test the page by opening up vbActiveX.HTM inside the /Package folder.

To test the updating, change a few things in the ActiveX control code, and re-make the project (File->Make). Then, copy the new .ocx from your main project folder into the /support directory, and use the batch file there to re-make the CAB file in /Package. Finally, copy that CAB file back into your project folder, and re-open the web page. You should notice that the changes have taken.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Matt Sciotto is an IT professional, who specializes in VB and .Net software/web application development. He has written several embeddable ActiveX COM controls, as well as custom server controls in VB.Net, and many scalable webform applications. In addition, he has a handy background in server scripting including knowlege of PHP,Perl,CGI and ASP, and Database Management with intense SQL foundation in MS SQL Server, MS Access, Oracle and MySql environments. He has worked on several OS platforms including all recent versions of Windows, Red Hat Linux, FreeBSD, UNIX and Solaris.

Comments and Discussions

 
QuestionI can't get it to work right! [modified] Pin
MulleDK1914-Oct-07 5:48
MulleDK1914-Oct-07 5:48 
QuestionRe: I can't get it to work right! Pin
Wilson tEN9-Sep-08 19:35
Wilson tEN9-Sep-08 19:35 
AnswerRe: I can't get it to work right! Pin
adolfogh7-Oct-08 22:04
adolfogh7-Oct-08 22:04 
Questionactivex control activation Pin
todevesh3-Oct-07 22:37
todevesh3-Oct-07 22:37 
QuestionYour Method Doesn't worked Pin
goldenrose925-Sep-07 10:53
goldenrose925-Sep-07 10:53 
QuestionMore than one OCX in a web page Pin
chewkokhooi13-Jul-07 18:19
chewkokhooi13-Jul-07 18:19 
GeneralQuestion for local access. Pin
eric feng2-Mar-07 8:12
eric feng2-Mar-07 8:12 
GeneralI m not getting the ActiveX control Contents Pin
salon22-Feb-07 0:22
salon22-Feb-07 0:22 
GeneralMy changes aren't being displayed Pin
alexsd4-Jan-07 11:12
alexsd4-Jan-07 11:12 
GeneralThank you Pin
Mr.No31-Jul-06 4:22
Mr.No31-Jul-06 4:22 
Generalonly works with administrative permissions [modified] Pin
masterminder19-Jun-06 22:20
masterminder19-Jun-06 22:20 
GeneralService pack 6 for visual basic 6.0 cabinates install message Pin
plmanikandan7-Jun-06 4:11
plmanikandan7-Jun-06 4:11 
GeneralI found it finally, but there is a big problem. Pls help... Pin
tduccuong12-Dec-05 14:59
tduccuong12-Dec-05 14:59 
GeneralRe: I found it finally, but there is a big problem. Pls help... Pin
qcha0s6-Jan-06 17:49
qcha0s6-Jan-06 17:49 
QuestionHow can I create such a control with VB.NET Pin
Member 145144717-Oct-05 19:20
Member 145144717-Oct-05 19:20 
GeneralThis is there and everyone is using it Pin
Anonymous13-Oct-05 19:18
Anonymous13-Oct-05 19:18 
GeneralOcx Dependency Pin
Vasudevan Deepak Kumar1-Aug-05 20:41
Vasudevan Deepak Kumar1-Aug-05 20:41 
GeneralGreat Info Thanks Pin
Pinhead_Me27-Oct-04 13:31
Pinhead_Me27-Oct-04 13:31 
GeneralRe: Great Info Thanks Pin
Anonymous23-Aug-05 0:21
Anonymous23-Aug-05 0:21 
GeneralThanks Pin
Pops4718-Oct-04 17:22
Pops4718-Oct-04 17:22 
GeneralSecurity Settings Pin
sandeepk199920-Sep-04 5:38
sandeepk199920-Sep-04 5:38 
GeneralRe: Security Settings Pin
mittalsumit29-Aug-06 2:23
mittalsumit29-Aug-06 2:23 
GeneralVB6 runtime Pin
Lars [Large] Werner11-Sep-04 22:19
professionalLars [Large] Werner11-Sep-04 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.