Undo when move to next record or close form

  • Thread starter gmazza via AccessMonster.com
  • Start date
G

gmazza via AccessMonster.com

Hey there,
I check for some validation after an update(s) is made to the form. If the
validation fails, I want to do undo the change(s) made. However, if the user
either closes the form, OR navigates to the next record, its not working
because I have a subform come up for validation, when they cancel out or the
vaildation fails for them, it errors on Undo because the form is either
closed so there is nothing to undo, thus saving the changes in the table
which is what I don't want, OR it navigates to the next record and again the
Undo errors and again the changes are saved.
I tried the Before Update and After Update on the form to call my validation
subform, either doesn't work.
I need to get back to the record they changed when validation fails, or they
cancel out, and Undo it.
Thanks!
 
F

fredg

Hey there,
I check for some validation after an update(s) is made to the form. If the
validation fails, I want to do undo the change(s) made. However, if the user
either closes the form, OR navigates to the next record, its not working
because I have a subform come up for validation, when they cancel out or the
vaildation fails for them, it errors on Undo because the form is either
closed so there is nothing to undo, thus saving the changes in the table
which is what I don't want, OR it navigates to the next record and again the
Undo errors and again the changes are saved.
I tried the Before Update and After Update on the form to call my validation
subform, either doesn't work.
I need to get back to the record they changed when validation fails, or they
cancel out, and Undo it.
Thanks!

And the actual Validation needed is?????

The proper place to do validation is in the Form's BeforeUpdate event.
Something like this:

Private Form_BeforeUpdate(Cancel as Integer)
If Me.[SomeControl] = "This" Or Me.[SomeControl] = "That" then
Else
MsgBox "Wrong data entered."
Cancel = True
End If

The above will cancel the save and return you to the form for entry
correction if anything other than "This" or "That' has been entered.

Place the same code in the Form's Unload event.
That will prevent incorrect data entry if the user tries to close the
form with the incorrect entry.
 
G

gmazza via AccessMonster.com

I understand what you are saying, that makes sense, and I currently do have
it in my Before Update Event but does it work the same for a subform coming
up for validation.
What happens is if the user changes a field and either closes the form OR
changes records, a sub form pops up and requests their password. If it fails
OR if they cancel the subform I need to undo the changes they made to the
field on the form.
Right now I change the field, close the form, up comes my pop up subform,
when I cancel that it complains it can't undo because the form closed. Same
goes for if they change records.
Thanks for your help Fredg!

Hey there,
I check for some validation after an update(s) is made to the form. If the
[quoted text clipped - 10 lines]
cancel out, and Undo it.
Thanks!

And the actual Validation needed is?????

The proper place to do validation is in the Form's BeforeUpdate event.
Something like this:

Private Form_BeforeUpdate(Cancel as Integer)
If Me.[SomeControl] = "This" Or Me.[SomeControl] = "That" then
Else
MsgBox "Wrong data entered."
Cancel = True
End If

The above will cancel the save and return you to the form for entry
correction if anything other than "This" or "That' has been entered.

Place the same code in the Form's Unload event.
That will prevent incorrect data entry if the user tries to close the
form with the incorrect entry.
 
J

JimBurke via AccessMonster.com

I don' think that a subform is really appropriate for what you're trying to
do, and you can't cancel anything after you close the form. You wrote 'Right
now I change the field, close the form, up comes my pop up subform' - well,
that's why you can't undo anything - you've already closed the form. it also
doesn't make sense to me that you could be using a subform if you're saying
you close the form and then the subform pops up. When you say subform do you
really mean a pop-up form?

I also don't understand why you would ask for a password after making changes
- I would think you would prompt for the password before they make any
changes. Is a password required for every single record that they change, or
can you prompt for it once when the form is opened? Does the user only view
one record when they open the form, or can they navigate to different records?


