希望有人可以阐明我在使用 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 中是否有某种方法可以将这些参数的值初始化为表单中的值?
当用户单击“确定”时,表单将打开查询和报告。