How to know if record changed in unbound form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am developing an unbound form, and I can load data, save data etc... But now that I am not using it as bound, afterupdate event is not usable as a form/record event. Is there a way top detect if the record is chaged without having to set a flag against every field? I am assuming it has to be a proccedure that loops through the fields and compares them to the recordset, and then sets a variable?
 
If you added a single field to your back-end/data, could you "flag" that
something was updated? One approach to this is to add a date/time field
that holds when the record was last edited/updated.

I'm a little confused about what you are comparing. If your form is
unbound, then when you load it from your back-end data source, it is not
changed. Are you looking to test to see if someone else has grabbed and
updated the record while you are holding a copy in your unbound form?
 
A user brings up the record, and may edit or change the record. Before they go to a new one, or close the form, I need to check and see if it has been changed, and ask the user if they want to update it.

So far what I do is pass the recordset from the form along with the form name to a proccedure. It goes through the recordset and compares the values of the recordset to the values in each fiels, and returns a true or a flase if the record has changed. It seems pretty fast, but I am wondering if there is simpler and fast ways to do it.
 
Sean Henry

I'm still a little unclear what's being compared...

Are you saying you need to check to see if the unbound form, once loaded,
has been changed? If so, you could keep that determination "local", without
revisiting the underlying data, PROVIDED that no one else could have changed
the back end. If someone else might have altered the data while you are
holding a copy on your form, you have a bigger problem -- whose version is
correct, if you make a change, but someone else already has?

If you only need to be concerned about a change to the local copy, could you
add a procedure and a variable to the form that would be "flagged" if/when a
field was altered? Each field would need to call this procedure, but only
if changed.
 
Right. So I have to add a proceedure to 50-60 fields? Ugghhh. So far what I have done is to compare the recordset I pulled to the value of each field andf see if anything is different. If it is, then I ask the user to confirm writing the changes. My code is probably not very effecient, so I thinght I would see what people think.
 
If you are asking about the (online help) 'dirty property' and the next one, the -->

Dirty Event
The Dirty event occurs when the contents of a form or the text portion of a combo box changes. It also occurs when you move from one page to another page in a tab control.

Private Sub Form_Dirty(Cancel As Integer)

The Dirty event procedure has the following argument.

Argument Description
Cancel The setting determines if the Dirty event occurs. Setting the Cancel argument to True cancels the Dirty event. You can also use the CancelEvent method of the DoCmd object to cancel the event.

Remarks
examples of this event include entering a character directly in the text box or combo box or changing the control's Text property setting by using a macro or Visual Basic.

Modifying a record within a form by using a macro or Visual Basic doesn't trigger this event. You must type the data directly into the record or set the control's Text property.
This event applies only to bound forms, not an unbound form or report.
To run a macro or event procedure when this event occurs, set the OnDirty property to the name of the macro or to [Event Procedure].

By running a macro or event procedure when a Dirty event occurs, you can determine if the record can be changed. You can also display a message and ask for edit permission.

Changing the data in a record by using the keyboard causes keyboard events to occur in addition to control events like the Dirty event. For example, if you move to a new record and type an ANSI character in a text box in the record, the following events occur in this order:

KeyDown > KeyPress > BeforeInsert > Dirty > KeyUp

The BeforeUpdate and AfterUpdate events for a record occur after you have entered the new or changed data in the record and moved to another record (or clicked Save Record on the Records menu), and therefore after the Dirty event for the record.

Canceling the Dirty event will cause the changes to the current record to be rolled back. It is equivalent to pressing the ESC key.

HTH
 
Does this work on an unbound form?
--
Sean Henry


jl said:
If you are asking about the (online help) 'dirty property' and the next one, the -->

Dirty Event
The Dirty event occurs when the contents of a form or the text portion of a combo box changes. It also occurs when you move from one page to another page in a tab control.

Private Sub Form_Dirty(Cancel As Integer)

The Dirty event procedure has the following argument.

Argument Description
Cancel The setting determines if the Dirty event occurs. Setting the Cancel argument to True cancels the Dirty event. You can also use the CancelEvent method of the DoCmd object to cancel the event.

Remarks
examples of this event include entering a character directly in the text box or combo box or changing the control's Text property setting by using a macro or Visual Basic.

