Microsoft Access和Visual Basic - 需要帮助运行模块

时间:2014-02-17 14:35:40

标签: vba ms-access

我正在尝试创建一个微软访问应用程序的帮助。 我创建了一个获取许多日期的数据库。我需要计算日期之间的天数,以及OMIT星期二,星期四,星期六和星期日在柜台。这是在两个不同的时间间隔(NotificationDate到OrderDate)和(PlacementDate到ReleaseDate)之间

我的问题是,现在我写了它,我如何在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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以在ControlSource的{​​{1}}中输入表达式,以显示表达式的结果。前面加上“=”。

TextBox

(在一行上)

请注意,您只需在表达式中输入其名称即可访问TextBox(以及当前Form的其他控件)。 Access将自动添加括号[]。

如果Access不能自动更新结果TextBox,您可以使用

强制重新计算它
=Weekdays(CDate([txtNotificationDate]), CDate([txtOrderDate]),
CDate([txtPlacementDate]), CDate([txtReleaseDate]))

但在大多数情况下,Access会自动更新它。