DAO.QueryDef在MS Access 2010中显示不正确的结果

时间:2014-10-06 09:38:27

标签: vba ms-access ms-access-2010 dao

我在MS Access中有以下存储的查询

SELECT 
    tblRegistration.ID, 
    tblRegistration.TypeCode, 
    CDate([tblRegistration].[RegStart]) AS Reg1, 
    CDate([tblRegistration].[RegStop]) AS Reg2
FROM 
    tblRegistration
WHERE 
    (
        (tblRegistration.TypeCode = "T" Or tblRegistration.TypeCode = "S" Or tblRegistration.TypeCode = "F") 
        AND 
        (CDate([tblRegistration].[RegStart]) Between CDate([Forms]![frmRegBilling]![RegStart]) And CDate([Forms]![frmRegBilling]![RegStop]))
    ) 
    OR 
    (
        (tblRegistration.TypeCode = "T" Or tblRegistration.TypeCode = "S" Or tblRegistration.TypeCode = "F") 
        AND 
        (CDate([tblRegistration].[RegStop]) Between CDate([Forms]![frmRegBilling]![RegStart]) And CDate([Forms]![frmRegBilling]![RegStop]))
    );

查询,当独立执行查询时(当frmRegBilling加载了查询使用的参数时)显示正确的结果(8条记录)。

但是当在VBA中执行查询时,只显示2条记录。在这种情况下,只验证第一个参数。

Dim qdf As DAO.QueryDef, rst As Recordset, varData As Variant
Dim intFields As Integer, intRecords As Integer, j As Integer, k As Integer
Dim rec As String, fld_type As Integer

Set qdf = CurrentDb.QueryDefs("Query1")
qdf.Parameters(0).Value = Eval(qdf.Parameters(0).Name)
qdf.Parameters(1).Value = Eval(qdf.Parameters(1).Name)
Set rst = qdf.OpenRecordset()

j = rst.RecordCount - 1
k = rst.Fields.Count - 1

切换线参数(0)或(1)没有帮助(如果因任何原因有影响)。为什么在VBA中执行时不在查询中验证这两个参数?

1 个答案:

答案 0 :(得分:0)

如果您使用的是VBA,为什么不将其创建为运行时查询?

Dim strSQL As String, rst As Recordset, varData As Variant
Dim intFields As Integer, intRecords As Integer, j As Integer, k As Integer
Dim rec As String, fld_type As Integer

strSQL = "SELECT tblRegistration.ID, tblRegistration.TypeCode, CDate([tblRegistration].[RegStart]) AS Reg1, " & _
         "CDate([tblRegistration].[RegStop]) AS Reg2 FROM tblRegistration WHERE " & _
         "((tblRegistration.TypeCode = 'T' Or tblRegistration.TypeCode = 'S' Or tblRegistration.TypeCode = 'F') AND " & _
         "(CDate([tblRegistration].[RegStart]) Between " & Format([Forms]![frmRegBilling]![RegStart], "\#mm\/dd\/yyyy\#") & _
         " And " & Format([Forms]![frmRegBilling]![RegStop], "\#mm\/dd\/yyyy\#") & ")) OR " & _
         "((tblRegistration.TypeCode = 'T' Or tblRegistration.TypeCode = 'S' Or tblRegistration.TypeCode = 'F') " & _
         "AND (CDate([tblRegistration].[RegStop]) Between " & Format([Forms]![frmRegBilling]![RegStart], "\#mm\/dd\/yyyy\#") & _
         " And " & Format([Forms]![frmRegBilling]![RegStop], "\#mm\/dd\/yyyy\#") & "));"

Set rst = CurrentDb.OpenRecordset(strSQL)