2015-07-28 29 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中作爲一個字符串。 但是,我仍然好奇爲什麼問題中的字符串不能作爲查詢條件傳遞...

相關問題