2014-02-17 40 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