Library tutorials & articles

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

Comments

  1. 26 Sep 2005 at 09:11
    Well I think the code should look like:
    Private Sub Form_Load()
    dcPekerja.Visible = False
    dcPekerja.ConnectionString = App.Path & "\db1.mdb"
    dcPekerja.RecordSource = "Select * From Table1;"
    End Sub

    All you have to add is a tiny little "\" and there you have it. I suppose that you already figured it out by now but if not hope it helps. Also make sure that the database is in the same directory as the ".exe" or ".vbp"

    Have fun coding!
  2. 23 Sep 2005 at 20:00
    Hi there... I went though your code and fond one mistake...

    Private Sub Form_Load()
    dcPekerja.Visible = False
    dcPekerja.ConnectionString = App.Path + "db1.mdb"
    dcPekerja.RecordSource = "Select * from Table1;"
    End Sub

    You Should Have

    Private Sub Form_Load()
    dcPekerja.Visible = False
    dcPekerja.ConnectionString = "\\db1.mdb"
    dcPekerja.RecordSource = "Select * From Table1;"
    End Sub
  3. 14 Sep 2004 at 01:23

    Or not

  4. 14 Sep 2004 at 01:21

    Well i supose you do have the database in the same directory with the application on the other computer

  5. 23 Apr 2004 at 23:34

    i'm using adodc to connect to access
    but i'm having problem when i use the application on other pc...
    it say that it cant found the database file...


    i use this coding... but still the error is there...


    Private Sub Form_Load()
    dcPekerja.Visible = False
    dcPekerja.ConnectionString = App.Path + "db1.mdb"
    dcPekerja.RecordSource = "Select * from Table1;"
    End Sub


    i also try this code...but nothing happens...
    Private Sub Form_Load()
    dcPekerja.Visible = False
    dcPekerja.ConnectionString = App.Path & "db1.mdb"
    dcPekerja.RecordSource = "Select * from Table1;"
    End Sub


    dcPekerja => adodc

  6. 27 Feb 2003 at 16:20

    Gave me insight, thanks

  7. 03 Aug 2002 at 15:38

    IMHO - The Data Control's sole usefullnes is when you literally want to scroll the data, one record at a time.javascript:smilie('')
    frown


    I find it MUCH easier to roll my own ADO RS control that will search for the resocrd(s) I need...javascript:smilie('')
    stick out tongue javascript:smilie('')
    wink


    This control should ONLY be used by total DB Newbies!!! (IMHO)javascript:smilie('')
    eek!

  8. 01 Jan 1999 at 00:00

    This thread is for discussions of ADO Data Control.

Leave a comment

Sign in or Join us (it's free).

Susan Harkins
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!