Solved

MS access and calling saved Queries

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

920 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