eHow launches Android app: Get the best of eHow on the go.

How To

How to Use ADODC in Visual Basic

Contributor
By Robert Karr
eHow Contributing Writer
(0 Ratings)

The ADO (Active Data Objects) Data Control (ADODC) is a new method added in Visual Basic 6.0. It allows a programmer to put controls, such as labels, text boxes, list boxes and other display controls, on a form and connect them to the Data Control, which then connects to a database. This provides considerably more power to the developer in creating and maintaining databases. Setting up the ADODC can be complicated, but following these steps will make the process straightforward.

Difficulty: Moderately Challenging
Instructions

Things You'll Need:

  • Visual Basic 6.0
  1. Step 1

    Right click on the Visual Basic 6.0 program and choose "Run as Administrator." Start a new project by clicking on "File" and then "New Project." Select the "Data Project" template from the list.

  2. Step 2
    ADODC Control
    ADODC Control

    Double-click on the "frmDataEnv" showing in the "Data Project" list on the right. Place an ADO Data Control on the form by double-clicking on the ADODC control in the Toolbox on the left of the screen.

  3. Step 3
    Database Connection
    Database Connection

    Set up a connection to an existing database by right-clicking on the ADODC control now on the form and choosing "ADODC Properties." On the properties page, make sure the "Use Connection String" option is selected and click "Build." Follow the steps that appear, which will lead you through selecting the type of data to use, where to find the database on your computer and whether or not to use a password.

  4. Step 4
    Connection Properties and Test
    Connection Properties and Test

    Click on the "Test Connection" button on the "Connection" tab of the Data Link Properties when all information from step 3 is complete. If the connection fails, go back and recheck the settings chosen to connect to your database.

  5. Step 5
    Add/Delete Controls
    Add/Delete Controls

    Place two Command buttons on the fmDataEnv by double-clicking on the "Button" control in the Toolbox and then doing this again. Change the Name of one button to "cmdAddNewRecord" and the Caption to "Add New Record." Change the Name of the other button to "cmdDeleteRecord" and the Caption to "Delete Record."

  6. Step 6

    Double-click on the "cmdAddNewRecord" and insert this code between the beginning and end of the routine: "Adodc1.Recordset.AddNew." Do the same for the cmdDeleteRecord" button with this code: "Adodc1.Recordset.Delete." (Do not include the period at the end of this code.)

  7. Step 7

    Write the code to take care of what should happen when the user clicks one of the choices in step 6:
    Private Sub Form_Load()
    Adodc1.Recordset.ActiveConnection.BeginTrans
    End Sub
    Private Sub Form_Unload(Cancel as Integer)
    If MsgBox("Accept record changes?", vbYesNo) = vbYes Then
    Adodc1.RecordSet.ActiveConnection.CommitTrans
    Else
    Adodc1.Recordsete.ActiveConnection.RollbackTrans
    End If
    End Sub

  8. Step 8
    Sample Database Interface
    Sample Database Interface

    Finish the ADODC setup by adding Labels and TextBox controls to the form to create a mechanism for a user to add or delete records. The specific controls will depend on the database needs. See the image for an example of how this will look.

Tips & Warnings
  • The trickiest part about using ADODC is selecting the correct database type and using the proper connection method. This depends solely on the user's own setup. If the first choice does not work, it is just a matter of trying some other options in step 3.
  • All of the code instructions must be entered exactly as shown or errors will occur. However, the actual names and captions of controls are a matter of individual choice. For example, in step 5, if you want the Caption to read "Create a New Record" you can do this. You cannot change a line like "Adodc1.RecordSet.ActiveConnection.CommitTrans" in step 7.

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics