【问题标题】:Prompted for input twice in query在查询中提示输入两次
【发布时间】:2013-09-04 20:41:59
【问题描述】:

希望有人能对我在使用 vb 和 Access 时遇到的问题有所了解。一般问题是这样的:

  • 有一个表单可以为查询提供用户输入
  • 查询使用表单中的用户输入运行
  • 生成的报告依赖于 vb 模块从查询中生成百分位数。

基本上,表单为查询提供输入,而查询又为报表中的模块提供输入。

我遇到的问题是用户在表单上输入一次,然后被提示(带有弹出窗口)再次输入数据。只有第二次复飞的数据会反映在报告中。有没有办法保留表单数据并且不再提示查询?

以下是我的模块代码(在报告中)。如您所见,我尝试从表单中传递参数,但它似乎不起作用:

Public Function PercentileRst(RstName As String, fldName As String, PercentileValue As
Double) As Double
'This function will calculate the percentile of a recordset.

Dim PercentileTemp As Double
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim RstOrig As DAO.Recordset
Dim RstSorted As DAO.Recordset


Dim xVal As Double
Dim iRec As Long
Dim i As Long

Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qryMaster")
'i need to pass the parameters to the query
qdf.Parameters!minA = Forms!demo_form.min_assets
qdf.Parameters!maxA = Forms!demo_form.max_assets
qdf.Parameters!minP = Forms!demo_form.min_parts
qdf.Parameters!maxP = Forms!demo_form.max_parts


Set RstOrig = qdf.OpenRecordset()

RstOrig.Sort = fldName
Set RstSorted = RstOrig.OpenRecordset()
RstSorted.MoveLast
RstSorted.MoveFirst
xVal = ((RstSorted.RecordCount - 1) * PercentileValue) + 1
'x now contains the record number we are looking for.
'Note x may not be     whole number
iRec = Int(xVal)
xVal = xVal - iRec
'i now contains first record to look at and
'x contains diff to next record
RstSorted.Move iRec - 1
PercentileTemp = RstSorted(fldName)
If xVal > 0 Then
RstSorted.MoveNext
PercentileTemp = ((RstSorted(fldName) - PercentileTemp) * xVal) + PercentileTemp
End If
RstSorted.Close
RstOrig.Close
Set RstSorted = Nothing
Set RstOrig = Nothing
Set dbs = Nothing
PercentileRst = PercentileTemp
End Function

我使用的表单将 4 个参数传递给查询。在查询中,我使用以下标准:'Between [minA] And [maxA]' and 'Between [minP] And [maxP]'

查询的SQL中有没有办法将这些参数的值初始化为表单中的值?

当用户单击“确定”时,表单会打开查询和报告。

【问题讨论】:

  • qryMaster 用作报表的记录源。根据用户输入(来自 demo_form)qryMaster 运行,并且(理论上)百分位函数在 qryMaster 上运行并生成摘要报告的输出。

标签: vb.net ms-access vba ms-access-2010


【解决方案1】:

qryMaster 包含参数,是报表的记录源。因此,当报表首次打开时,Access 会要求用户提供这些参数的值。

稍后,您在将其结果集加载到DAO.Recordset 的过程中再次使用qryMaster。那时您的代码会为参数提供值,因此不会要求用户再次提供值。但是,这对表单打开时的情况没有任何影响。

尝试修改后的 qryMaster,您可以在其中指向表单的控件:

WHERE
        some_field BETWEEN Forms!demo_form!min_assets
            AND Forms!demo_form!max_assets
    AND another_field BETWEEN Forms!demo_form!min_parts
            AND Forms!demo_form!max_parts

db引擎可以查看demo_form上的那些控件来获取它需要的值,所以不会要求用户单独提供参数值。

【讨论】:

  • 我会试一试的。一旦我将查询更改为直接引用表单,我应该如何更改报告中的模块?基本上,当我在我的 vb 代码中引用查询时,我如何(如果有的话)传递参数? (再次感谢)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
相关资源
最近更新 更多