在要求日期的输入框中单击"取消"时出现运行时错误



这样做的最终结果是一个时间表。我最初把它放在excel中,但我想把工资单信息/模块转移到access中。当我在excel中时,取消按钮工作正常,但在访问中总是出错。网上的一些研究表明,这是一个我认为可以用类似的东西解决的问题

If StartDate = "" Then
GoTo ext:
End If

这导致

运行时错误13。

我相信发生这种情况是因为当点击cancel时,StartDate变量为空,并且输入框需要某种类型的输入。我会一直在线查看,但也许问题是我不应该使用输入框,但如果是这样的话,我不确定应该使用什么?完整的脚本如下。感谢您的帮助。

Private Sub CreateTimeSheet_Click()
Dim StartDate As Date
mbox = InputBox("Enter Start Date YYYY/MM/DD", "Enter Start Date")
StartDate = mbox
If StartDate = "" Then
GoTo ext:
End If
FirstDay = Format(CDate(StartDate))
Text76 = Format(CDate(StartDate + 1))
Text77 = Format(CDate(StartDate + 2))
Text78 = Format(CDate(StartDate + 3))
Text79 = Format(CDate(StartDate + 4))
Text80 = Format(CDate(StartDate + 5))
Text81 = Format(CDate(StartDate + 6))
Text82 = Format(CDate(StartDate + 7))
Text83 = Format(CDate(StartDate + 8))
Text84 = Format(CDate(StartDate + 9))
Text85 = Format(CDate(StartDate + 10))
Text86 = Format(CDate(StartDate + 11))
Text87 = Format(CDate(StartDate + 12))
Text88 = Format(CDate(StartDate + 13))
Text61 = Format(CDate(mbox), "dddd")
Text63 = Format(CDate(StartDate + 1), "dddd")
Text64 = Format(CDate(StartDate + 2), "dddd")
Text65 = Format(CDate(StartDate + 3), "dddd")
Text66 = Format(CDate(StartDate + 4), "dddd")
Text67 = Format(CDate(StartDate + 5), "dddd")
Text68 = Format(CDate(StartDate + 6), "dddd")
Text69 = Text61.value
Text70 = Text63.value
Text71 = Text64.value
Text72 = Text65.value
Text73 = Text66.value
Text74 = Text67.value
Text75 = Text68.value
ext:
Exit Sub
End Sub

由于InputBox返回String,因此您希望将结果存储到String中,而不是Date变量中。然后,您可以使用IsDate()检查结果文本是否可以转换为日期。然后继续,否则退出。请记住,用户可以在InputBox中输入任何内容-不能依赖于他们输入实际日期值。

Dim mbox As String
Dim StartDate As Date
mbox = InputBox("Enter Start Date YYYY/MM/DD", "Enter Start Date")
If IsDate(mbox) Then
StartDate = CDate(mbox)
Else
If mbox = "" Then
' user entered nothing, so cancel
Exit Sub
Else
' user entered garbage, so tell them what they did wrong
MsgBox "That is not a date."
Exit Sub
End If
End If
' valid data entered... rest of your code here

此外,如果要将天数添加到日期值,则需要使用DateAdd,而不是简单的添加。

最新更新