Modifying a record within a form by using a macro or Visual Basic doesn't trigger this event. You must type the data directly into the record or set the control's Text property.
This event applies only to bound forms, not an unbound form or report.
To run a macro or event procedure when this event occurs, set the OnDirty property to the name of the macro or to [Event Procedure].

By running a macro or event procedure when a Dirty event occurs, you can determine if the record can be changed. You can also display a message and ask for edit permission.

Changing the data in a record by using the keyboard causes keyboard events to occur in addition to control events like the Dirty event. For example, if you move to a new record and type an ANSI character in a text box in the record, the following events occur in this order:

KeyDown > KeyPress > BeforeInsert > Dirty > KeyUp

The BeforeUpdate and AfterUpdate events for a record occur after you have entered the new or changed data in the record and moved to another record (or clicked Save Record on the Records menu), and therefore after the Dirty event for the record.

Canceling the Dirty event will cause the changes to the current record to be rolled back. It is equivalent to pressing the ESC key.

HTH
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


Sean Henry said:
I am developing an unbound form, and I can load data, save data etc... But now that I am not using it as bound, afterupdate event is not usable as a form/record event. Is there a way top detect if the record is chaged without having to set a flag against every field? I am assuming it has to be a proccedure that loops through the fields and compares them to the recordset, and then sets a variable?
 
I get you on this. But it is an awful lot of work to enter 50-60 proceedure calls for each field on every form I have.

What I ended up doing is this:

1. I pass thje current recordet and from name to a proceedure. It loops through the recrodset, and compares each field values to the recordset value. If it finds something different, it stops and returns a boolean value of true. Then I run the proceedure.

Unless there is a faster way to do this, I think this is the answer.
 
Yup <grin>. I use unbounds lots, have some tab forms, subforms, et al, and need to check for 'dirty' on some forms so as to Not update the forms. Read 'dirty procedure' in help/ask ?/ type dirty/cr
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


Sean Henry said:
Does this work on an unbound form?
--
Sean Henry


jl said:
If you are asking about the (online help) 'dirty property' and the next one, the -->

Dirty Event
The Dirty event occurs when the contents of a form or the text portion of a combo box changes. It also occurs when you move from one page to another page in a tab control.

Private Sub Form_Dirty(Cancel As Integer)

The Dirty event procedure has the following argument.

Argument Description
Cancel The setting determines if the Dirty event occurs. Setting the Cancel argument to True cancels the Dirty event. You can also use the CancelEvent method of the DoCmd object to cancel the event.

Remarks
examples of this event include entering a character directly in the text box or combo box or changing the control's Text property setting by using a macro or Visual Basic.

Modifying a record within a form by using a macro or Visual Basic doesn't trigger this event. You must type the data directly into the record or set the control's Text property.
This event applies only to bound forms, not an unbound form or report.
To run a macro or event procedure when this event occurs, set the OnDirty property to the name of the macro or to [Event Procedure].

By running a macro or event procedure when a Dirty event occurs, you can determine if the record can be changed. You can also display a message and ask for edit permission.

Changing the data in a record by using the keyboard causes keyboard events to occur in addition to control events like the Dirty event. For example, if you move to a new record and type an ANSI character in a text box in the record, the following events occur in this order:

KeyDown > KeyPress > BeforeInsert > Dirty > KeyUp

The BeforeUpdate and AfterUpdate events for a record occur after you have entered the new or changed data in the record and moved to another record (or clicked Save Record on the Records menu), and therefore after the Dirty event for the record.

Canceling the Dirty event will cause the changes to the current record to be rolled back. It is equivalent to pressing the ESC key.

HTH
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


Sean Henry said:
I am developing an unbound form, and I can load data, save data etc... But now that I am not using it as bound, afterupdate event is not usable as a form/record event. Is there a way top detect if the record is chaged without having to set a flag against every field? I am assuming it has to be a proccedure that loops through the fields and compares them to the recordset, and then sets a variable?
 
Sean Henry said:
I get you on this. But it is an awful lot of work to enter 50-60
proceedure calls for each field on every form I have.

If you make it a function you can select all of the controls and paste the
function into all of their event properties at one time.
 
Can you give me an example? I am lost. I am using anu nbound form here, so I need this to be triggered on the form event, not on each field right?
--
Sean Henry


