2015-07-28 67 views
0

我得到了下面的函数返回一个字符串:之间#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?这里是一个尝试,只返回一个真正的值,而不是一个SQL查询可以使用的代码:

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 

任何想法?

回答

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; 
+0

仍然没有工作,但接近,如果你走过代码它贯穿一切,返回三个真实的,但是当它打开该查询没有记录显示。如果我手动在[Ddate]字段中输入true,则显示3条记录。 – Rhdr

+0

如果您将这三个值手动输入到函数中,它会返回什么? – Gustav

+1

不要在意我的sql中有错误我错过了=真正的部分...它的作品谢谢你 – Rhdr

0

创建两个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中作为一个字符串。 但是,我仍然好奇为什么问题中的字符串不能作为查询条件传递...

相关问题