Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Use ADO and ADOX to Modify the Base Query of an Access QueryDef Object in Visual Basic

View products that this article applies to.


This article demonstrates how to use ActiveX Data Objects (ADO) and ADO Extensions for DDL and Security (ADOX) to modify the underlying SQL query of an Access 97 or Access 2000 QueryDef object from a Visual Basic application at run time.

↑ Back to the top

More information

The following example uses the Northwind Access database that ships with Visual Basic. For illustration purposes, a new Query object named "All Customers" is created in this database at design time using Access. The underlying query for the QueryDef object is a simple SQL SELECT statement that retrieves all the records in the Customers table. Using ADO and ADOX, the Visual Basic code modifies the SQL query of the "All Customers" QueryDef object.

Step-by-Step Example

  1. Use Microsoft Access to open the Northwind.mdb database, which is located in the Visual Basic installation directory.
  2. Create a new Query object in the database that retrieves all the records in the Customers table. Specify the SQL query for the new Query object as "Select * from Customers". Save the query object as "All Customers". Save your changes to the database, and exit Access.
  3. Create a new Standard EXE Project in Visual Basic.
  4. On the Project menu, click References, and select the Microsoft ActiveX Data Objects 2.1 library and Microsoft ADO Ext. 2.1 for DDL and Security check boxes.
  5. Add a command button to Form1.
  6. Add the following code to the form's General Declarations section:
       Dim cn As Connection
       Dim mcat As ADOX.Catalog
       Dim mview As ADOX.View
  7. Add the following code to the command button's Click() event procedure:
    Private Sub Command1_Click()
        Dim cmd As ADODB.Command
        Set cn = New ADODB.Connection
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=nwind.mdb"
        Set mcat = New ADOX.Catalog
        Set mcat.ActiveConnection = cn
        Set mview = mcat.Views("All Customers")
        Set cmd = mview.Command
        cmd.CommandText = "Select * from customers Order by CompanyName"
        Set mview.Command = cmd
        MsgBox "Querydef [All Customers] has been modified !"
        Set mview = Nothing
        Set cmd = Nothing
        Set mcat = Nothing
    End Sub
    The Query objects that are defined in an Access database are listed in the Views collection of the ADOX Catalog object. Each ADOX.View object has a Command property that contains a reference to an ADODB.Command object, which defines the underlying query of the QueryDef object. The preceding code modifies the CommandText property of this Command object to modify the query definition of the Access Query object.
  8. Press the F5 key to run the project.
  9. Click the command button that appears on the form. The code in the Click event runs and modifies the query definition of the "All Customers" QueryDef object.

↑ Back to the top

Keywords: KB255782, kbhowto

↑ Back to the top

Article Info
Article ID : 255782
Revision : 6
Created on : 2/12/2007
Published on : 2/12/2007
Exists online : False
Views : 419