?
Solved

Easy VB Script Question

Posted on 2004-11-13
17
Medium Priority
?
228 Views
Last Modified: 2010-04-06
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.
0
Comment
Question by:ecohouse
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 12574437
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
 
LVL 12

Expert Comment

by:minichicken
ID: 12575046
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
 

Author Comment

by:ecohouse
ID: 12575955
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
minichicken earned 136 total points
ID: 12576185
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
 

Author Comment

by:ecohouse
ID: 12578228
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
 
LVL 3

Expert Comment

by:weddell
ID: 12581436
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
 

Author Comment

by:ecohouse
ID: 12585120
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
 
LVL 2

Expert Comment

by:mcdown75
ID: 12585279
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
 

Author Comment

by:ecohouse
ID: 12585427
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
 
LVL 2

Expert Comment

by:mcdown75
ID: 12586308
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
 

Author Comment

by:ecohouse
ID: 12586745
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
 
LVL 2

Assisted Solution

by:mcdown75
mcdown75 earned 132 total points
ID: 12588080
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
 
LVL 3

Assisted Solution

by:weddell
weddell earned 132 total points
ID: 12588199
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
 

Author Comment

by:ecohouse
ID: 12589282
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
 
LVL 3

Expert Comment

by:weddell
ID: 12589382
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question