访问vba使用自定义消息

时间:2017-06-15 15:21:45

标签: access-vba runtime-error

我一直在尝试通过Access设置outlook任务项,并用自定义消息替换运行时错误440。 当某个字段(me.dueBy)为空时,会弹出此错误。 目前我所拥有的代码成功地为具有dueBy数据的记录创建了一个任务,但是当我点击没有截止日期的记录按钮时,没有任何反应。没有消息框,没有错误,没有。

我只需要一个消息框告诉我,当该字段为空时,我需要一个截止日期来设置任务。

Private Sub Command15_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim DataErr As Integer
Dim Response As Integer

On Error GoTo err_handler

Set db = CurrentDb
Set rs = db.OpenRecordset("tblActions")

Set outLookApp = CreateObject("outlook.application")
Set OutlookTask = outLookApp.CreateItem(olTaskItem)


With OutlookTask
.Subject = "Action Due Date: " & Me.dueBy & " for Contract ID " & Me.contractID
.Body = "Due date for Action: < " & Me.actionNote & " > is " & Me.dueBy & "."
.ReminderSet = True
.ReminderTime = Me.dueBy + TimeValue("8:00:00 AM")
.Save
End With
MsgBox "Action Task has been set in Outlook successfully."


exit_err_handler: Exit Sub
err_handler:
If DataErr = 440 Then
Response = acDataErrContinue
MsgBox "Due date is required.", vbOKOnly, "Due date Error"
End If
Resume exit_err_handler

Set rs = Nothing
Set db = Nothing

End Sub

1 个答案:

答案 0 :(得分:1)

DataErrResponse是Form_Error-Event的参数。它们通常对错误处理没有影响。在您的过程中,DataErr只是一个整数变量,除非您明确指定另一个值,否则它将始终具有值0.

您需要在错误处理程序中检查Err.Number以识别特定的错误情况。