简体   繁体   中英

Check if record already exists in table Access 2010 VBA

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.

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?"

I am relatively new to VBA, however I assume I need some sort of Count method on the employee ID. Thanks in advance!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM