www.Inmagic.com    Inmagic Forums    Inmagic Forums  Hop To Forum Categories  Scripting    Script - If Query for comparison of 2 fields
Go
New
Find
Notify
Tools
Reply
  
-star Rating Rate It!  Login/Join 
Posted
I have 2 fields which need to be compared, loss1 and loss2. Using the IF statement I would like to determine which field holds the minimum value (both fields are defined calculation fields). Then I would populate the minimum value amount in a blank number field named minTotal (which is a blank number field).

I have tried both JScript and VBScript and neither will populate the minTotal value with the less then amount.

Below is the script I started. I would really appreciate assistance.

function onRecordSave()
{
var TLoss1 = Form.boxes ("Loss1)
var TLoss2 = Form.boxes ("Loss2)
}
If TLoss1 > TLoss2
Form.boxes("MinLoss").content = Form.boxes ("TLoss2").content

var temp = Form.boxes("BotLoss").content
If Form.boxes("TopLoss").content > Form.boxes("BotLoss").content;
Form.boxes("MinLoss").content = temp;

This message has been edited. Last edited by: Lisa,
 
Posts: 5 | Location: Houston, Tx USA | Registered: Tue September 30 2003Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
Hi

I usually write in VB and am more comfortable with it! But I can see a couple of errors in your jscript, that would prevent it working:

You have

function onRecordSave()
{
var TLoss1 = Form.boxes ("Loss1)
var TLoss2 = Form.boxes ("Loss2)
}

but the } above should be at the end of the script

("Loss1) should be ("Loss1")(and also Loss2)

You have not been consistent with ending lines with a ;

In this line

Form.boxes("MinLoss").content = Form.boxes ("TLoss2").content

I think TLoss2 should be Loss2

Have you checked the Names section of the Script Window to be sure what the boxes are actually called? There seem to be too many different names in your script?

I hope these tips will help

Norma


Maxus Australia
Authorised Inmagic Partner
 
Posts: 56 | Location: Melbourne, Australia | Registered: Thu July 17 2003Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
Norma, thank you for your assistance. I have not worked with DBText scripting for several years.

I am still attempting to identify coding errors and have switched to VBScript with no results also.

The final product is to identify which 2 loss fields are the smallest and populate the field MinLoss with the smallest result.

I have checked my field name in the Script Names. Also I added a script button to the form to insert the value when clicked. I named the script name to "button1". I still cannot get this to work.

Sub button1_onClick()
temp1 = Form.boxes("Loss1").content
temp2 = Form.boxes("Loss2").content
If temp1 > temp2 Then
Form.boxes("MinLoss").content = temp2
End Sub

Laurie
Fulbright & Jaworski L.L.P.
Houston, Texas
 
Posts: 5 | Location: Houston, Tx USA | Registered: Tue September 30 2003Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
Hi,

I think you are comparing strings instead of numbers. You must explicitly convert each box content value to a number type, then compare number values. For example, if you compare the strings "2" and "10", you will find that "2" is greater than "10", whereas the integer 2 compared to the integer 10 is less. Javascript is a loosely-typed language, which means you are not forced to pick a type when declaring a variable, but that ease-of-use can also lead to pitfalls.

You haven't mentioned whether the numbers you are expecting to find as box content values are integers (whole numbers) or decimals (floats). I'll assume decimal numbers for this demo. So:

 
function button1_onClick() {
var strLoss1 = Form.boxes("Loss1").content;
var strLoss2 = Form.boxes("Loss2").content;
var loss1 = parseFloat(strLoss1);
var loss2 = parseFloat(strLoss2);

Form.boxes("MinLoss").content = loss1 < loss2 ? loss1 : loss2;
}


Peter Tyrrell, MLIS
Director and Lead Developer
Andornot Consulting Inc.
http://andornot.com/about/developerblog
http://twitter.com/andornot
 
Posts: 188 | Location: Victoria, BC, Canada | Registered: Thu September 20 2001Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
Peter, I am sorry that I did not get back to you sooner, but we multi-task quite a bit. I want to thank you very much for the script that you supplied, it works perfect.

Thank you again.

I also want to think Norma for her input.


Laurie Phillips, Sr. Database Analyst
FULBRIGHT & Jaworski L.L.P.
Houston, Texas
 
Posts: 5 | Location: Houston, Tx USA | Registered: Tue September 30 2003Reply With QuoteEdit or Delete MessageReport This Post
  Powered by Eve Community  
 

www.Inmagic.com    Inmagic Forums    Inmagic Forums  Hop To Forum Categories  Scripting    Script - If Query for comparison of 2 fields