从访问另一个查询参数传递给查询 [英] Pass parameter to a query from another query in Access

查看:208
本文介绍了从访问另一个查询参数传递给查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个参数化查询GET_CUSTOMER:

I have a parameterized query GET_CUSTOMER:

SELECT * FROM Customer WHERE id = [customer_id]

我想调用另一个查询该查询,并传递一个参数:

I want to call this query from another query and pass it a parameter:

SELECT * FROM GET_CUSTOMER(123)

请注意上面的code是无效的,它是在这里给你什么,我试图做一个想法。是否有可能做到这一点在MS Access?

Note the above code is not valid, it is here to give you an idea of what I'm trying to do. Is it possible to do this in MS Access?

更新1:

我张贴的查询是例子。实际查询是复杂得多。我知道我可以使用表连接,但在我的特定情况下,它会更容易,如果我可以运行里面的其他查询(这些参数也一样)参数化查询。因为我使用的访问与我的.NET应用程序无法使用访问形式。

The queries I posted are for example. The actual queries are much more complex. I know I can use table joins, but in my specific case it would be much easier if I could run parameterized queries inside other queries (that are parameterized as well). I can't use access forms because I'm using access with my .NET application.

推荐答案

这是我结束了帮助解决这个 HTTP: //stackoverflow.com/a/24677391/303463 。原来,访问共享的所有查询中的参数,所以没有必要专门传递从一个查询参数到另一个

This is how I end up solving this with help of http://stackoverflow.com/a/24677391/303463 . It turned out that Access shares parameters among all queries so there is no need to specifically pass parameters from one query to another.

查询1:

SELECT * FROM Customer WHERE ID > [param1] AND ID < [param2]

Query2:

Query2:

SELECT * FROM Query1

VB.NET code:

VB.NET code:

    Dim ConnString As String = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=Database.mdb"
    Dim SqlString As String = "Query2"
    Using Conn As New OleDbConnection(ConnString)
        Using Cmd As New OleDbCommand(SqlString, Conn)
            Cmd.CommandType = CommandType.StoredProcedure
            Cmd.Parameters.AddWithValue("param1", "1")
            Cmd.Parameters.AddWithValue("param2", "3")
            Conn.Open()
            Using reader As OleDbDataReader = Cmd.ExecuteReader()
                While reader.Read()
                    Console.WriteLine(reader("ID"))
                End While
            End Using
        End Using
    End Using

这篇关于从访问另一个查询参数传递给查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