使用自定义错误消息访问输入表单 [英] Access Input Form with Custom Error Messages

查看:75
本文介绍了使用自定义错误消息访问输入表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

解决方案是不尝试捕获错误,而是作为添加新记录"命令按钮的一部分来处理错误:

The solution was to not try and capture the errors but do the error handling myself as part of the Add New Record command button:

Private Sub buttonNewRecord_Click()

Dim ErrorInt As Integer
Dim TeleCheck As Variant

Name.SetFocus
If Name.Text = "" Then
MsgBox "Name is missing!"
ErrorInt = ErrorInt + 1
End If

TeleCheck = DLookup("[Telephone]", "tblColdCallLog", "[Telephone] = '" & Me.Telephone & "'")
If Not IsNull(TeleCheck) Then
MsgBox "Telephone number already exists in the table!"
ErrorInt = ErrorInt + 1
End If

If ErrorInt < 1 Then
DoCmd.GoToRecord , , acNewRec
MsgBox "Record Added!"
End If

End Sub

原始帖子:

我所拥有的:

我创建了一个简单的Access 2013表单,用于将数据输入到表中.在表单上,​​用户将数据输入到字段中,然后单击使用命令按钮向导"添加新记录所创建的按钮.

I have created a simple Access 2013 Form used to input data into a table. On the Form, the user enters data into the fields and clicks on a button made using the Command Button Wizard to Add New Record.

该表单具有一个必填字段[名称],并将一个字段设置为[索引:是(无重复),[电话号码].在[表单]中,如果[名称]栏位为空,或[电话]栏位中的表格中也有重复的号码,则会正确产生错误讯息.

The form has one required field, [Name], and one field set to 'Index: Yes (No Duplicates)', [Telephone Number]. In the Form, this correctly produces error messages if the [Name] field is empty or there is a duplicate number detected in the [Telephone] field that is also in the table.

我要做什么:

出现的错误消息不是用户友好的.我想用自定义错误消息替换它们,如果没有错误,也许一条消息说一切顺利.

The error messages that appear are not user friendly. I would like to replace them with custom error messages and if there are no errors, maybe a message that says all went well.

我尝试过的事情:

在表单属性"的事件"选项卡上的事件出错"中的"[事件过程]"中:

On the Form properties, Events tab, in 'On Error', [Event Procedure]:

Private Sub Error_Sub(DataErr As Integer, Response As Integer)
  If DataErr = 3022 Then
    MsgBox "Duplicate telephone number found in table!"
    Response = acDataErrContinue
  End If
  If DataErr = 3314 Then
    MsgBox "Name is missing!"
    Response = acDataErrContinue
  End If
End Sub

这有效,但仅当您关闭表单时...单击添加新记录"命令按钮时,它仅在适当时显示默认错误消息.

This works but only when you close the Form... When you click the 'Add New Record' Command Button, it simply shows the default error messages when appropriate.

也许我应该使用更新前"事件?我似乎无法使用相同的VBA脚本.我不允许定义DataErr或Response.因此,我将改为使用表达式:

Maybe I should use the Event 'Before Update'? I can't seem to use the same VBA script. I'm not allowed to define DataErr or Response. So, I'll use an Expression instead:

=IIf(Error(3022),MsgBox("Duplicate telephone number found in table"))
=IIf(Error(3314),MsgBox("Name is missing"))

这行得通...但是没有错误时.即使[名称]字段中有一个名称,也会显示错误,但至少会替换默认错误消息.

This works... but when there is no error. Even if there is a name in the [Name] field, the error shows but at least it replaces the default error message.

我们将其放在按钮本身中吗?我将不得不使用Macro Builder对其进行编辑.复制并粘贴此代码有点困难,因此我将简化操作:

Let's put it in the button itself? I'll have to use the Macro Builder to edit it. It's a bit hard to copy and paste this one so I'll simplify:

OnError GoTo Error_Handling
GoToRecord New
If [MacroError]<>0 Then 
   MsgBox = "[MacroError].[Description]"
End If

Error_Handling:
If Error(3022) Then
  MsgBox = "Duplicate telephone number found in table!"
End If
If Error(3314) Then
  MsgBox = "Name is missing!"
End If

这与更新前"事件相同;替换默认错误消息,但无论是否首先触发该错误消息.

This does the same as the 'Before Update' event; replaces the default error message but regardless of whether or not the error message should be triggered in the first place.

我做错了什么?我觉得这很简单.我尝试了多种其他组合和无尽的Google搜寻,但是我感到很沮丧.

What am I doing wrong? I get the feeling it's something really simple. I've tried a variety of other combinations and endless Googling but I feel stumped.

推荐答案

Private Sub buttonNewRecord_Click()

Dim ErrorInt As Integer
Dim TeleCheck As Variant

Name.SetFocus
If Name.Text = "" Then
MsgBox "Name is missing!"
ErrorInt = ErrorInt + 1
End If

TeleCheck = DLookup("[Telephone]", "tblColdCallLog", "[Telephone] = '" & Me.Telephone & "'")
If Not IsNull(TeleCheck) Then
MsgBox "Telephone number already exists in the table!"
ErrorInt = ErrorInt + 1
End If

If ErrorInt < 1 Then
DoCmd.GoToRecord , , acNewRec
MsgBox "Record Added!"
End If

End Sub

这篇关于使用自定义错误消息访问输入表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