Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 处理传递给Access VBA函数的错误值_Ms Access_Vba - Fatal编程技术网

Ms access 处理传递给Access VBA函数的错误值

Ms access 处理传递给Access VBA函数的错误值,ms-access,vba,Ms Access,Vba,我有一个VBA函数,基本上是这样的: Public Function JoinDateTime(DateTime As String, Time As String) As Date Dim dtDate As Date dtDate = CDate(Format(DateTime, "dd/mm/yyyy")) dtDate = dtDate & " " & Format(Time, "hh:mm") JoinDateTime = dtDate

我有一个VBA函数,基本上是这样的:

Public Function JoinDateTime(DateTime As String, Time As String) As Date
    Dim dtDate As Date
    dtDate = CDate(Format(DateTime, "dd/mm/yyyy"))
    dtDate = dtDate & " " & Format(Time, "hh:mm")
    JoinDateTime = dtDate
End Function
它将日期和时间粘在一起形成日期时间值。(真正的函数背后有更多的逻辑。)

问题是,我想为传递给它的恼人值添加处理。这主要用于空/空值-如果DateTime为空,则返回空。如果是一个文本字符串返回#Error,那么它不只是默默地失败似乎是个好主意

问题是,我不知道怎么做。我曾考虑过提前返回,可能是在函数开始时推这个:

If DateTime = Null or DateTime = "" Then 
    JoinDateTime = Null
End If
<>但它似乎并不认为这是一个返回,仍然执行函数的其余部分。

有办法做到这一点吗?理想情况下,更好的方法是什么

If Nz(DateTime, "") = "" Then 
    JoinDateTime = Null
End If

两点:最好返回一个“特殊”日期(比如纪元的开始),而不是返回null;另外,“
DateTime
”不是变量的好名字。

要从VBA中的函数过早返回,需要使用
Exit function
语句,类似于
Exit Sub
Exit for
,等等。因此

If DateTime = Null or DateTime = "" Then 
    JoinDateTime = Null
    Exit Function 'Au revoir
End If

将阻止以下代码的其余部分执行。

最干净、最防错误的方法是引发错误

Public Function JoinDateTime(DateTime As String, Time As String) As Date
    If DateTime = "" Then 
        Err.Raise _
            Number:=12345, _
            Source:="JoinDateTime", _
            Description:="Invalid input. DateTime cannot be empty string."
    End If
    'NB: could also check for IsNull(DateTime) if DateTime were type Variant.
    ' etc.
这将显示错误并显示以下消息:“运行时错误“12345”:输入无效。DateTime不能为空字符串。”执行将停止

如果不希望停止执行,可以在调用
JoinDateTime
的过程中处理错误。例如:

Sub tester()

    On Error GoTo ErrorHandler ' Telling VBA where to go when an error is raised

    Dim d As String
    Dim j As Date        
    d = InputBox("Date please:")
    j = JoinDateTime(d) ' Raises an error upon invalid input
    MsgBox Prompt:=j

ErrorHandler:
    Select Case Err.Number
    Case 12345 ' "Invalid input" error was raised, therefore do the following...
        ' Whatever you want to happen when this error occurs, e.g.
        ' MsgBox Prompt:=Err.Description
        ' or 
        ' j = 0
        ' Resume Next
    End Select
    Resume ExitProcedure

ExitProcedure:
    'Cleanup code goes here
End Sub

首先,您需要将函数声明更改为使用
Variant
而不是
String
。这是因为在VBA中,
字符串
数据类型不能保存值
Null
。如果希望函数返回无效日期的
Null
,则还需要将返回类型更改为
Variant
。我还将采纳的建议,将您的参数重命名为与内置函数不冲突的参数(例如
dateSection
timeSection

其次,比较
DateTime=Null
将永远不会计算为true。任何与
Null
值的比较都将产生
Null
值。相反,您应该使用(请参阅错误5)

第三,您可以考虑使用。此函数将确保参数为有效日期,包括检查空值。这取决于您是否希望格式不良的日期也以静默方式失败

总而言之:

Public Function JoinDateTime(dateSection As Variant, timeSection As Variant) As Variant
    Dim dtDate As Date
    If IsNull(dateSection) Then
        JoinDateTime = Null
    Else
        dtDate = CDate(Format(dateSection, "dd/mm/yyyy"))
        dtDate = dtDate & " " & Format(timeSection, "hh:mm")
        JoinDateTime = dtDate
    End If
End Function

还有,
Time
是一个名字。@David-W-Fenton:不,我不知道。可能是因为我从来没用过。在正确的上下文中,某些日期被合理地称为“无效日期”,例如公历的开始。早在20世纪70年代,所有程序员都这么说,使用9/9/99作为表示空的神奇日期是可以的,因为代码在1999年9月9日之前永远不会被使用。他们错了。第二,这些神奇日期的文档在哪里?另一个程序员怎么知道这是什么意思?@David-W-Fenton:你打算回答这个问题吗?@David-W-Fenton:嗯,大卫。比如说,申请将处理1980年以后的日期,并保证不会早于1980年(比如说,它将处理1980年生效的立法)。用纪元的开始来表示一个无效的日期不会有什么危险。这绝对是处理这个问题的“正确”方法。返回null充其量是不明确的,最坏的情况下可能导致调试噩梦。为什么不直接键入参数作为日期,然后测试每个参数是否具有适当的部分?也就是说,日期参数必须是可解释的整数值,时间部分应小于1。我只是觉得你做这种事是自找麻烦。我甚至不明白你为什么需要它。您不是在日期字段中存储数据吗?如果是这样的话,即使日期和时间在不同的字段中,将结果连接到一个有效的时间也不会有问题——根本不需要复杂的函数@David Re:原因:我们正在将客户发送给我们的数据转换为一种一致的格式,以运行基准测试报告。我们的数据库使用日期-时间格式,但是客户端倾向于以“日期”和“时间”的形式发送数据。该函数将它们粘在一起,但也可以处理这样的事情:当它们只向我们发送小时数时(即,对于1PM,它们通过整数“13”发送)-这是我提到的“附加逻辑”的一部分。因为这个字段是可选的,所以我不希望在他们不提供数据的情况下它崩溃。回复:验证:我会调查的。