Trapping Key Violations

K

Kerry Miller

I have a database, part of which contains the following tables:

tblVolunteer(Volunteer ID, First Name, Last Name, etc.)
tblHours(Volunteer ID, Program ID, Date, Hours)
tblProgram(Program ID, Program Name)

Please note that

The primary keys are the ID fields. I have designed a form (frmVolunteer)
which has a subform (sfrmHours). The forms are linked by the Volunteer ID
fields. Everything works fine. My only problem is when a user adds hours
without first adding a volunteer, it produces a key violation that Access
traps and displays a dialog box which reads "Index or primary key cannot
contain a null value" (this is a good thing). However I would like to trap
this error before Access and display my own dialog. I have tried every
technique I know and have searched help thoroughly. I'm stumped. I don't
know where or how to implement the error trap. Any help would be greatly
appreciated.

TIA,

K Miller
 
P

PC Datasheet

Kerry,

I use another approach with a form/subform. Open the main form in design view
and select the subform control. Put the following code in the Enter event:

If IsNull(Me!NameOfPrimaryKeyTextbox) Then
MsgBox "Enter Main Form Record First"
End If

This prevents entering a record in the subform before a record is created in the
main form.
 
K

Kerry Miller

Whoa, that was fast! Thanks a heap

K Miller

PC Datasheet said:
Kerry,

I use another approach with a form/subform. Open the main form in design view
and select the subform control. Put the following code in the Enter event:

If IsNull(Me!NameOfPrimaryKeyTextbox) Then
MsgBox "Enter Main Form Record First"
End If

This prevents entering a record in the subform before a record is created in the
main form.


--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
(e-mail address removed)
www.pcdatasheet.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top