Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trap a warning 1

Status
Not open for further replies.

mymou

Technical User
May 21, 2001
355
GB
Hi All

I have a form with a series of subreports.

Everything works fine - but a foreign key field will generate an error - which is correct and valid - but I would like to change its content to something that the users understand!

This is generated when a component is added in the component subform and the product subform is ready to add a new value.

'The field 'tblComponents.ProductID' cannot contain a Null value because the Required property for this field is set to True. Enter a value in this field.'

How do I trap this one?

Stew
 
First of all you have to determine exactly which Event Procedure the action being take is erroring out on. Is it the AfterUpdate of the SubForm Control? Or, one of the SubForm events.

Once that is determined you can use this generic code to trap for the error. You will have to determine the error # by running the process and having the ACCESS error pop up first.

Private Sub XXXX_Event()
On Error Goto XXXX_Event_Error_Trap

. . . Other code for this procedure

XXXX_Event_Exit:
Exit Sub

XXXX_Event_Error_Trap:
If Err = ???? then
MsgBox "Put your custom message here?"
me![ProductID].setfocus 'Send Focus to Null control
GoTo XXXX_Event_Exit
end if
MsgBox "Err#: " & Err & "Description: " & Err.Description
GoTo XXXX_Error_Event_Exit
End Sub

This code represents the format for trapping an error in any event procedure. Remember you have to first determine where the actual error is occurring in your process and then place the modified code into the appropriate event to trap for the error.

Good luck. Let me know if there is anything else I can help with on this one. Bob Scriver
 
Use the Error event of the form. Something like:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
Dim StrMsg$
Select Case DataErr 'this is the error number
Case 3022 'Duplicate key
Response = acDataErrContinue
StrMsg = "Duplicate value"
Case Else
StrMsg ="Whatever message"
End Select
MsgBox StrMsg
End Sub

Good luck... [pipe]
Daniel Vlas
Systems Consultant
danvlas@yahoo.com
 


The problem is that I cannot identify where this error is generated.

I have a workaround now - that simple uses the 'on enter' event for the subform - that validates all requirements. This works just fine.

So I have avoided the error - rather than trapping it. I would like to know how to find out where this error is produced - just for future reference.

Thanks

Stew
 
We are not talking about VB run-time errors, but about Jet errors.
Your VB code may work great, but when it comes to duplicates, null values in primary keys, required fields not filled in and other cases, you get a Jet generated error. Such things are trappable through the Error event of forms.
Read the Help on the Error event...

[pipe]
Daniel Vlas
Systems Consultant
danvlas@yahoo.com
 
danvlas

You were 100% right. Next time just ask me to read your posts more carefully!

Thanks for the VB/jet error distinction.

Stew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top