How to code DLookup using multiple combo boxes?

34 Views Asked by At

I've been wrestling with how to code a DLookup on a form. I have two Combo Boxes (one Employee, one Training Class Name) and need a message box to pop up if an Employee has already taken a class. I am grabbing on record from the Employee table and one record from the Training Class table and if the combination of these two exists, I need to code the messagebox that they've already taken the class. Note I am not a VBA programmer; I've taken on an existing Access database project so I'm learning as I go from YouTube and wonderful community boards like this one.

Here's what I've tried that isn't working:

Private Sub Date_Taken_BeforeUpdate(Cancel As Integer)

If Not IsNull(DLookup("[Training Classes Taken tbl].[Officer]", "[Training Classes Taken tbl]", "[Training Classes Taken tbl].[Officer] = '" & Me.Officer & "'") & "'AND Me.[Training Class Name]= '" & Me.[Training Class]) Then

MsgBox "Class already taken."

Cancel = True

End If
   Exit Sub  
1

There are 1 best solutions below

0
On

DLookup looks for ("the field you want", "the table it comes from", and the "lookup field and value it should be") . So, I would probably use in the on click event of the button and try (assuming office:

Dim varx as variant

varx = Dlookup("Officer", "[Training Classes Taken]", "[Training Class Name]='" & me.[Training Class] & "'")

if not IsNull(varx) then
    msgbox("you have already taken this course")
end if