Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dlookup problem with joint PK Subform

Posted on 2004-10-11
9
Medium Priority
?
282 Views
Last Modified: 2012-05-05
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
 
0
Comment
Question by:JohnSaint
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 39

Assisted Solution

by:stevbe
stevbe earned 400 total points
ID: 12280656
I would use the BeforeUpdate and then cancel it if there is already a match.

Private Sub Course_Code_BeforeUpdate(Cancel As Integer)
If Me.NewRecord = True Then
    If Not IsNull(DLookup("[Course Code]", "TblEnrolmentBookings", "[Student Code]='" & Me![Student Code] & "'")) Then
         Msgbox "Already Exists"
         Cancel = True
    End If
End If
End Sub

can you get the dlookup to work if you hardcode values in the immediate window (ctl+g)

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

Steve
0
 

Author Comment

by:JohnSaint
ID: 12280854
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!
0
 
LVL 39

Expert Comment

by:stevbe
ID: 12280931
is there a student code AA11 in the bookings table?

TblEnrolmentBookings has fields Course Code and Student Code?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12280979
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
0
 

Author Comment

by:JohnSaint
ID: 12281075
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?
0
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 1600 total points
ID: 12281128
Put your code in the OnExit event rather than the LostFocus event. Then instead of the GotoControl line, use:
Cancel=True

Cancelling an event only applies to certain events - not the LostFocus, so that's why you have to move it to Exit.
0
 
LVL 39

Expert Comment

by:stevbe
ID: 12281440
shane ... out of curiosity ... why the Exit vent and not the before_update?

Steve
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12281533
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 :)
0
 
LVL 39

Expert Comment

by:stevbe
ID: 12281925
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
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question