MS-Access:我不明白为什么没有显示错误消息

MS-Access: I can't figure out why the error message is not showing up

我正在建立一个用于癌症研究的数据库。我创建了一个名为 "inputPI_form" 的表单。 PI 代表 "Principal Investigator" 又名研究员。 tblPI 只是一个包含名字和姓氏的 table。

这是我的表格:

当您点击 "save" 按钮时,您 运行 底部代码。

我使用这两个名称在 tblPI 中创建了一个组合键以防止任何重复记录。此代码防止重复记录,但没有显示 MsgBox:

'Add new PI's name and verify uniqueness with composite key'
Private Sub newPI_Button_Click()

   'Declare duplication error number'
   Const ERR_DUPLICATE_VALUE = 3022

   On Error GoTo Err_Handler

   'Declare database object and string variables'
   Dim dbs As Database
   Dim firstName As String
   Dim lastName As String

   'Capture firstName and lastName from inputPI_form as strings'
   firstName = Forms("inputPI_form")!firstName.Value
   lastName = Forms("inputPI_form")!lastName.Value

   'Set the dbs object'
   Set dbs = CurrentDb

  'Excute SQL code to create new record in tblPI by passing firstName and lastName values'
dbs.Execute "INSERT INTO tblPI (lastName, FirstName) VALUES " & _
    "('" & lastName & "','" & firstName & "');"

  'Update the PI selection combobox on inputProtocolForm'
  Forms("inputProtocolForm")!selectionPI.Requery

  dbs.Close
  DoCmd.Close acForm, "inputPI_form"

Err_Handler:
  If Err.Number = ERR_DUPLICATE_VALUE Then
     MsgBox ("This PI's name is already taken. Please select another one.")
  End If
End Sub

我不熟悉在VBA中捕获错误。我犯了一个明显的错误吗?我将非常感谢社区的反馈。谢谢!

尝试简化您的错误处理程序。取出 If 语句并替换它是一个简单的 Msgbox Err.number & Err.description。你得到你期望的了吗?