检查表 Access 2010 VBA 中是否已存在记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21232110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if record already exists in table Access 2010 VBA
提问by Chipsaird
I have created a table in Access 2010 that holds a list of employee names, using an ID as the primary key. I have another table which uses these names as a foreign key, via the ID, in a drop down box, which allows users to select an employee's name, and then record training to that name using a form.
我在 Access 2010 中创建了一个包含员工姓名列表的表,使用 ID 作为主键。我有另一个表,它使用这些名称作为外键,通过 ID,在下拉框中,允许用户选择员工的姓名,然后使用表单记录对该姓名的培训。
What I would like to ask is, how would I write the VBA code that would check if an employee's name had already been added to the training table, thus checking if an employee had already completed that training, and then return a message box alerting the user; "This employee has already completed their training, are you sure you want to proceed?"
我想问的是,我将如何编写 VBA 代码来检查员工的姓名是否已添加到培训表中,从而检查员工是否已完成该培训,然后返回一个消息框提醒用户; “这名员工已经完成了培训,你确定要继续吗?”
I am relatively new to VBA, however I assume I need some sort of Count method on the employee ID. Thanks in advance!
我对 VBA 比较陌生,但是我认为我需要对员工 ID 使用某种 Count 方法。提前致谢!
回答by Chipsaird
I figured it out, thanks for your help!
我想通了,谢谢你的帮助!
'Save button
'Activated on click
'Runs macro if that employee has not already been added to the WelfareTraining database
Private Sub SaveRecord_Click()
Dim stDocName As String
stDocName = "SaveRecordTraining"
If DCount("[Employee]", "WelfareTraining", "[Employee] = " & Me![Employee] & " ") > 0 Then
MsgBox ("This employee has already completed their training")
Else
DoCmd.RunMacro stDocName
End If
End Sub