jl said:
Yup <grin>. I use unbounds lots, have some tab forms, subforms, et al, and need to check for 'dirty' on some forms so as to Not update the forms. Read 'dirty procedure' in help/ask ?/ type dirty/cr
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


Sean Henry said:
Does this work on an unbound form?
--
Sean Henry


jl said:
If you are asking about the (online help) 'dirty property' and the next one, the -->

Dirty Event
The Dirty event occurs when the contents of a form or the text portion of a combo box changes. It also occurs when you move from one page to another page in a tab control.

Private Sub Form_Dirty(Cancel As Integer)

The Dirty event procedure has the following argument.

Argument Description
Cancel The setting determines if the Dirty event occurs. Setting the Cancel argument to True cancels the Dirty event. You can also use the CancelEvent method of the DoCmd object to cancel the event.

Remarks
examples of this event include entering a character directly in the text box or combo box or changing the control's Text property setting by using a macro or Visual Basic.

Modifying a record within a form by using a macro or Visual Basic doesn't trigger this event. You must type the data directly into the record or set the control's Text property.
This event applies only to bound forms, not an unbound form or report.
To run a macro or event procedure when this event occurs, set the OnDirty property to the name of the macro or to [Event Procedure].

By running a macro or event procedure when a Dirty event occurs, you can determine if the record can be changed. You can also display a message and ask for edit permission.

Changing the data in a record by using the keyboard causes keyboard events to occur in addition to control events like the Dirty event. For example, if you move to a new record and type an ANSI character in a text box in the record, the following events occur in this order:

KeyDown > KeyPress > BeforeInsert > Dirty > KeyUp

The BeforeUpdate and AfterUpdate events for a record occur after you have entered the new or changed data in the record and moved to another record (or clicked Save Record on the Records menu), and therefore after the Dirty event for the record.

Canceling the Dirty event will cause the changes to the current record to be rolled back. It is equivalent to pressing the ESC key.

HTH
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


:

I am developing an unbound form, and I can load data, save data etc... But now that I am not using it as bound, afterupdate event is not usable as a form/record event. Is there a way top detect if the record is chaged without having to set a flag against every field? I am assuming it has to be a proccedure that loops through the fields and compares them to the recordset, and then sets a variable?
 
Sorry for the delay, just logged in. So, here is my sub. And yes, use on form not fields, much cleaner/elegant a solution.

Private Sub Form_Dirty(Cancel As Integer)
On Error GoTo ErrorHandler

Cancel = True

ErrorHandlerExit:
Exit Sub

ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & _
Err.Description
Resume ErrorHandlerExit

End Sub

Use / reuse for any form.
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


Sean Henry said:
Can you give me an example? I am lost. I am using anu nbound form here, so I need this to be triggered on the form event, not on each field right?
--
Sean Henry


jl said:
Yup <grin>. I use unbounds lots, have some tab forms, subforms, et al, and need to check for 'dirty' on some forms so as to Not update the forms. Read 'dirty procedure' in help/ask ?/ type dirty/cr
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


Sean Henry said:
Does this work on an unbound form?
--
Sean Henry


:

If you are asking about the (online help) 'dirty property' and the next one, the -->

Dirty Event
The Dirty event occurs when the contents of a form or the text portion of a combo box changes. It also occurs when you move from one page to another page in a tab control.

Private Sub Form_Dirty(Cancel As Integer)

The Dirty event procedure has the following argument.

Argument Description
Cancel The setting determines if the Dirty event occurs. Setting the Cancel argument to True cancels the Dirty event. You can also use the CancelEvent method of the DoCmd object to cancel the event.

Remarks
examples of this event include entering a character directly in the text box or combo box or changing the control's Text property setting by using a macro or Visual Basic.

Modifying a record within a form by using a macro or Visual Basic doesn't trigger this event. You must type the data directly into the record or set the control's Text property.
This event applies only to bound forms, not an unbound form or report.
To run a macro or event procedure when this event occurs, set the OnDirty property to the name of the macro or to [Event Procedure].

By running a macro or event procedure when a Dirty event occurs, you can determine if the record can be changed. You can also display a message and ask for edit permission.

Changing the data in a record by using the keyboard causes keyboard events to occur in addition to control events like the Dirty event. For example, if you move to a new record and type an ANSI character in a text box in the record, the following events occur in this order:

