Access VBA Function不会在查询中返回任何值

时间:2017-03-29 08:44:37

标签: sql vba ms-access access-vba

我有以下Access VBA模块(根据需要通过Debug.Print测试它返回信息),然后我在SQL代码中使用以下方式。

Public Function DayInterval(DueDate As String) As String
Dim Today As Date
Dim ConvertData As Date

Today = Date
ConvertDate = DateSerial(Year(DueDate), Month(DueDate), Day(DueDate))

Select Case DateDiff("d", ConvertDate, Today)
    Case 1 To 10
      DueDate = "0-10"
    Case 11 To 30
      DueDate = "11-30"
    Case 31 To 60
      DueDate = "31-60"
    Case 61 To 90
      DueDate = "61-90"
   Case 91 To 180
      DueDate = "91-180"
   Case 181 To 365
      DueDate = "181-365"
   Case 366 To 540
      DueDate = "366-540"
   Case 541 To 730
      DueDate = "541-730"
   Case Else
      DueDate = ">730"
End Select

DayInterval = DueDate 

End Function

SELECT 
DueDate AS [Date], 
DayInterval([DUE_DATE]) AS [Interval]
FROM Invoices;

问题在于,当我运行上述查询时,我没有得到任何结果。

enter image description here

为何会发生这种情况?

谢谢。

1 个答案:

答案 0 :(得分:2)

您必须使用真实的日期值:

ConvertDate = DateValue(DueDate)

或者只是(见Minty的评论):

class db {

static $serverName = "SERVERNAME"; 
static $connectionInfo = array( "Database"=>"DBName", "UID"=>"local_user", "PWD"=>"password_local");
static $conn;

function connectToDB(){

    global $serverName,$connectionInfo,$conn;

    $conn = sqlsrv_connect(self::$serverName , self::$connectionInfo);

    if( $conn ) {
         //echo "Connection established.<br />";
    }else{
         echo "Connection to Database could not be established, please contact your admin!<br />";
         //die( print_r( sqlsrv_errors(), true));
    }