gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:27:47 25 4
gpt4 key购买 nike

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

这是我的表格:

enter image description here

当您单击“保存”按钮时,将运行底部代码。

我在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。你得到了你所期望的吗?

关于ms-access - MS-Access : I can't figure out why the error message is not showing up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36095015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com