Chipsaird
Chipsaird

Reputation: 21

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!

Upvotes: 1

Views: 21145

Answers (1)

Chipsaird
Chipsaird

Reputation: 21

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

Upvotes: 1

Related Questions