Solved

MS access and calling saved Queries

Posted on 2016-10-28
4
26 Views
Last Modified: 2016-11-01
From VB.NET code, I'm calling a query saved in an Access database.   Is there a way to call multiple saved queries, each of which return a set of records, and get back each set of records in  a DataTable within a in DataSet?  Here's my current code:

   Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.JET.oledb.4.0;data source=C:\MyAccess\service.mdb")

        Dim cmd As New OleDbCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "GetWorkOrder"
        cmd.Parameters.Add("@LocalID", OleDbType.Integer).Value = 35

        Try
            cmd.Connection = con
            con.Open()

            Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)

            Dim dt As New DataTable

            Dim ds As DataSet = New DataSet()
            da.Fill(ds, "WorkOrder")

            dt = ds.Tables("WorkOrder")

            con.Close()
            con.Dispose()

        Catch ex As Exception
        End Try
0
Comment
Question by:HLRosenberger
  • 2
4 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 41864543
Could try something like that:

Dim cmd1 As New OleDbCommand
Dim cmd2 As New OleDbCommand
Dim cmd3 As New OleDbCommand
Dim dt1 As New DataTable
Dim dt2 As New DataTable
Dim dt3 As New DataTable
            

con.ConnectionString = "Provider=Microsoft.JET.oledb.4.0;data source=C:\MyAccess\service.mdb"
con.Open()


    cmd1.Connection = con
    cmd1.CommandType = CommandType.StoredProcedure
    cmd1.CommandText = "GetWorkOrder"
    Dim da1 As OleDbDataAdapter = New OleDbDataAdapter(cmd1)
    cmd1.Parameters.Add("@LocalID", OleDbType.Integer).Value = 35
  Dim ds1 As DataSet = New DataSet()
    da1.Fill(ds1, "WorkOrder")
dt1 = ds1.tables("WorkOrder")


   cmd2.Connection = con
    cmd2.CommandType = CommandType.StoredProcedure
    cmd2.CommandText = "SecondQueryName"
    Dim da2 As OleDbDataAdapter = New OleDbDataAdapter(cmd2)
    cmd2.Parameters.Add .....
  Dim ds2 As DataSet = New DataSet()
    da2.Fill(ds2, "Table2")
dt2 = ds2.tables("Table2")


     cmd3.Connection = con
    cmd3.CommandType = CommandType.StoredProcedure
    cmd2.CommandText = "ThirdQueryName"
    Dim da3 As OleDbDataAdapter = New OleDbDataAdapter(cmd3)
    cmd3.Parameters.Add .....
  Dim ds3 As DataSet = New DataSet()
    da3.Fill(ds3, "Table3")
dt3 = ds3.tables("Table3")

Open in new window

0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 41864591
But that hits the database three time, correct?  it requires three open connections, correct?  I was looking to do this to improve speed.
0
 
LVL 34

Accepted Solution

by:
PatHartman earned 500 total points
ID: 41864610
Access queries can only return a single recordset at one time.  If you need three separate recordsets, you must run three queries.  If the queries return the same columns in the same order, you could create a union query to get back a single recordset.
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 41868357
Thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now