I got it.. I got it....
Here is how you create a query from a VB code in an Access File...
Add references to the following before executing this code....
1. Microsoft ActiveX Data Objects 2.8 Library
2. Microsoft ADO Ext. 2.8 for DDL and Security..
You can do so by going to the Project Menu and then select References. In the new dialog select the above mentioned options... and then copy the following code
Code:Option Explicit 'Declare a Connection Object Dim cnDB As ADODB.Connection 'Declare a Catalog object Dim cQuery As ADOX.Catalog Sub Main() Set cnDB = New ADODB.Connection 'Open a Connection to the database (Change the C:\DB1.MDB to your own MDB File With cnDB .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\DB1.MDB" .Open End With Set cQuery = New ADOX.Catalog 'Sets the active connection of the catalog object to the connection that is opened cQuery.ActiveConnection = cnDB 'Create a command object Dim cmdNewQuery As ADODB.Command Set cmdNewQuery = New ADODB.Command 'Write the query which you want to be created in Access Database cmdNewQuery.CommandText = "Select * from TABLE1" 'This will add the above created query to the access file.... cQuery.Views.Append "QueryFromVB", cmdNewQuery 'Close the connection cnDB.Close Set cnDB = Nothing Set cQuery = Nothing Set cmdNewQuery = Nothing End Sub
This works with Access 2002..
Hope it helps you...
--Shuja




Reply With Quote