Solved

MS access and calling saved Queries

Posted on 2016-10-28
4
49 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 35

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

813 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

19 Experts available now in Live!

Get 1:1 Help Now