How to stop acces from saving the record

C

Chipcom

Hi

I am using main form and subform and when I focus on the sub form
Access saves the record and I need to know how to stop acces from
saving the record when the subform is on focus?

I understand that I need to unbound the main form and then bound it on
before update event.
I tried this code but I get some errors.

On Error GoTo Error_Handler
Dim db As DAO.Database
Dim rst As DAO.Recordset


Set db = CurrentDb
Set rst = db.OpenRecordset("tblWhatever", dbOpenDynaset)


With rst
.AddNew
!WhateverID = Me.txtWhateverID
!FirstName = Me.txtFName
!LastName = Me.txtLName
.Update
End With


Exit_Here:
rst.Close
Set rst = Nothing
Set db = Nothing


Error_Handler:
MsgBox Err.Number
Resume Exit_Here


Hi

I tried the code with form's Before Update even But I getting:
Error 0
Error 20
Error 91


What I do wrong?
 
C

Chipcom

Hello Chipcom.














Be sure to insert "Exit Sub" before the Error_Handler label.
That will prevent all 3 errors:
0: Err.Number 0 was displayed because no error had occured and you did
not prevent the code execution at label "Error_Handler" in this case.
20: There was no error (Err.Number as zero), so resume is not allowed.
This "resume" caused a runtime error that was trapped by your handler, so
this time "Resume" was allowed, the execution continues at "Exit_Here".
But here you try to execute the close method of an object that no longer
exists, because the variable was already set to nothing before "Error 0"
was shown, this caused Error 91.
--
Regards,
Wolfgang- Hide quoted text -

- Show quoted text -

If I'll use the Exit Sub I'll get error 2105 bacuse the before update
procedure didn't complate the procedure (didn't reach to end sub). is
that correct?

What should I do?

Thanks
 
C

Chipcom

Hello Chipcom.







No. What is Error 2105?

I was just explaining why the errors 0, 20 and 91 are shown. Try this:

On Error GoTo Error_Handler
Dim db As DAO.Database
Dim rst As DAO.Recordset

Set db = CurrentDb
Set rst = db.OpenRecordset("tblWhatever", dbOpenDynaset)

With rst
.AddNew
!WhateverID = Me.txtWhateverID
!FirstName = Me.txtFName
!LastName = Me.txtLName
.Update
End With

Exit_Here:
rst.Close
Set rst = Nothing
Set db = Nothing
Exit Sub ' That's my new line!

Error_Handler:
MsgBox Err.Number
Resume Exit_Here

--
Regards,
Wolfgang- Hide quoted text -

- Show quoted text -

The code is written in beforeupdate so if you exit sub the
beforeupdate the data can not be saved in the record therefore error
2105 means that you can't go to an new record.


How to solve the problem?
 
C

Chipcom

Hello Chipcom.









If you get an error when jumping to a new record, this is not caused
by not completing the BeforeUpdate procedure, because you did not
include "Cancel = True" in your code.
So, if you get an error, stating that you could not move to another
record, this has another reason, depeding on Whatever you do.

When not exiting the sub before the Error_Handler label, the code
after that label will be executed, no matter if or if not an error
occurred.

--
Regards,
Wolfgang- Hide quoted text -

- Show quoted text -

I tried "Cancel = True" but I still get error 2105.
I don't know what could be the reason for that error.
Any Idea can be helpful.
 

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