My Assistant
![]() ![]() |
|
|
Feb 28 2012, 03:37 PM
Post
#1
|
|
|
UtterAccess Veteran Posts: 377 From: Sydney, Australia |
Hi
This is a theoretical question in my quest to better understand the dirty event. I have a simple table with 3 fields and a bound datasheet form. In the form’s On Dirty event I have coded a MsgBox. If I change all 3 fields the MsgBox displays only when I change the first field. This led me to believe that once the form is Dirty it stays Dirty until either move to a new record, refresh screen or save record. So I added the line Me.Dirty = False in the On Dirty event which I understand forces the record to be saved. The MsgBox still only displays after I change the first field? Regards John |
|
|
|
Feb 28 2012, 05:07 PM
Post
#2
|
|
|
UtterAccess VIP Posts: 1,402 From: Northern Virginia |
when you change the "first" field, the form becomes dirty.
Saving the record makes it 'not-dirty' Setting Me.dirty = false does NOT save the record. <-- not true, depending on context (where and how used) see details below in following posts Put the following in your form and play around with it. (Note these are all form level events) Private Sub Form_AfterUpdate() MsgBox "After update: Form dirty status is: " & me.dirty End Sub Private Sub Form_BeforeUpdate(Cancel As Integer) MsgBox "before update-record is about to be saved. Form dirty status is currently: " & me.dirty End Sub Private Sub Form_Dirty(Cancel As Integer) MsgBox "Dirty-Trying to make it making it NOT dirty" Me.Dirty = False msgbox "is the record still unsaved? (Check for the pencil icon). Dirty status is currently:" & Me.dirty End Sub Hope this helps. This post has been edited by CyberCow: Feb 29 2012, 11:42 AM
Reason for edit: Correction and clarification of OP's statement
|
|
|
|
Feb 28 2012, 05:31 PM
Post
#3
|
|
|
UtterAccess Veteran Posts: 377 From: Sydney, Australia |
Hi rsindle
Thanks for that. "Me.dirty = false does NOT save the record" makes it a bit more clearer. I will play around with the code as you suggest. Regards John |
|
|
|
Feb 28 2012, 05:59 PM
Post
#4
|
|
|
Access Wiki and Forums Moderator Posts: 48,114 From: SoCal, USA |
Hi John,
I think there is a difference between the Dirty property and the Dirty event. I believe the Dirty event fires as soon as a "record change" action is performed but the Dirty property doesn't get set to True until a field has been updated. Setting the Dirty property to False when it is still False doesn't have any effect. Setting it to False when it is True DOES save the record to the table. Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) |
|
|
|
Feb 28 2012, 06:27 PM
Post
#5
|
|
|
UtterAccess Veteran Posts: 377 From: Sydney, Australia |
Hi dbGuy
OH, now I am a bit confused again. If I understand you correctly why is the addition of Me.Dirty = False in the On Dirty event not working as I expect then? Regards John |
|
|
|
Feb 28 2012, 06:29 PM
Post
#6
|
|
|
Access Wiki and Forums Moderator Posts: 48,114 From: SoCal, USA |
Hi John,
That's because the Dirty property is not yet set to True at the time the Dirty event fires. Like I said, setting the Dirty property to False when it's still False has no effect at all. Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) |
|
|
|
Feb 28 2012, 07:04 PM
Post
#7
|
|
|
UtterAccess Veteran Posts: 377 From: Sydney, Australia |
dbGuy
Sorry if I am being a bit of a moron but I added this code to try & work out what is going on: Private Sub Form_Dirty(Cancel As Integer) MsgBox "dIRTY" Me.Dirty = False End Sub So, I would have expected that when I change the first field the On Dirty event fires, which it does because i get the msgbox. Then I thought Me.Dirty = False would save the record. But when I change the next field the On Dirty event does not fire. What am I missing? Thanks for you patience John |
|
|
|
Feb 28 2012, 10:50 PM
Post
#8
|
|
|
UtterAccess VIP Posts: 18,396 From: Oklahoma City, Oklahoma |
dbGuy Sorry if I am being a bit of a moron but I added this code to try & work out what is going on: Private Sub Form_Dirty(Cancel As Integer) MsgBox "dIRTY" Me.Dirty = False End Sub So, I would have expected that when I change the first field the On Dirty event fires, which it does because i get the msgbox. Then I thought Me.Dirty = False would save the record. But when I change the next field the On Dirty event does not fire. What am I missing? Thanks for you patience John John, That is not how it works. The following would probably be closer to what you expect to see. Note: The record is not getting saved. CODE Private Sub Form_Dirty(Cancel As Integer) MsgBox "dIRTY" Cancel = True End Sub The Form's Dirty even is what sets the form's dirty property if it is not Cancelled. The Dirty property is not actually set to truu until the event has happened (completed successfully). You can use the On Dirty event to allow change to be made. Example: CODE Private Sub Form_Dirty(Cancel As Integer) If MsgBox("Allow Changes?", vbYesNo) = vbNo Then Cancel = True End Sub If you set Cancel = True then no changes are allowed and the control is not changed. Until the On Dirty event completes with Cancel = False (the default) no controls are allowed to be updated. Nothing has actually changed when the On Dirty event first fires. It is not until the event completes without being canceled is any control allowed to change. When a control actual changes is when the form's Dirty property is set to yes.You will need to use some other event to save the record. I put together a little example that may help. See attached example created in Access 2007 format.
Attached File(s)
|
|
|
|
Feb 29 2012, 12:31 AM
Post
#9
|
|
|
Access Wiki and Forums Moderator Posts: 48,114 From: SoCal, USA |
Hi John,
dbGuy Sorry if I am being a bit of a moron but I added this code to try & work out what is going on: Private Sub Form_Dirty(Cancel As Integer) MsgBox "dIRTY" Me.Dirty = False End Sub So, I would have expected that when I change the first field the On Dirty event fires, which it does because i get the msgbox. Then I thought Me.Dirty = False would save the record. But when I change the next field the On Dirty event does not fire. What am I missing? Thanks for you patience John Boyd's demo probably explained this already but I told you that when the Dirty event fires, the Dirty property is still set at False so, therefore, setting it to False again will not do anything. I don't know why you keep on expecting that it will do something. Try putting the Me.Dirty = False code in the AfterUpdate event of your first field and then see what happens when you try to change the second field. Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) |
|
|
|
Feb 29 2012, 01:36 AM
Post
#10
|
|
|
UtterAccess Ruler Posts: 2,670 |
And just to add fuel to the fire (as a child I always did like squirting more fire-starter on the already flaming outdoor grill! (IMG:style_emoticons/default/big_grin.gif) ) and to further your quest for knowledge, a few more facts about Dirtying a Record and Saving a Record:
Linq ;0)> |
|
|
|
Feb 29 2012, 07:28 AM
Post
#11
|
|
|
UtterAccess VIP Posts: 1,402 From: Northern Virginia |
Can someone delete my post in this thread? or modify it?
Although maybe useful, my statement: QUOTE Setting Me.dirty = false does NOT save the record. is wrong/misleading. (In my contrived example, setting me.dirty does NOT save the record in the event where I put it, BUT, as a couple of others pointed out, it DOES save the record in general). And I HATE sounding authoritative when I'm wrong. (IMG:style_emoticons/default/shocked.gif) Or, if you can't delete the post, maybe this post will help... Sorry for the misleading/wrong info. Rob |
|
|
|
Feb 29 2012, 06:32 PM
Post
#12
|
|
|
UtterAccess Veteran Posts: 377 From: Sydney, Australia |
Thank you again everyone for this very helpful information (IMG:style_emoticons/default/thanks.gif)
|
|
|
|
Mar 1 2012, 12:55 AM
Post
#13
|
|
|
Access Wiki and Forums Moderator Posts: 48,114 From: SoCal, USA |
Hi John,
Thank you again everyone for this very helpful information (IMG:style_emoticons/default/thanks.gif) (IMG:style_emoticons/default/yw.gif) We are all happy to help. Good luck! |
|
|
|
Mar 1 2012, 01:18 AM
Post
#14
|
|
|
UtterAccess VIP Posts: 18,396 From: Oklahoma City, Oklahoma |
John,
Hope the example was helpful. |
|
|
|
Mar 1 2012, 07:11 PM
Post
#15
|
|
|
UtterAccess Veteran Posts: 377 From: Sydney, Australia |
Yes it was.
The code given solved my initial query. Private Sub Form_Dirty(Cancel As Integer) MsgBox "dIRTY" Cancel = True End Sub (IMG:style_emoticons/default/thumbup.gif) |
|
|
|
Mar 3 2012, 01:26 PM
Post
#16
|
|
|
UtterAccess VIP Posts: 18,396 From: Oklahoma City, Oklahoma |
Thanks for the update.
If you are wantng to make a for read-only you can also set the form's record sour type to snapshot. This will prevent all edits. If you want to turn on and of the ablilty to edit then you can set the Form.s Allow Edits property as needed. |
|
|
|
Mar 5 2012, 07:09 AM
Post
#17
|
|
|
UA Forum + Wiki Administrator Posts: 11,961 From: Sudbury, Ontario, Canada |
Can someone delete my post in this thread? or modify it? Although maybe useful, my statement: is wrong/misleading. (In my contrived example, setting me.dirty does NOT save the record in the event where I put it, BUT, as a couple of others pointed out, it DOES save the record in general). And I HATE sounding authoritative when I'm wrong. (IMG:style_emoticons/default/shocked.gif) Or, if you can't delete the post, maybe this post will help... Sorry for the misleading/wrong info. Rob Rob, I know the feeling well of being proved wrong when I have been authoritative on an issue. Nevertheless, your post. and similar (ie 'wrong/misleading') posts in thousands of other threads, is still helpful to anyone following the discussion. That's why, as general rule, we don't delete them. We don't want to prolong your embarrassment, but would prefer to leave the post as is. I have always found Dirty somewhat obscure and don't feel that its name really conveys what it does and does not do. This thread, including your part in it, may well help others who happen on the thread in the future to cut through the obscurity. Glenn Glenn |
|
|
|
Mar 5 2012, 07:15 AM
Post
#18
|
|
|
UtterAccess VIP Posts: 1,402 From: Northern Virginia |
Hey Glenn,
I'm OK with that. Love this forum. I find it by far the most helpful because there are so many willing to offer their time/experience. Rob |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 26th May 2013 - 01:28 AM |