KeyDown > KeyPress > BeforeInsert > Dirty > KeyUp

The BeforeUpdate and AfterUpdate events for a record occur after you have entered the new or changed data in the record and moved to another record (or clicked Save Record on the Records menu), and therefore after the Dirty event for the record.

Canceling the Dirty event will cause the changes to the current record to be rolled back. It is equivalent to pressing the ESC key.

HTH
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


:

I am developing an unbound form, and I can load data, save data etc... But now that I am not using it as bound, afterupdate event is not usable as a form/record event. Is there a way top detect if the record is chaged without having to set a flag against every field? I am assuming it has to be a proccedure that loops through the fields and compares them to the recordset, and then sets a variable?
 
Thanks for the clarification.

My dilemma is I need to know if a change happened and ask the user if they want to save the changes before they take the next action. In that case, would I check the dirty status before allowing them to continue?
--
Sean Henry


jl said:
In forms/properties, select On Dirty event, paste the 'dirty' subroutine, and yes, when a value changes, pre update/save event, leaving the form via undo, esc, et al, will trigger the 'dirty' event and the user will be back on a 'clean' form without any changes to the data. Luck.
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


Sean Henry said:
I think I am too dense. So I put this on a form. Then what? Thats the part I am stuck on. Anytime someone changes a field value, the dirty value is set?
--
Sean Henry


jl said:
Sorry for the delay, just logged in. So, here is my sub. And yes, use on form not fields, much cleaner/elegant a solution.

Private Sub Form_Dirty(Cancel As Integer)
On Error GoTo ErrorHandler

Cancel = True

ErrorHandlerExit:
Exit Sub

ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & _
Err.Description
Resume ErrorHandlerExit

End Sub

Use / reuse for any form.
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


:

Can you give me an example? I am lost. I am using anu nbound form here, so I need this to be triggered on the form event, not on each field right?
--
Sean Henry


:

Yup <grin>. I use unbounds lots, have some tab forms, subforms, et al, and need to check for 'dirty' on some forms so as to Not update the forms. Read 'dirty procedure' in help/ask ?/ type dirty/cr
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


:

Does this work on an unbound form?
--
Sean Henry


:

If you are asking about the (online help) 'dirty property' and the next one, the -->

Dirty Event
The Dirty event occurs when the contents of a form or the text portion of a combo box changes. It also occurs when you move from one page to another page in a tab control.

Private Sub Form_Dirty(Cancel As Integer)

The Dirty event procedure has the following argument.

Argument Description
Cancel The setting determines if the Dirty event occurs. Setting the Cancel argument to True cancels the Dirty event. You can also use the CancelEvent method of the DoCmd object to cancel the event.

Remarks
examples of this event include entering a character directly in the text box or combo box or changing the control's Text property setting by using a macro or Visual Basic.

Modifying a record within a form by using a macro or Visual Basic doesn't trigger this event. You must type the data directly into the record or set the control's Text property.
This event applies only to bound forms, not an unbound form or report.
To run a macro or event procedure when this event occurs, set the OnDirty property to the name of the macro or to [Event Procedure].

By running a macro or event procedure when a Dirty event occurs, you can determine if the record can be changed. You can also display a message and ask for edit permission.

Changing the data in a record by using the keyboard causes keyboard events to occur in addition to control events like the Dirty event. For example, if you move to a new record and type an ANSI character in a text box in the record, the following events occur in this order:

KeyDown > KeyPress > BeforeInsert > Dirty > KeyUp

The BeforeUpdate and AfterUpdate events for a record occur after you have entered the new or changed data in the record and moved to another record (or clicked Save Record on the Records menu), and therefore after the Dirty event for the record.

Canceling the Dirty event will cause the changes to the current record to be rolled back. It is equivalent to pressing the ESC key.

HTH
--
JL: Inpatient, OwItHurts Hospital for incurably logical thinkers. Legal -> Just my opinions.


:

I am developing an unbound form, and I can load data, save data etc... But now that I am not using it as bound, afterupdate event is not usable as a form/record event. Is there a way top detect if the record is chaged without having to set a flag against every field? I am assuming it has to be a proccedure that loops through the fields and compares them to the recordset, and then sets a variable?
 
Back
Top