在ACCESS中运行代码vba时消除#Error

时间:2018-11-27 16:47:01

标签: ms-access access-vba ms-access-2016

我需要帮助,因为当我运行代码时,某些日期字段为空时会出现错误。我有一个包含信息的表,并且从生成器开始运行此代码。

在ACCESS中运行代码vba时消除#Error

感谢您的帮助。

Option Compare Database

Public Function WorkingDays2(FECHA_DE_VALIDACION_FA As Date, FECHA_IMPRESIÓN As Date) As Integer
'....................................................................
' Name:     WorkingDays2
' Inputs:   StartDate As Date
'   EndDate As Date
' Returns: Integer
' Author: Arvin Meyer
' Date:     May 5,2002
' Comment: Accepts two dates and returns the number of weekdays between them
' Note that this function has been modified to account for holidays. It requires a table
' named tblHolidays with a field named HolidayDate.
'....................................................................

Dim intCount As Integer
Dim rst As DAO.Recordset
Dim DB As DAO.Database

Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT [DIAFESTIVO] FROM DIASFESTIVOS", dbOpenSnapshot)

'StartDate = StartDate + 1
'To count StartDate as the 1st day comment out the line above

intCount = 0

Do While FECHA_DE_VALIDACION_FA <= FECHA_IMPRESIÓN

rst.FindFirst "[DIAFESTIVO] = #" & FECHA_DE_VALIDACION_FA & "#"
If Weekday(FECHA_DE_VALIDACION_FA) <> vbSunday And Weekday(FECHA_DE_VALIDACION_FA) <> vbSaturday Then
If rst.NoMatch Then intCount = intCount + 1
End If

FECHA_DE_VALIDACION_FA = FECHA_DE_VALIDACION_FA + 1



Loop

WorkingDays2 = intCount

Exit_WorkingDays2:
Exit Function

WorkingDays2 = intCount

Exit_WorkingDays2:
Exit Function

Err_WorkingDays2:
Select Case Err

Case Else
MsgBox Err.Description
Resume Exit_WorkingDays2
End Select


End Function

2 个答案:

答案 0 :(得分:0)

这取决于您在数据库中从何处调用此函数。可能作为查询中的计算字段?像这样: WorkingDays: WorkingDays3([YourDateField])

尝试以下方法: WorkingDays: WorkingDays3(Nz([YourDateField],Date())

答案 1 :(得分:0)

您最初的问题包括一个名为WorkingDays3的函数的代码,该函数带有一个日期参数。

您的插图显示了一个名为WorkingDays2的函数,该函数带有两个日期参数。

我认为您需要提供有关正在使用的数据以及在什么情况下会看到#Error的更详细的信息。