Link to home
Start Free TrialLog in
Avatar of JohnSaint
JohnSaint

asked on

Dlookup problem with joint PK Subform

I have created a form and subform
The main form 'Students' is PK'd on 'Student code'
The Subform 'TblEnrolmentBookings' is based on a joint PK of 'Student code' and 'Course Code', so I have a list of booking for a particular student


I am try to prevent users from booking students on the same course twice using Dlookup...

Private Sub Course_Code_LostFocus()
    If Me.NewRecord And Not IsNull(DLookup("[Course Code]", "TblEnrolmentBookings", _
       "[Student Code]='" & Me![Student Code] & "'")) Then
   'PK already exists
        Debug.Print Me![Student Code]
        MsgBox "Student already booked on this course. Choose another"
        DoCmd.GoToControl "[Course Code]"
    End If
End Sub

I thought the lookup would return null if it didn't find the course code in the table but it is flagging it every time, even when the course code is new to the entire Bookings tbale, not just for the significant Student Code.
To add insult to injury, the GoToControl action doesn't even work. It just moves to the next field!

Help! and thanks in advance
 
SOLUTION
Avatar of stevbe
stevbe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JohnSaint
JohnSaint

ASKER

That didn't seem to work. And what do you make of these results..

?DLookup("[Course Code]", "TblEnrolmentBookings", "[Student Code]='AA11'")
21

The first course code in the list is 21

I tried to take it a step further....

?DLookup("'33333'", "TblEnrolmentBookings", "[Student Code]='AA11'")
33333

There is no 33333 in the table.

Any ideas??? I am stumped!
is there a student code AA11 in the bookings table?

TblEnrolmentBookings has fields Course Code and Student Code?
If it's a joint PK, you'll need to check both the student and course values in the dlookup (although you only need to return one field),i.e:

    If Me.NewRecord And Not IsNull(DLookup("[Course Code]", "TblEnrolmentBookings", _
       "[Student Code]='" & Me![Student Code] & "' AND [Course Code]='" & Me![Course Code] & "'")) Then
Ah! thanks, that worked. I was confused because it was flagging course codes weren't in the table at all for any student!

My GoToConrol command still doesn't work though. Any ideas?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
shane ... out of curiosity ... why the Exit vent and not the before_update?

Steve
Either one will do really - it's a matter of preference I suppose. I personally do tend to use BeforeUpdate but it's just that when I see code that uses LostFocus I automatically think "switch it to Exit instead"....don't ask me why :)
I use Before_Update because it will stop the data from being written and the Exit will only stop you from moving to a different control, if you only use Exit and then try to navigate a record (nav buttons) it will not switch to the next record but it WILL commit your changes.

Steve