的MS Access:查询重用/供应使用VBA查询参数参数、MS、Access、VBA

2023-09-08 11:09:45 作者:你是我血液里的毒、

我有两个所显示相同的形式,并通过该供应形式是利用多个参数对非常复杂的查询。我想重复使用查询在VBA功能和/或在另一种形式。因此,我有两个问题:

有没有办法写SQL语句,使得查询动态确定的形式,从而读取它的参数不必指定表单的名字吗?例如,沿着我行的东西!的startDate,而不是形式!myForm的!的startDate。

有没有办法为DAO记录?

打开时,它提供的参数查询 解决方案

在大多数情况下,李连杰(访问)不受同一注射的问题,其他数据库的经验,所以它可能适合使用VBA编写SQL和无论是更新与新的SQL查询,或设置窗体的记录源。

大致来说:

  sSQL =SELECT F1,F2从TBL其中f3 ='&放大器; Me.txt3和放大器; '

CurrentDB.QueryDefs(aquery)。SQL = sSQL
 

另外,你可以使用参数:

查询:

 参数txtCompany文本(150);

选择< ...>
WHERE公司= txtCompany
 

code:

 设置QDF = db.QueryDefs(QueryName)

qdf.Parameters!txtCompany =修剪(FRM!txtCompany)
 
access怎样用宏调用VBA

I have two very complex queries that are displayed on the same form and that use multiple parameters supplied by that Form. I would like to reuse the query in a VBA function and/or on another form. As such, I have two questions:

Is there a way to write the SQL statement such that a query dynamically determines the form to read its parameter from rather than having to specify the name of the form? E.g., Something along the line of Me!startDate, rather than Forms!myForm!startDate.

Is there a way to supply parameters to a query when opening it as a DAO RecordSet?

解决方案

For the most part, Jet (Access) is not subject to the same injection problems that other databases experience, so it may suit to write the SQL using VBA and either update a query with the new sql, or set the form's record source.

Very roughly:

sSQL = "SELECT f1, f2 FROM tbl WHERE f3 = '" & Me.txt3 & "'"

CurrentDB.QueryDefs("aquery").SQL = sSQL

Alternatively, you can use parameters:

Query:

PARAMETERS txtCompany Text(150);

SELECT <...>
WHERE Company = txtCompany

Code:

Set qdf = db.QueryDefs("QueryName")

qdf.Parameters!txtCompany = Trim(frm!txtCompany)