简体   繁体   中英

How to use a Function (returning a string) as query criteria?

I got the following function returning a String: Between #2009/01/01# And #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

I want to use this string as an query criteria in a date field like so:

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

Error: Data type mismatch in criteria expression

After reading this thread: Expressing basic Access query criteria as regular expressions I am lead to believe that Regex is what I am after but I am not sure how to use it? Here is an attempt that only return a true value and not code that an sql Query can use:

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

Any ideas?

Creating two seprate functions get the job done:

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

And then calling them in sql separately and not as one string. However I am still curious as to why the string in the question could not be passed as a query criteria...

Your function returns not SQL but a string. That can't be compared with a date value, thus the error.

You can do this:

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

And then:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM