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.
ecohouseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
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 %>
0
minichickenCommented:
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>
0
ecohouseAuthor Commented:
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.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

minichickenCommented:
Hi

If you would like to do client-side scripts, i recommend you use JAVASCRIPT rather then VBSCRIPT as vbscript is only supported on IE and not the other browser, where javascript is supported on all browsers. And also for some reason vbscript don't accept more than one parameter when using sub. So JAVASCRIPT is the way forward. It script should be in the <head> section.

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

<HTML>
<HEAD>

<script language ="javascript">

function do_calc(field1, field2)
{
      var field3 = field1 + field2; //do calculation
      document.write (field3); //output result
      document.write ("<br>"); //new line      
}

do_calc(2,3); //call function where field1 = 2 and field2 = 3

do_calc(1.5,2.6); //call function where field1 = 1.5 and field2 = 2.6

do_calc(10.4,25.9); //call function where field1 = 10.4 and field2 = 25.9

</script>

</HEAD>
<BODY>

</BODY>
</HTML>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ecohouseAuthor Commented:
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?
0
weddellCommented:
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>
0
ecohouseAuthor Commented:
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.
0
mcdown75Commented:
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
0
ecohouseAuthor Commented:
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?
0
mcdown75Commented:
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.  
0
ecohouseAuthor Commented:
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?
0
mcdown75Commented:
First, this is JavaScripting, not Java.  Big difference.  OK, when do you want the calculation to occur?  When they enter Quantity?  If so then you make the onblur event of Quantity1 and Quantity2 send the call to the javascript function.  myFunction() is the call to the javascript function.  The stuff inside the parens (this.name) are the variables that get passed.  Essentially what you have set up inside the javascript function is a series of if statements that do the correct action for the sending field.  If fieldname=Quanitity1 then{ do this;}  If fieldName=Quantity2 then {do this;}

For a quick primer on Javascript functions take a look at WebMonkey.com.  There is a section called Thau's Javascript Tutorial that is excellent for explaining what is going on.  I learned a big chunk of Javascript in one day from that site.  Javascripting is a client side operation that can be very useful.  Just remember, it is not ADA compliant, and users can turn off the Javascript functionality if they are paranoid about that kind of thing.

The other option that you have is to have them submit the form, do the calculations in the DB you are using, then redisplay the page.  90% of the time Javascript will work just fine though.

TekNik
0
weddellCommented:
The function below (calc) takes in two field objects, adds their values and returns the calculated result. The function run1() sends the alerts the value and sets the two fields as parameters. Any text field can be sent to the function and it will work.

<script language ="javascript">

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

      function run1() {
            var fld1 = document.formName.fieldName1;
            var fld2 = document.formName.fieldName2;
            alert(num1, num2);
      }

</script>

You should possibly mention more detail on how you want to implement the scripts so it can be written to your specific needs... and yes - your right - it is a very easy script.
0
ecohouseAuthor Commented:
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.
0
weddellCommented:
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>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.