The Forums on slxdeveloper.com are now retired. The forum archive will remain available for the time being. Thank you for your participation on slxdeveloper.com!
|
|
Making a Check Box checked by default, when connected to a null data record
Posted: 11 Aug 06 11:04 AM
|
fiogf49gjkf0d I have a check box (multiple actually) that enable or disable a calculation. By default I want these boxes checked. However, when I go to a new record and pull up the form they are unchecked making all my calculations zero.
I've tried a few different methods to force the box to be checked, including looking for a null value and setting: .State = cbChecked (didn't work) .Text = "T" (didn't work)
I'm guessing I'm missing something stupid.
My other option is to make the check boxes "Disable" and changing my statements to calculate on False instead of True.
Thoughts?
Marc |
|
|
| |
|
Re: Making a Check Box checked by default, when connected to a null data record
Posted: 11 Aug 06 1:45 PM
|
fiogf49gjkf0d Your checkbox method should be in the OnChange event of the form so that it fires every time the record changes.
I use the .Checked property only because I find traditional bools (true/false) easier to deal with than "T", "F" or sometimes NULL with the SLX Boolean field.
If you have multiple checkboxes and you're looking for a way to simplify checking them all, you could place them all on a panel then loop through the Form.Controls collection to only check the checkboxes who's Parent.Name = "panelName". 2 checkboxes wouldn't benefit from this technique much but anything over 5 and this is how I do it. |
|
|
|
Re: Making a Check Box checked by default, when connected to a null data record
Posted: 11 Aug 06 1:51 PM
|
fiogf49gjkf0d I've ended up switching my logic so the check box disables the control rather than enables it. This isn't the solution I wanted but it works. The suggestions given above work fine, the problem is that they get reset back to unchecked somewhere in my loop. I've tried to figure it out but I suspect it has a lot more to do with my logic flow than my code.
I'm caulking this up to my novice developer level. I'll email the code to anyone who wants to look at it but I honestly wouldn't expect any of you guys to optimise it for me.
Marc A. Johnson Network Administrator Optimum Solutions, Inc. 615-329-2313 x2240 office 615-329-4448 fax mjohnson@optimum-solutions.com www.optimum-solutions.com Your only payroll, HR and time & attendance solution. |
|
|
|
Re: Making a Check Box checked by default, when connected to a null data record
Posted: 11 Aug 06 2:04 PM
|
fiogf49gjkf0d I can look at it if you'd like or you could post just the method here if it's not too huge (unless of course you don't want everyone to see it, which I understand completely). I've never had that specific problem before but I have had code not behave as expected. I'm way too stubborn to work around an issue like that because most of the time the problem is something I overlook. |
|
|
| |
|
Re: Making a Check Box checked by default, when connected to a null data record
Posted: 14 Aug 06 1:13 PM
|
fiogf49gjkf0d Even more interesting CheckBox issues. The following sub routine is called, onClick:
Sub cbPRClick(Sender) if cbPR.Text = "F" then CalculatePR else txtPayroll.Text = 0 end if End Sub
The problem is that this event is firing when the form is opened, without any click. I can only assume that it's working similar to onChange when the data is loaded into the checkbox from the database. The check box is bound to a field (Boolian).
Why would this event fire without a click and is there any way to prevent this?
Marc |
|
|
| |
|
Re: Making a Check Box checked by default, when connected to a null data record
Posted: 14 Aug 06 1:33 PM
|
fiogf49gjkf0d As far as I can tell, (someone please correct me if I am wrong), but OnClick would be similar to OnChange - pretty much as you describe. If Record A has the Boolean field as "T" and then you move to another record, as the checkbox goes to Record B whose Boolean field is "F" then the checkbox "CLICKS" itself to false.
Instead of cbPRClick, you could try cbPRMouseUp as that is actually a user clicking something.
Also consider unbinding the checkbox, throwing a textbox that is bound to your Boolean field and basing your cbPR.checked property on the txtKludge.text value. It is ugly, cumbersome and can compete for top honors in a Kludge competion, but it does work. Now if you have a ton of checkboxes, then you'd better get to work... |
|
|
| |
|
Re: Making a Check Box checked by default, when connected to a null data record
Posted: 14 Aug 06 6:46 PM
|
fiogf49gjkf0d Jim's right in that the checkbox's OnClick behaves EXACTLY like a editboxes OnChange event.
This means that when the FORM's OnChange event fires and proceeds to change cbPR.Text from the default NULL to (database value), cbPRClick fires. This happens with EVERY CONTROL that has an OnChange event and some events behave as an OnChange event (like checkbox's OnClick).
To me it sounds like you're looking for an event that fires with your Form's OnChange event that will set everything up, then events that fire during the editing process that will make calculations on the fly. If this is the case you'll want the on-the-fly calculations to be done as little as possible because OnChange events that affect another control's Text property will trigger that control's OnChange event, causing a cascading effect that may skew the end result.
If for instance you do the calculation on a button press you can perform validation before the form closes to make sure users pressed that button. Here's rough code that'd do that (won't compile because I'm too lazy to look up sub/function exacts):
dim booleanIsCalculated
AXFormChange(Sender) booleanIsCalculated = false End
AXFormValidate(Sender) if (not (booleanIsCalculated)) then MsgBox "You must click Calculate before closing" AXFormValidate = false end if End
cbPRClick(Sender) booleanIsCalculated = true End [Repeat for each control in the calculation]
CalculateClick(Sender) booleanIsCalculated = false End
Your events are each control's OnChange, the form's OnChange and OnValidate routines, and your buttons OnClick event. The booleanIsCalculated variable is declared outside of any method so that it can be manipulated by each method in the form. Setting AXFormValidate to false (it may be true, I forget exactly) prevents the form from closing so your user will be stuck there until they click calculate. You could be nice and instead of calling a msgbox just call CalculateClick(Sender) but I like to let my users know when they're doing something wrong. Annoy them enough and they'll do it right eventually. |
|
|
|
Re: Making a Check Box checked by default, when connected to a null data record
Posted: 14 Aug 06 7:17 PM
|
fiogf49gjkf0d The problem is all caused by the fact that the checkbox's "click" event fires whether the user clicked it or it was set by the binding engine. If the checkbox is bound, then as the control is populated on the OnChange event of the form, it's click event is fired. The same goes for setting the checkbox programatically.
In 6.2.3 there were some new form properties added so you can now monitor what is happening at the time the event occurs.
IsReading - means the data binding system is currently "active" IsValidating - validate event is currently being evaluated/run IsWriting - data is being saved to the DB Modified - an edit has been made to the value of a data bound control
So, if you want to be sure that the click event was triggered by the user clicking the checkbox, and not that the event is just being raised it being set by the binding engine, then you could just do this:
Sub cbPRClick(Sender) If Not IsReading ' do something here MsgBox "The user clicked the checkbox" End If End Sub
This exact scenario is why these form properties were added in SP3. Makes for much cleaner code IMO. |
|
|
| |
|