Link to home
Start Free TrialLog in
Avatar of ecohouse
ecohouse

asked on

Easy VB Script Question

I'm new to using VB Script and have a quick question.  I need to have a total field which gets it's information from two other fields.  So I need to do the following:

field1 * field 2 = field3

So my question is would I have to create a procedure to pass in the variables, and if so how do I do that?  Or can I just do that simple calculation in HTML?  If I can do it in HTML how would I do that?

I know VBA very well but I haven't dealt with VB script.  So I'm sure this is a pretty basic question.

Thanks.
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Assuming you are talking about a web page that runs on IIS and using ASP, you would use this

<% =field1 * field2 %>

Or

<% Response.Write field1 * field2 %>
Avatar of minichicken
minichicken

Hi ecohouse

Are you looking for the client-side VBSCRIPT??
Here's it....

************************************

<script language ="vbscript">
sub do_calc()
      dim field1, field2, field3
      
      field1 = document.form1.field1.value
      field2 = document.form1.field2.value
      field3 = cdbl(field1) + cdbl(field2) 'use cdbl() to conver to numeric type

      document.form1.field3.value = field3
end sub
</script>
<form name ="form1" method ="post" action"">
Field 1:<input type ="text" name="field1">+
Field 2:<input type ="text" name="field2">=
Field 3:<input type ="text" name="field3"><br>
<input type ="button" name="btn" value="calculate" onclick="do_calc()">
</form>
Avatar of ecohouse

ASKER

Thanks for the help.  I am looking for client side script.  

I need to do this multiple times on this webpage.  Is there a way to pass in values the way you do in a VBA procedure?  Also where would I place this code in the webpage?  And I would like it to automatically run after filling in the second field.
ASKER CERTIFIED SOLUTION
Avatar of minichicken
minichicken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again for the help.  This is being used on a submission form so the numbers are always changing.  Is there a way to get the values of fields 1 & 2 and pass them to the function?
This should do what you want, reply if you need more info

<script language ="javascript">

      function calcFields() {

            // get the values of the two fields
            // where formName is the name of the form <form action="" name="formName">
            // where fieldName is the name of the field <input type="text" name="fieldName">

            var num1 = document.formName.fieldName1.value;
            var num2 = document.formName.fieldName2.value;

            // do the calculation
            
            var result = num1 + num2;

            // do something with the calculated value
            alert(result);
      }
      
</script>
Thanks for my help.  But the problem is that I need to run this function about ten different times using different fields to get the calculation.  Would this still work, and if so what else would I have to do?

I thought that this would something pretty simple but I guess it's not so simple with web pages.
In the field where you pass it in use the following example:

<input type=text onblur = "calcFields(this.name);">

In the calcFields Function do this:

function calcFields(FieldName){

  if( FieldName = "MyField" ){  Var Field1 = document.formName.Field1.value
       Var Field2 = document.formName.Field2.value;
       Var result = Field1 * Field2;
       Document.formName.Field3.value = result
    }
\\For the next set do another if statement
}

The trick is figuring out what type of field you are putting this on and what events you can trigger off of to get the javascript to fire.  If you are doing this all at once, then put a single call to the script on a button that the user clicks.  Make the onclick event run the whole thing.

TekNik
So I would have to keep recreating this function for other fields?

 I'm an Access and VB programmer.  And if I did this using one of those programs I would just pass in the fields using a procedure.  There is no way to just keep using the same function and pass in the different values each time it's used?
It's the same thing.  You set the event to trigger the script, like onclick.  In VB we use OnLoad to trigger certain events, it's the same thing with web boxes, just named differently.  Each time the event fires it sends the call the to Javascript Function to do it's thing.  If you have 10 textfields that you want to process each time the person leaves the field, just put the onblur="myFunction(this.name);" bit in the definition of the web field.  Then each time the user leaves the field, creating the onblur event, the javascript function fires and does the calculation.    In VB it would be the same as setting the LostFocus event for a field to trigger a calculation to occur.  

I am assuming that you use VBA to do your access programming.  If not, then this all makes no sense.  
I understand what you are saying in the VB sense.   I don't know java at all.  The way this page will be set up is the following:

Price1
Quantity1
Total1

Price2
Quantity2
Total2
etc.

So you are saying that this java script will be able to use the appropriate fields each time it fires?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again for all the help.  As I've stated I've never dealt with programming web pages.

I would like the function to run after each quantity is entered each time. So I have to create an additional IF statement for each set of fields?

I haven't had time to play with this yet.  I'll get back when I try it to see how it goes.
No problem. This should do as you've asked.

<head>
<script language ="javascript">

     function calc(obj1, obj2) {
          // do the calculation
          return(obj1.value + obj2.value);
     }    


     function doCalc(obj) {
            // determine which addition we are handling
            var x = obj.name;      // returns eg. a_1
            var n = x.split('_')      // returns array with a and 1
            
            // get all the objects related to this addition
            // ie. a_(n), b_(n) and c_(n) and our array n[1] holds the number
            obj1 = document.getElementById("a_" + n[1]);
            obj2 = document.getElementById("b_" + n[1]);
            obj3 = document.getElementById("c_" + n[1]);

            // ensure the left and right of the equation are not blank
            if(obj1 <> '' && obj2 <> '') {
                  // calculate the value and set the field value
                  obj3.value = calc(obj1, obj2);
            }
      }
</script>
</head>
<body>

<input type="text" name="a_1" onChange="doCalc(this)"> + <input type="text" name="b_1" onChange="doCalc(this)"> = <input type="text" name="c_1" readonly>

</body>