ADO Data Control

Creating a bound form

Now, let’s create the form shown in Figure A. First, launch a new standard .exe project, insert a form, and change that form’s name to frmDataControl. Then, save the project as DataControl.

Adding the ADO data control:

The control you’ll be working with in this article isn’t the data control you’re accustomed to. The control on the Toolbox is for backward compatibility when running applications created in older versions of VB. The data control we’ll be working with--the new ADO version-is available via the Components option. To add the ADO data control to the Toolbox, press Ctrl+T to open the Components dialog box. Select MS ADO Data Control 6.0 (OLEDB), and click Apply, and then click Close. Once you’ve added the ADO data control to the toolbox, you can add one to the bottom of your form (fromDataControl). Name the new control adoData.

Connecting adoData to a Jet database:

At this point, you must establish where the data control will find its records. You have more than one option. You can connect the data control to a SQL Server or a Jet database. For our example, we’re going to connect to a Jet database; the Nwind.mdb database (MS Access) that comes with VB. To get started, we’ll need to change a few of our ADO data control’s default properties. Refer to Table A for those settings.

Table A

CommandType

1 - adCmdText

EOFAction

2 - adDoAddNew

RecordSource

SELECT * FROM Customers ORDER BY CompanyName

Now you’re ready to set the ConnectionString property. First, select the ConnectionString property field and then click the resulting Build button. Choose the Use Connection String option (if it isn’t already selected). Next, click that option’s Build button to display the Data Link Properties dialog box. Then, choose Microsoft Jet 3.51 OLE DB Provider in the Provider tab. At this point, select the Connection tab and identify the database you’re connecting to in the Select or enter a database name control. We connected to

C:Program FilesMicrosoft Visual StudioVB98Nwind.mdb

Of course, your setup will probably be different, so be sure to specify the correct path to your database file. To test the connection, click the Test Connection button at the bottom of the dialog box. If the connection is sound, click OK twice to return to your form. If the connection fails, you’ll need to adjust the entry until it works.

Adding the remaining controls:

Add the appropriate label and text box controls until your form resembles the one shown in Figure A. Set each text box control’s DataSource property to adoData (or the name of your ADO data control) to bind the text box to the data control. Now you need to bind each control to a field. To do so, select a field from the text box control’s DataField property. It’s also a good idea to name each control. Table B lists the names we chose for our controls.

Table B

txtCompanyName

CompanyName

txtAddress

Address

txtCity

City

txtRegion

Region

txtZip

PostalCode

txtCountry

Country

Quick Tip: You can save time when creating several controls of the same type by creating one control and setting all the shared properties. Then, use the Copy and Paste commands to create the total number of controls you’ll need. When VB asks if you’re creating a control array, answer "No". Once you have all the controls you need, simply select them individually and enter the unique properties--such as the Name property.

Now, let’s finish our form by adding the last control--a command button. Name that button cmdExit. If you like, add the caption property E&xit. At this point, double-click the command button to open the form’s module and add the procedure shown in below. Close the window to return to your form.

Code A

Private Sub cmdExitCommand_Click()
    Unload frmDataControl
End Sub

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski