Setting the Procedure Attributes

Setting the Procedure Attributes

Visual Studio 6.0

All right, now that all of the properties have been added, we now need to do something special. Because our control will be data aware - remember it will be taking data from an ADO data control - we must set up the text boxes to be able to talk to the recordset so that its values can be displayed. We want to bind the text boxes to the data fields in the recordset. It's very simple to do this, but it is a critical step.

Try It Out - Setting the Procedure Attributes

1.   From the main VB menu, click on Tools-Procedure Attributes… The names of the three properties we added will be in the Name drop-down box. Click on name and then click the Advanced>> button to expose the rest of the dialog box. For the name, check the Property is data bound in the Data Binding frame. Then check Show in DataBindings collection at design time.

As you can see, VB 6.0 allows us to mark properties of our ActiveX control as bindable. This lets us to create data-aware controls. We can associate bindable properties with fields in any data source, making it easier to use our control in database applications.

2.  Click Apply to make the changes. Now do the same thing for the other two properties, the id and company. When you are finished, click on OK to save the changes and dismiss the box.

Now we are ready to make our control.

3.  From the main VB menu, click File-Make and name the control pubCtl. The default will be controlPrj.ocx reflecting the name of our project. So the name pubCtl is a bit more descriptive of what our control is doing. Now press OK. This step does a couple of things for us. It will create the .OCX file and also register it in the registry. Now Windows will know how to find our control when we need it:

When you press OK you will hear some disk activity. When it stops, the control is ready to use. As in the previous project, an .OCX file is created and can be included in any project. Like any ActiveX control, it does not do us much good until there is a host to use the control. So we want to add another project to the existing one. Adding a new project to the existing one will automatically create a project group for us. 

4.  From the main menu, select File-Add Project. Select a Standard EXE project. This will host our newly minted control. We will select the ctlHost project as our startup and it will host our control. This way we can easily go between projects until our control is working the way we want it to.

5.  Rename the new project and form as shown to ctlHost.vbp and frmRepeater.frm. Bring up the frmRepeater form. You will actually see your new control in the tool palette. Neat, eh?

6.  OK, we want to add the data repeater control to our project. This new project, ctlHost, will be responsible for hosting our new ActiveX control. Right-click on the tool palette and bring up the Components dialog box and select both the Microsoft DataRepeater Control 6.0 and the Microsoft ADO Data Control 6.0:

Press OK.

The data repeater control icon looks like this:

7.  OK, now both the data repeater control and the ADO data control have been added to your tool palette. Once the controls are on your palette, draw each of the controls on your form as shown in the next screenshot.

Since we have a project group, we cannot work on the control while the host is open. So if you need to go back and tweak the control, be sure to close the host form.

8.  The first thing we want to do is wire up our ADO data control to the Biblio.mdb database. We went over how to create the connection string in the last chapter under the heading Review of steps to set up the ADODC ConnectionString. So just repeat those steps and connect the ADO data control to the Biblio.mdb database and select the Publishers table.

9.  Next, set the Visible property of the ADO data control to False. We will use it to link to the database, but we will navigate the recordset using the data repeater control. So the ADO data control is important to our project, but it does not need to be seen by our users.

Now we need to tell the data repeater which data control it will be using. Right-click on the control and set the DataSource property to Adodc1.

Now we have linked the control to the database.  Notice how this is exactly the same as the standard data control set up.

10.  Next, we need to tell the data repeater which control it will be repeating. Of course, it will be the new control we just created. So click the drop-down button for the RepeatedControlName property and you will see all of the registered controls on your machine. Since our control (controlPrj.pubCtl) was automatically registered when we made the .OCX, it will be in the list. There will probably be a ton of controls that will be displayed. So you can see why it is important to provide our control with a descriptive name so we can easily find it.

By the way, ensure that the IntegralHeight property is set to True. This handy property tells the control only to show another record if another entire version of the control can be shown. This ensures that we won't be seeing only half of our control in the data repeater:

11.  The last thing we need to do is bind our user control properties to the ADO data control. Once our control is contained by the data repeater, we want to bind the control's properties to the record source which is, of course, Publishers. So this time we want to look at the custom property box for the data repeater. Right-click on the data repeater control and select Properties.

12.  Click on the RepeaterBindings tab. The PropertyName drop-down box will contain the three properties we added to our control. Remember when we told our control to display the properties in the bound properties collection? Well, here they are. For the PropertyName company, select Company Name in the DataField drop-down box. This will map our company property to the Company Name field in the database. Click Add to add the grouping.

13.  Now map the id property to the PubID data field and the name property to the Name data field. Be sure to add each one. When the three properties are mapped, press OK. That's it!

When each of the property names have been bound to the data field, your property page should look like the screen shot above.

14.  Since we have two projects in a single project group, we must tell VB which project to use for startup. Right-click the ctlHost.prj in the Project Explorer and click Set as Start Up. Now click on controlPrj and select Properties, select (None) from the drop-down box under Startup Object.

15.  Save your project group and press F5 to run your new data control. Use the scroll bar to navigate the recordset. While you are scrolling, consider that the data repeater is actually displaying two instances of your control simultaneously and each instance is holding a record from the Publishers table. Now that is amazing for 10 minutes work! OK, maybe 15 minutes tops.

This next screenshot shows the data repeater in action:

Go ahead and scroll through the recordset with the vertical scroll bar provided by the data repeater, and consider what you have just done. You created a brand new .OCX ActiveX control, learned about the project group feature, data bindings, not to mention how to use the data repeater control. That's not bad; and all using ActiveX Data Objects.

I'm sure that you can think of all sorts of uses for the data repeater control. We built a pretty straightforward control. You could easily build a control that displays images in a database, or tracks your personal finances. The uses of this new control are really only limited by your imagination.

© 1998 by Wrox Press. All rights reserved.

© 2015 Microsoft