2014-02-17 89 views
1

我想創建一個Microsoft Access應用程序需要幫助。 我創建了一個數據庫,可以獲取多個日期。我需要計算日期和OMIT星期二,星期四,星期六和星期日在櫃檯之間的天數。這是在兩個不同的時間間隔(NotificationDate到OrderDate)和(PlacementDate到ReleaseDate)之間Microsoft Access和Visual Basic - 需要幫助運行模塊

我的問題是,現在我寫了它,我如何在Microsoft Access中實際使用它?我如何從表單運行它?

'//////This is for Valley Estimate of Demurrage Days///////////// 
Public Function Weekdays(ByRef NotificationDate As Date, ByRef OrderDate As Date, ByRef PlacementDate As Date, ByRef ReleaseDate As Date) As Integer 
Dim numWeekdays As Integer 
Dim totalDays As Integer 
Dim totaldays2 As Integer 
Dim WeekendDays As Integer 
Dim WeekendDays2 As Integer 
numWeekdays = 0 
WeekendDays = 0 
WeekendDays2 = 0 

totalDays = DateDiff(dateinterval.Day, NotificationDate, OrderDate) + 1 

'for i as integer = 1 to totalDays 

    If DatePart(dateinterval.Weekday, NotificationDate) = 1 Then 
     WeekendDays = WeekendDays + 1 
    End If 
    If DatePart(dateinterval.Weekday, startDateNotificationDate) = 3 Then 
     WeekendDays = WeekendDays + 1 
    End If 
    If DatePart(dateinterval.Weekday, NotificationDate) = 5 Then 
     WeekendDays = WeekendDays + 1 
    End If 
    If DatePart(dateinterval.Weekday, NotificationDate) = 7 Then 
     WeekendDays = WeekendDays + 1 
    End If 
     NotificationDate = DateAdd("d", 1, NotificationDate) 
     '/////////////////////////////////////////////////////////////////////////// 

totaldays2 = DateDiff(dateinterval.Day, PlacementDate, ReleaseDate) + 1 

    If DatePart(dateinterval.Weekday, PlacementDate) = 1 Then 
     WeekendDays2 = WeekendDays2 + 1 
    End If 
    If DatePart(dateinterval.Weekday, PlacementDate) = 3 Then 
     WeekendDays2 = WeekendDays2 + 1 
    End If 
    If DatePart(dateinterval.Weekday, PlacementDate) = 5 Then 
     WeekendDays2 = WeekendDays2 + 1 
    End If 
    If DatePart(dateinterval.Weekday, PlacementDate) = 7 Then 
     WeekendDays2 = WeekendDays2 + 1 
    End If 
     PlacementDate = DateAdd("d", 1, PlacementDate) 

        numWeekdays = WeekendDays + WeekendDays2             

End Function 

回答

0

您可以通過在進入它顯示一個表達式的結果ControlSourceTextBox。用「=」加上。

=Weekdays(CDate([txtNotificationDate]), CDate([txtOrderDate]), 
CDate([txtPlacementDate]), CDate([txtReleaseDate])) 

(一行)

請注意,您可以通過只在表達其名稱輸入訪問文本框(和當前表單的其他控件)。 Access將自動添加括號[]。

如果訪問不應自動更新結果文本框,您可以強制它重新計算與

Me!txtResult.Requery 

但在大多數情況下,訪問將自動更新。

+0

好的,非常感謝,這更符合我所尋找的內容。我現在將測試它 – NavyNuke

0

我建議將函數名更改爲ModWeekdays。這樣你就知道它是在模塊中定義的。另外,我認爲功能Weekdays已經定義。

那麼你已經擁有的功能設置爲公共,因此,所有你需要做的就是像下面這樣:

Dim MyReturnVal As Integer 
MyReturnVal = ModWeekdays(txtNotificationDate, txtOrderDate, txtPlacementDate, txtReleaseDate) 

而且,你不需要通過所有的變量的因爲你沒有改變它們,所以引用函數。所以不是我會用:

Public Function ModWeekdays(ByRef NotificationDate As Date, OrderDate As Date, ByRef PlacementDate As Date, ReleaseDate As Date) As Integer 

編輯,這裏是我在你修改的MS Access文件中使用的代碼:

Private Sub cmdSubmit_Click() 
    On Error GoTo Err_cmdSubmit 
    Dim MyReturnVal As Integer 

    If (IsNull(txtNotificationDate)) Then GoTo Err_DataMissing 
    If (IsNull(txtOrderDate)) Then GoTo Err_DataMissing 
    If (IsNull(txtPlacementDate)) Then GoTo Err_DataMissing 
    If (IsNull(txtReleaseDate)) Then GoTo Err_DataMissing 

    If (IsDate(txtNotificationDate) = False) Then GoTo Err_DataType 
    If (IsDate(txtOrderDate) = False) Then GoTo Err_DataType 
    If (IsDate(txtPlacementDate) = False) Then GoTo Err_DataType 
    If (IsDate(txtReleaseDate) = False) Then GoTo Err_DataType 

    MyReturnVal = ModWeekdays(txtNotificationDate, txtOrderDate, txtPlacementDate, txtReleaseDate) 
    MsgBox ("The value returned from the ModWeekdays function is: " & MyReturnVal) 

Exit_cmdSubmit: 
    Exit Sub 
Err_cmdSubmit: 
    MsgBox Err.Description 
    Resume Exit_cmdSubmit 
Err_DataMissing: 
    MsgBox ("You must supply a value for the Notification Date, Order Date, Placement Date, and Release Date.") 
    GoTo Exit_cmdSubmit 
Err_DataType: 
    MsgBox ("You must enter Dates only. Please correct and try again.") 
    GoTo Exit_cmdSubmit 
End Sub 
+0

「txtNotificationDate」中「txt」的用途是什麼? – NavyNuke

+0

這只是在表單上引用一個控件。 'txtNotificationDate'將是應該包含通知日期的控件的名稱。您可以使用任何變量名稱或硬編碼日期,如「05/05/14」。 – Linger

+0

我很感謝你的幫助,但也許我的誤解更根本。輸入你的建議後,(MyReturnVal)如何在Access窗體中查看?另外,我有用戶在標準訪問視圖中輸入的變量NotificationDate,OrderDate等,那麼如何將它鏈接到我的VBA代碼中的變量呢?這是一個鏈接到我的訪問計劃:https://drive.google.com/file/d/0B2aq4WToAF2YN2lkNEsyeWZna0k/edit?usp=sharing – NavyNuke