Check Boxes That Show a Running Total
Here's the scenario: I receive a payment from my customer that will cover multiple invoices. Therefore, I'd like to go to a browse that shows all the open invoices for that customer, and mark the ones to be paid using check boxes. I'd also like to see the total of all marked invoices as soon as I've checked them, even if I haven't saved the record (I might check one, decide it's not right, and uncheck it).
1 Actually, I put script_play("ar_pay") in the field rule event, and then created the global script shown above. This way, I can edit the script more easily without having to re-open the field rule editor.
by Bill Warner
Well, this was a little trickier than I first thought. I tried putting a calc field on the form that would give me the running total using DBSUM(). This function usually works quite well, because it will update as soon as the record is saved. However, it's value would not change until I saved the record, so I had to check record and move to another one to see the result. This was not what I wanted (it's rather disconcerting to click on one record after another and see the field change, when it's actually showing the result of the last click, not the current one).
After posting a message on the board and getting some suggestions (thanks to all of you that replied), I decided to try doing what I wanted with a variable. The trick was to get the variable to show the result immediately. After much trial and error, I came up with a method that works quite well.
First, I created a simple script to run in the OnChangeRecord record event in the field rules for this set:1
dim global pylst as c
if pylst = ""
end
end if
sys_send_keys("{f9}")
amtbal = parentform:browse1:amtbal.value
py = parentform:browse1:pay.value
if py = .f.
pysum = pysum + amtbal
else
pysum = pysum - amtbal
end if
parentform.Resynch()
The variable pylst is set to a non-blank value when the user goes to the form. When leaving the form, pylst is set to blank again. Therefore, this script will only run for the correct form. This is necessary because there are other forms based on the same set, and I don't want to save the record every time I change a field value in the other forms.
Then I use sys_send_keys("{f9}") to save the record as soon as it is changed. This allows me to see the value right after I click the check box.
The next few lines allow pysum to sum up the amount from the AmtBal field, so it shows on the form. Note that the logic seems to be backwards - if Py (the check box) is false, the amount is added. This is because Alpha retrieves the value of Py before it is changed.
Finally, I put the browse with the check boxes on a form (as an embedded browse), so I could add various buttons and other fields. I placed the global variable pysum on the form, so it would show the results of the script.
The final result is a browse in which I can check a box and see the results right away - no need to save the record (since the script is saving it) - exactly what I wanted!!