Question : Querying Query Results in VB.NET

Hello Experts,

I have two queres saved in MS ACCESS:

1).  Query4:
 SELECT DISTINCT First(StatsBatchModule.ExternalBatchID) AS FirstOfExternalBatchID, StatsBatch.BatchName, StatsBatchModule.EndDateTime, StatsBatchModule.ModuleLaunchID
FROM StatsModuleLaunch INNER JOIN (StatsBatch INNER JOIN (StatsBatchModule INNER JOIN StatsFormType ON StatsBatchModule.BatchModuleID=StatsFormType.BatchModuleID) ON StatsBatch.ExternalBatchID=StatsBatchModule.ExternalBatchID) ON StatsModuleLaunch.ModuleLaunchID=StatsBatchModule.ModuleLaunchID
GROUP BY StatsBatch.BatchName, StatsBatchModule.EndDateTime, StatsBatchModule.ModuleLaunchID, StatsModuleLaunch.ModuleName, StatsBatchModule.BatchStatus, StatsFormType.FormTypeName, StatsBatchModule.Deleted
HAVING (((StatsBatchModule.EndDateTime)>=#7/1/2005# And (StatsBatchModule.EndDateTime)<#8/1/2005#) And ((StatsModuleLaunch.ModuleName)="Recognition Server") And ((StatsBatchModule.Deleted)=False))
ORDER BY StatsBatchModule.EndDateTime;

2)  Query5:
 SELECT Sum(StatsBatchModule.PagesScanned) AS SumOfPagesScanned, Sum(StatsBatchModule.PagesDeleted) AS SumofPagesDeleted, Sum([PagesScanned]-[PagesDeleted]) AS [Total Pages Scanned for May]
FROM Query4 LEFT JOIN StatsBatchModule ON Query4.FirstofExternalBatchID=StatsBatchModule.ExternalBatchID;

Note: Query5 is selecting from results of Query4.

Can somebody provide with code snippets in VB.NET to call such queries.  It works from Access if I just click on Query 5(under the query tab for the database).

It would be great if I don't have to save these queries in ACCESS and directly call it from code.  I would not prefer to create temporary tables, but If thats the only option, can you provide code snippets for that too in any case.  I want to avoid JOINS to because I would be working on Query5(adding other constraints later on).

Thanks!

Answer : Querying Query Results in VB.NET

If you want to get data from an Access Query, you have to treat that Query as a Stored Procedure.  Here's outline code

        Dim ConnectionString As String = "YourConnectionString"

        'Name of the query object in Microsoft Access - enclosed in square brackets
        Dim CommandText As String = "[Query4]"

        Dim conn As New OleDbConnection(ConnectionString)
        Dim cmd As New OleDbCommand(CommandText, conn)

        'Set the command type property to stored procedure
        cmd.CommandType = CommandType.StoredProcedure

        Dim ds As New DataSet
        Dim da As New OleDbDataAdapter
        da.SelectCommand = cmd
        da.Fill(ds)

If you use this with CommandText of "[Query5]" it will, internally to Access, make the necessary reference to Query4.

Roger
Random Solutions  
 
programming4us programming4us