How to not trigger dirty event when initializing control on form through VB?

  • Thread starter Thread starter P
  • Start date Start date
P

P

Hi,

I have a form with several controls. One of them is initialized through VB
in frm_current. I am using the frm_beforeupdate event to prompt users for
saving changes. How can I make it so that the initialization of the control
does not dirty the form? I don't want to prompt the users for saving changes
unless he changed one of the remaing controls on the form. BTW, the control
I initialize is a foreign key and changes over time. Thank you. P
 
P said:
Hi,

I have a form with several controls. One of them is initialized through VB
in frm_current. I am using the frm_beforeupdate event to prompt users for
saving changes. How can I make it so that the initialization of the control
does not dirty the form? I don't want to prompt the users for saving changes
unless he changed one of the remaing controls on the form. BTW, the control
I initialize is a foreign key and changes over time. Thank you. P

You can't unless you can set the control's default value property instead. As
long as you set the Value property the record will be dirtied. You could also
wait until the BeforeUpdate event to make the assignment instead of using
Current.
 
Thank Rick for your suggestion. I set the control's default value property
in frm_load and that takes care of the issue. However, I have on the same
form a subform listing all current record for that foreign key. I run a
requery of the subform in frm_current. Although the combo box now shows the
default value, the subform no longer lists the associated records when the
main form is in New-Rec mode. It looks like the master field for the subform
does not longer hold the key value when in New-Rec mode although the combo
box shows the value. Any thoughts? Thank you. P
 
Right after you update the field in code, do a

me.Refresh

The above will force a disk writge, and thus dirty = false.
 
Thank you Albert. I tried me.refresh but it did not work.
The form is bound to a table with a two-field key (many-to-many table). I
set one part of the key (AccountKey) by setting its default value when I
load the form. The form also contains a subform which has its master field
set to the same key field (AccountKey). When I go to a new record (I use
docmd.GoToRecord acForm, frm.name, record:=acNewRec), I can see the value
set for AccountKey but the subform is empty until I save the record. Any
thoughts? P
 
I don't understand the problem that the sub-form is empty?

I mean, there is no need/reason to add sub-form records until the user
edits, or needs one?

Virtually 90% of all my quires are left joins. A left join means that
records will appear in reports EVEN if a child record is not present when
doing a join.

Also, when you state that you are moving to a new record, are you talking
about the main form, or the sub-form?

I also don't see why the fact that the sub-form is empty is a problem? You
should not be adding sub-records until the user actually edits the sub-form.
And, in fact, when the cursor moves to the sub-form, the main form is
written to disk anyway for you. (thus, you probably don't need my me.refresh
idea anyway).

Are you trying to add a sub-form/child record from code in the main form?
(and do you *really* need/want to do this?. As mentioned, there is NO REASON
to create these child record needlessly...is there?).

There is no question that you MUST FIRST write out the main record BEFORE
you attempt to add, or edit a child record. Your code that sets the default
key should do the trick here, and that should cause the main record to be
dirtied. At that point, you are free to add/edit child records...but
certainly no child records should exist until the main record is edited (and
further, as mentioned, NO CHILD records need to be added until the user
actually edits the sub-form).

In addtion, you are talking about a form-load event, and then you are
talking about a commadn to move to a new reocrd. When you move to a new
record, the form load event is not triggered. Perahps you need to add the
"set default" code to the buttion that moves to a new record?
 
Hum, in reading your original question, you problem is not one of "ditry",
but one of setting the default record id, and NOT triggering the before
update event. I kind of miss-read what your actual probelm is. (it is not
one of adding the child reocrd, but simply that the "before updat" even is
trigged.

I would suggest that you actaully add the reocrd first, and then load the
form "to" that record.

The code to "add" the new record could be:

Dim lngPos As Long

Me.RecordsetClone.AddNew
lngPos = YouNextIDFunction()
Me.RecordsetClone!ID = lngPos
Me.RecordsetClone.Update

Me.Bookmark = Me.RecordsetClone.LastModified

Me.Somefield.SetFocus


The above adds the record in code, and NOT via the form. You then "move" the
form to the record.
 
Back
Top