如何使用Function(返回字符串)作为查询条件?

时间:2015-07-28 09:52:58

标签: sql regex vba function ms-access

我得到以下函数返回一个字符串:在#2009/01/01#和#2015/07/28#

之间
 Public Function PayrollDateGet() As String
    'get dates
    'test for null
    If IsNull(PStartD) = True Or IsNull(PEndD) = True Then
        MsgBox "Please set the payroll parameters first by calling the PayrollAgentSet() function"
        Exit Function
    Else
        PayrollDateGet = CStr("Between #" & PStartD & "# And #" & PEndD & "#")
    End If
End Function

我想在日期字段中使用此字符串作为查询条件,如下所示:

SELECT TestTblD.ID, TestTblD.Ddate, TestTblD.TestValue
FROM TestTblD
WHERE (((TestTblD.Ddate)=PayrollDateGet()));

错误:条件表达式中的数据类型不匹配

阅读完这篇帖子后:Expressing basic Access query criteria as regular expressions我认为正则表达式是我所追求的,但我不确定如何使用它?这是一个只返回true值的尝试,而不是sql Query可以使用的代码:

Public Function PayrollDateGet() As String
    'get dates
    'test for null
    If IsNull(PStartD) = True Or IsNull(PEndD) = True Then
        MsgBox "Please set the payroll paramaters first by calling the PayrollAgentSet() funciton"
        Exit Function
    Else
        'create the sting
        Dim pattern As String
        pattern = CStr("Between #" & PStartD & "# And #" & PEndD & "#")

        ' Initialise the Regex object '
        Static regex As Object
        If regex Is Nothing Then
            Set regex = CreateObject("vbscript.regexp")
            With regex
                .Global = True
                .IgnoreCase = True
                .MultiLine = True
            End With
        End If

        ' Update the regex pattern if it has changed since last time we were called '
        If regex.pattern <> pattern Then regex.pattern = pattern
            ' Test the value against the pattern '
            PayrollDateGet = regex.Test(pattern)
        End If
End Function

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

创建两个seprate函数可以完成工作:

Public Function PayrollStartDate() As String
    'get start date
    'test for null
    If IsNull(PStartD) = True Then
        MsgBox "Please set the payroll paramaters first by calling the PayrollAgentSet() funciton"
        Exit Function
    Else
        PayrollStartDate = PStartD
    End If
End Function

Public Function PayrollEndDate() As String
    'get end date
    'test for null
    If IsNull(PEndD) = True Then
        MsgBox "Please set the payroll paramaters first by calling the PayrollAgentSet() funciton"
        Exit Function
    Else
        PayrollEndDate = PEndD
    End If
End Function

然后分别在sql中调用它们而不是一个字符串。 但是我仍然很好奇为什么问题中的字符串不能作为查询条件传递...

答案 1 :(得分:0)

您的函数不返回SQL而是字符串。这不能与日期值进行比较,因此也就是错误。

你可以这样做:

Public Function PayrollDateCheck(ByVal PDate As Variant) As Variant

    Dim Check As Variant

    Check = PDate  ' Null for empty field.

    'get dates
    'test for null

    If IsNull(PStartD) Or IsNull(PEndD) Then
        MsgBox "Please set the payroll parameters first by calling the PayrollAgentSet() function"
        Exit Function
    Else
        If Not IsNull(PDate) Then
            Check = (PDate >= CDate(PStartD) And PDate <= CDate(PEndD))
        End If
    End If

    PayrollDateCheck = Check

End Function

然后:

SELECT TestTblD.ID, TestTblD.Ddate, TestTblD.TestValue
FROM TestTblD
WHERE PayrollDateCheck([Ddate]) = True;