Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS access and calling saved Queries

Posted on 2016-10-28
4
Medium Priority
?
145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 39

Accepted Solution

by:
PatHartman earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

719 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