onChange / OnDirty etc (2 Viewers)

Geordie2008

Registered User.
Local time
Today, 18:19
Joined
Mar 25, 2008
Messages
177
Hi all, Im not sure which "on" event I want to use....

Everyime a user updates a field on the form I want the form to loop through on of my macros....

so everytime a change is made I want the code to execute..... I didn;t want to have to put an "On Change" on every individual text and combobox. Is there a way to apply it to the change of any control on the form?

Cheers,
Mand.:D
 

Geordie2008

Registered User.
Local time
Today, 18:19
Joined
Mar 25, 2008
Messages
177
loops through each field and assesses if the data within the cell is valid. If yes, the cell background is white, else red. When the user updates a red cell with a valid selection, I want the cell to go white, so I need to re-loop the criteria.....

Thanks,
M
 

KenHigg

Registered User
Local time
Today, 13:19
Joined
Jun 9, 2004
Messages
13,327
This is not a very efficient way to validate data. I suggest either validate each field as the user exits the control or before they save the record. It's kind of like you doing a hybrid of these two...
 

Geordie2008

Registered User.
Local time
Today, 18:19
Joined
Mar 25, 2008
Messages
177
I already do a further check on close. It wont let them close a form if the data is not a valid entry....

I just want the cells to be coloured for problem areas and then go white when they are resolved...... Is this poss?
 

KenHigg

Registered User
Local time
Today, 13:19
Joined
Jun 9, 2004
Messages
13,327
I not sure you can using macros but you certainly can with VBA.
 

boblarson

Smeghead
Local time
Today, 10:19
Joined
Jan 12, 2001
Messages
32,059
I already do a further check on close. It wont let them close a form if the data is not a valid entry....

I just want the cells to be coloured for problem areas and then go white when they are resolved...... Is this poss?

Use conditional formatting.
 

Geordie2008

Registered User.
Local time
Today, 18:19
Joined
Mar 25, 2008
Messages
177
ah, Im speaking in excel terms again, sorry. When I said Macro I meant VBA code.... I have the code, it works fine, I just need it to be run everytime something changes amd I dont know on which "event" I need to out it...

Thanks,
M
 

KenHigg

Registered User
Local time
Today, 13:19
Joined
Jun 9, 2004
Messages
13,327
I'm not sure there is a form level event that fires when a control like a text box is exited or the data is changed. I'm pretty sure you're going to have to either call the code when the user exits or changes each control or before the user saves the data in the entire form.
 

Geordie2008

Registered User.
Local time
Today, 18:19
Joined
Mar 25, 2008
Messages
177
Mmmmm..... was hoping to avoid all of that pointless code.... will have a think about if its really required......

Thanks anyway.
M
 

KenHigg

Registered User
Local time
Today, 13:19
Joined
Jun 9, 2004
Messages
13,327
What you could do is build a stand alone routine in the form module that does the checks and call it from an event in each control. I think I'd just run it in the forms before update event.
 

boblarson

Smeghead
Local time
Today, 10:19
Joined
Jan 12, 2001
Messages
32,059
Why not conditional formatting?

And conditional formatting won't work why???? :confused:
 

missinglinq

AWF VIP
Local time
Today, 13:19
Joined
Jun 20, 2003
Messages
6,420
Checking whether a required field contains data or is empty, on the form level, has to be done the in Form_BeforeUpdate event, otherwise the user could simply not enter the textbox and no flag would be raised.

Checking the validity of entered data in a control can be done either in the control's or the form's BeforeUpdate event. Placing validity code in the OnExit or LostFocus event is inefficient, because it'll run the check every time the user tabs thru the given control , whether data is entered/edited or not.

There is a form level event that fires when a control has data entered or data is changed, the Form_Dirty event. The problem with using this event is that it only fires once. If TextboxA is changed, Form_Dirty fires. If TextboxB is then changed, Form_Dirty doesn't fire again. So the validation code placed here wouldn't run the second time a control was altered.

Writing a public function to check all textboxes to insure that each one has data (is not Null) would be worthwhile, especially if you had multiple forms you wanted to do the same with. But if by checking whether the entries are valid you mean does the SSN field have 9 digits and no alpha characters, does the date field have a date that is in the future, is the percent field equal to or less than 100% etc., writing a public function to handle this becomes another matter entirely, and probably pointless, since it only applies to the one form. If it only applies to a single form, place it in that form.

As Geordie has already figured out, to insure that something that concerns a certain control is done each time that control is changed, you have to have individual code for that control tied to that control, or check that control in the Form_BeforeUpdate event.

Bob's suggestion on Conditional Formatting is a good one if all you want to do is mark empty fields or fields where the data doesn't meet certain criteria by changing the background color. But you cannot use this, of course, if you actually need to prevent the record from being saved unless the data is entered and correct.

Conditional Formatting also has the advantage of working in all form views, while changing colors in VBA code, in a Datasheet or Continuous view form, will format the control in all records according to the value on the current record.

Using VBA code to just change the color of a given control in a given record also requires placing code in the Form_Current event in order for the current record to be formatted correctly. Using Conditional Formatting also avoids having to do this.
 

Users who are viewing this thread

Top Bottom