Microsoft
Software
Hardware
Network
Question : Querying Query Results in VB.NET
Hello Experts,
I have two queres saved in MS ACCESS:
1). Query4:
SELECT DISTINCT First(StatsBatchModule.Ext
ernalBatch
ID) AS FirstOfExternalBatchID, StatsBatch.BatchName, StatsBatchModule.EndDateTi
me, StatsBatchModule.ModuleLau
nchID
FROM StatsModuleLaunch INNER JOIN (StatsBatch INNER JOIN (StatsBatchModule INNER JOIN StatsFormType ON StatsBatchModule.BatchModu
leID=Stats
FormType.B
atchModule
ID) ON StatsBatch.ExternalBatchID
=StatsBatc
hModule.Ex
ternalBatc
hID) ON StatsModuleLaunch.ModuleLa
unchID=Sta
tsBatchMod
ule.Module
LaunchID
GROUP BY StatsBatch.BatchName, StatsBatchModule.EndDateTi
me, StatsBatchModule.ModuleLau
nchID, StatsModuleLaunch.ModuleNa
me, StatsBatchModule.BatchStat
us, StatsFormType.FormTypeName
, StatsBatchModule.Deleted
HAVING (((StatsBatchModule.EndDat
eTime)>=#7
/1/2005# And (StatsBatchModule.EndDateT
ime)<#8/1/
2005#) And ((StatsModuleLaunch.Module
Name)="Rec
ognition Server") And ((StatsBatchModule.Deleted
)=False))
ORDER BY StatsBatchModule.EndDateTi
me;
2) Query5:
SELECT Sum(StatsBatchModule.Pages
Scanned) AS SumOfPagesScanned, Sum(StatsBatchModule.Pages
Deleted) AS SumofPagesDeleted, Sum([PagesScanned]-[PagesD
eleted]) AS [Total Pages Scanned for May]
FROM Query4 LEFT JOIN StatsBatchModule ON Query4.FirstofExternalBatc
hID=StatsB
atchModule
.ExternalB
atchID;
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(Connection
String)
Dim cmd As New OleDbCommand(CommandText, conn)
'Set the command type property to stored procedure
cmd.CommandType = CommandType.StoredProcedur
e
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
Regional settings GREEK , decimal separator "," sql server 2000, But does not update a textbox tha binds to a float field,C# framework 2.0
MS Word conversion to Adobe
New RAM Causing laptop to shut down
How to build a control by its name as a string
vb.net selected item from listview
MS Project 2007, How to add a Column 'Percent'
Click an Hyperlink in Internet Explorer Using an Excel Macro
Date conversion
Programming Help - vb.net/C# app to click in another application window using Win API or sendmessage
FTP to unix servers from windows using VB 6.0 or Excel VBA