If you really need to prompt for the password after they have made changes
but before they're saved, I would think you would want to open a pop-up form
in the before update event proc (don't close the form yet!) to prompt for the
password. On your 'main' form create a hidden textbox that keeps track of
whether the password was entered. Before opening the pop-up, set it to false.
The pop-up form would just have a textbox for the password and an OK button
and a Cancel button. The OK button would check the password and set the main
form's hidden textbox to true if it is the correct pw, then close the form.
The cancel button would just close the form. Then in the main form's before
update proc, after the OpenForm for the pop-up, check the value of the
hidden textbox. If it is false, set Cancel = True so that the Update is
cancelled.
I understand what you are saying, that makes sense, and I currently do have
it in my Before Update Event but does it work the same for a subform coming
up for validation.
What happens is if the user changes a field and either closes the form OR
changes records, a sub form pops up and requests their password. =-f it fails
OR if they cancel the subform I need to undo the changes they made to the
field on the form.
Right now I change the field, close the form, up comes my pop up subform,
when I cancel that it complains it can't undo because the form closed. Same
goes for if they change records.
Thanks for your help Fredg!
[quoted text clipped - 20 lines]
That will prevent incorrect data entry if the user tries to close the
form with the incorrect entry.
 
G

gmazza via AccessMonster.com

Hello Jim, thanks for your reply.
You are right, its not a subform, its a pop-up form.
The user can view any number of records at any time. I have it working for
changing 1 field, because the form is still open, and it goes back to the
original form after the pop up and if the password was right the change is
saved and if the password was wrong then the change is undone.
I now need a different form to work for changing an entire record. So when
they are done changing a record, I want to propmt them with the pop-up. The
way to determine if they are done changing an entire record is uf they goto
the next record or close the form and thats when I run into the problem. I
have my code to open the pop-up and ask for their password in the Before
Update on the form, but if they make a change and then close the form its
saving the change and still opening the pop-up.
I can't prompt when the form opens, has to be done after a change is made to
a record.
You say to put it in the Before Update and don't close the form but thats
what a user does sometime, they close the form, or navigate to the next
record and thats when I need the pop-up to come up, and at the same time keep
the form open on the same record so if the password is wrong it can undo.
I hope I am making sense and your help is appreciated!
G
I don' think that a subform is really appropriate for what you're trying to
do, and you can't cancel anything after you close the form. You wrote 'Right
now I change the field, close the form, up comes my pop up subform' - well,
that's why you can't undo anything - you've already closed the form. it also
doesn't make sense to me that you could be using a subform if you're saying
you close the form and then the subform pops up. When you say subform do you
really mean a pop-up form?

I also don't understand why you would ask for a password after making changes
- I would think you would prompt for the password before they make any
changes. Is a password required for every single record that they change, or
can you prompt for it once when the form is opened? Does the user only view
one record when they open the form, or can they navigate to different records?

If you really need to prompt for the password after they have made changes
but before they're saved, I would think you would want to open a pop-up form
in the before update event proc (don't close the form yet!) to prompt for the
password. On your 'main' form create a hidden textbox that keeps track of
whether the password was entered. Before opening the pop-up, set it to false.
The pop-up form would just have a textbox for the password and an OK button
and a Cancel button. The OK button would check the password and set the main
form's hidden textbox to true if it is the correct pw, then close the form.
The cancel button would just close the form. Then in the main form's before
update proc, after the OpenForm for the pop-up, check the value of the
hidden textbox. If it is false, set Cancel = True so that the Update is
cancelled.
I understand what you are saying, that makes sense, and I currently do have
it in my Before Update Event but does it work the same for a subform coming
[quoted text clipped - 13 lines]
 
J

JimBurke via AccessMonster.com

I'm guessing that your code is working for when you navigate to a a new
record - Before Update should handle that. If they close the form, is there
any reason to keep it open, or is it simply a matter of saving the changes if
they enter the correct password? If you may need to keep the form open for
any reason, put code in the forms UnLoad event proc, and cancel the On Unload
event if you want to keep the form open. Otherwise use the On Close proc. In
either case, If they made changes and don't enter the right password, undo
the changes. Then, if you have a situation where the form should stay open,
in the On Unload, after undoing the changes, set Cancel = True and that will
cancel the closing of the form. On Close does not have a Cancel option, On
Unload does.

Hello Jim, thanks for your reply.
You are right, its not a subform, its a pop-up form.
The user can view any number of records at any time. I have it working for
changing 1 field, because the form is still open, and it goes back to the
original form after the pop up and if the password was right the change is
saved and if the password was wrong then the change is undone.
I now need a different form to work for changing an entire record. So when
they are done changing a record, I want to propmt them with the pop-up. The
way to determine if they are done changing an entire record is uf they goto
the next record or close the form and thats when I run into the problem. I
have my code to open the pop-up and ask for their password in the Before
Update on the form, but if they make a change and then close the form its
saving the change and still opening the pop-up.
I can't prompt when the form opens, has to be done after a change is made to
a record.
You say to put it in the Before Update and don't close the form but thats
what a user does sometime, they close the form, or navigate to the next
record and thats when I need the pop-up to come up, and at the same time keep
the form open on the same record so if the password is wrong it can undo.
I hope I am making sense and your help is appreciated!
G
I don' think that a subform is really appropriate for what you're trying to
do, and you can't cancel anything after you close the form. You wrote 'Right
[quoted text clipped - 28 lines]
 
G

gmazza via AccessMonster.com

It isn't working either when navigating to the next record because when I hit
the next record button, I see it goto the next record, then I get the pop-up,
and I assure you my code is in the Before Update.
There is no reason to keep the form open when they close it, its just
screwing up because if they change the field, then close the form, my pop-up
is still coming up because its firing on Before Update, yet the form is still
closing and the record is still getting saved.
I'm guessing that your code is working for when you navigate to a a new
record - Before Update should handle that. If they close the form, is there
any reason to keep it open, or is it simply a matter of saving the changes if
they enter the correct password? If you may need to keep the form open for
any reason, put code in the forms UnLoad event proc, and cancel the On Unload
event if you want to keep the form open. Otherwise use the On Close proc. In
either case, If they made changes and don't enter the right password, undo
the changes. Then, if you have a situation where the form should stay open,
in the On Unload, after undoing the changes, set Cancel = True and that will
cancel the closing of the form. On Close does not have a Cancel option, On
Unload does.
Hello Jim, thanks for your reply.
You are right, its not a subform, its a pop-up form.
[quoted text clipped - 23 lines]
 
J

JimBurke via AccessMonster.com

Can you post the code you are using in the before update event? And you're
certain you have the pop-up form properties set so that PopUp is True and
Modal is False?
It isn't working either when navigating to the next record because when I hit
the next record button, I see it goto the next record, then I get the pop-up,
and I assure you my code is in the Before Update.
There is no reason to keep the form open when they close it, its just
screwing up because if they change the field, then close the form, my pop-up
is still coming up because its firing on Before Update, yet the form is still
closing and the record is still getting saved.
I'm guessing that your code is working for when you navigate to a a new
record - Before Update should handle that. If they close the form, is there
[quoted text clipped - 13 lines]
 
G

gmazza via AccessMonster.com

Popup is set to Yes and Modal is set to No.
Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim stDocName As String
Dim stLinkCriteria As String

ModifierID = ap_GetUserName
ModifierDate = TimeAndDate
bWasNewRecord = Me.NewRecord
Call AuditEmployee.AuditEditBegin("Employee", "audTmpEmployee",
"EmployeeID", Nz(Me.EmployeeID, 0), bWasNewRecord)

stDocName = "Security"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Forms!Security.txtPassword.SetFocus
End Sub
Can you post the code you are using in the before update event? And you're
certain you have the pop-up form properties set so that PopUp is True and
Modal is False?
It isn't working either when navigating to the next record because when I hit
the next record button, I see it goto the next record, then I get the pop-up,
[quoted text clipped - 9 lines]
 
J

JimBurke via AccessMonster.com

You're not opening your security form as a popup. The security form is the
one that needs the popup property set, not the form that is doing the editing.
Try this when opening the security form:

DoCmd.OpenForm stDocName, acNormal, , , acFormEdit, acDialog

Also, you should be setting the focus of the security form's control in that
form's open event proc. I also don't see where you're trying to undo anything.
Where's your code to undo changes? The only thing the security form should do
is get the user password and set a flag to indicate whether it is a valid
password or not. The statement in the BeforeUpdate proc after the call to
open the security form should be checking if the password is valid, and if
not, undoing the changes (see my comments from earlier about creating a
hidden field for the flag).

Popup is set to Yes and Modal is set to No.
Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim stDocName As String
Dim stLinkCriteria As String

ModifierID = ap_GetUserName
ModifierDate = TimeAndDate
bWasNewRecord = Me.NewRecord
Call AuditEmployee.AuditEditBegin("Employee", "audTmpEmployee",
"EmployeeID", Nz(Me.EmployeeID, 0), bWasNewRecord)

stDocName = "Security"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Forms!Security.txtPassword.SetFocus
End Sub
Can you post the code you are using in the before update event? And you're
certain you have the pop-up form properties set so that PopUp is True and
[quoted text clipped - 5 lines]
 
G

gmazza via AccessMonster.com

That Open Form statement did the trick, thanks for your help Jim!
You're not opening your security form as a popup. The security form is the
one that needs the popup property set, not the form that is doing the editing.
Try this when opening the security form:

DoCmd.OpenForm stDocName, acNormal, , , acFormEdit, acDialog

Also, you should be setting the focus of the security form's control in that
form's open event proc. I also don't see where you're trying to undo anything.
Where's your code to undo changes? The only thing the security form should do
is get the user password and set a flag to indicate whether it is a valid
password or not. The statement in the BeforeUpdate proc after the call to
open the security form should be checking if the password is valid, and if
not, undoing the changes (see my comments from earlier about creating a
hidden field for the flag).
Popup is set to Yes and Modal is set to No.
Private Sub Form_BeforeUpdate(Cancel As Integer)
[quoted text clipped - 19 lines]
 

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