Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

VBScript Problem - Increment variableHelpful Member! 

lennitw (Programmer) (OP)
13 Jan 06 15:05
Below is code I use to populate some text boxes.  The problem is that when the user presses a button, a new row of text boxes appears and I need this code to point to them and leave the data in the previous row.  For example, the way it is coded it will only work with the text box (document.frmMS47.SpaceTypeTask2) but if a new row is added I need it to work with (document.frmMS47.SpaceTypeTask3).

Ideas?

=============

<script language=VBScript>
    sub window_onLoad
        PopulateTextbox
    end sub

    sub PopulateTextbox()
        dim x, scX, space_comp, taskid, minper, spacetype, newtime, checktime
                  x = 0
                  y = 2
                  scX = 0
                  space_comp = ""
                  taskid = ""
                  minper = 0
                  

                do until x > LEN(document.frmMS47.SpaceTypeTask2.value)
                    if right(left(document.frmMS47.SpaceTypeTask2.value,x),1) = "-"  AND space_comp = "" then
                        space_comp = left(document.frmMS47.SpaceTypeTask2.value,x-1)
                        scX = x
                        x = x + 1
                    end if
                    if right(left(document.frmMS47.SpaceTypeTask2.value,x),1) = "-" AND taskid = "" then
                        taskid = right((left(document.frmMS47.SpaceTypeTask2.value,(x-1))),x-scx-1)
                        xx = x
                        minper = right(document.frmMS47.SpaceTypeTask2.value, LEN(document.frmMS47.SpaceTypeTask2.value) - x)
                        x = LEN(document.frmMS47.SpaceTypeTask2.value)
                    end if
                    
                    x = x + 1
                loop                  

                document.frmMS47.TimePerSqFt2.value =  minper
                
                checktime = document.frmMS47.endtime0.value

                myDec = InStr(1, CStr(minper * document.frmMS47.sqFT_Fix2.value), ".", vbTextCompare)
                    If myDec > 0 Then
                        roundUp = CDbl(Left(CStr(minper * document.frmMS47.sqFT_Fix2.value), myDec)) + 1
                    Else
                        roundUp = minper * document.frmMS47.sqFT_Fix2.value
                    End If

                newtime = (DATEADD("n",((roundUp)),checktime))        
                document.frmMS47.endTime2.value =  formatdatetime(newtime,4)
                
                document.frmMS47.startTime2.value =  formatdatetime(checktime,4)
                
    end sub
</script>
Helpful Member!  trollacious (Programmer)
13 Jan 06 19:49
If you use the following format to access form elements, you may have an easier time doing what you want:

CODE

Dim varnum

varnum = 2
'refers to element SpaceTypeTask2
document.frmMS47.elements["SpaceTypeTask" & varnum].value

varnum = varnum + 1
'now refers to element SpaceTypeTask3
document.frmMS47.elements["SpaceTypeTask" & varnum].value
Lee
lennitw (Programmer) (OP)
17 Jan 06 7:44
Thanks for the response Lee, but when I make the changes, I get an error "Object Expected".  Here is the code as I changed it:

=========
    sub PopulateTextbox()
        dim x, scX, space_comp, taskid, minper, spacetype, newtime, checktime, varnum
                  x = 0
                  varnum = 2
                  scX = 0
                  space_comp = ""
                  taskid = ""
                  minper = 0
                  

                do until x > LEN(document.frmMS47.elements["SpaceTypeTask" & varnum].value)
                    if right(left(document.frmMS47.elements["SpaceTypeTask" & varnum].value,x),1) = "-"  AND space_comp = "" then
                        space_comp = left(document.frmMS47.elements["SpaceTypeTask" & varnum].value,x-1)
                        scX = x
                        x = x + 1
                    end if
                    if right(left(document.frmMS47.elements["SpaceTypeTask" & varnum].value,x),1) = "-" AND taskid = "" then
                        taskid = right((left(document.frmMS47.elements["SpaceTypeTask" & varnum].value,(x-1))),x-scx-1)
                        xx = x
                        minper = right(document.frmMS47.elements["SpaceTypeTask" & varnum].value, LEN(document.frmMS47.elements["SpaceTypeTask" & varnum].value) - x)
                        x = LEN(document.frmMS47.elements["SpaceTypeTask" & varnum].value)
                    end if
                    
                    x = x + 1
                loop                  

                document.frmMS47.elements["TimePerSqFt" & varnum].value =  minper
                
                checktime = document.frmMS47.endtime0.value

                myDec = InStr(1, CStr(minper * document.frmMS47.elements["sqFT_Fix" & varnum].value), ".", vbTextCompare)
                    If myDec > 0 Then
                        roundUp = CDbl(Left(CStr(minper * document.frmMS47.elements["sqFT_Fix" & varnum].value), myDec)) + 1
                    Else
                        roundUp = minper * document.frmMS47.elements["sqFT_Fix" & varnum].value
                    End If

                newtime = (DATEADD("n",((roundUp)),checktime))        
                document.frmMS47.elements["endTime" & varnum].value =  formatdatetime(newtime,4)
                
                document.frmMS47.elements["startTime" & varnum].value =  formatdatetime(checktime,4)
                
    end sub
</script>
tsuji (TechnicalUser)
17 Jan 06 10:27
Have to fallback more thoroughly on vbs syntax.
>document.frmMS47.elements["SpaceTypeTask" & varnum]
document.frmMS47.elements("SpaceTypeTask" & varnum)
trollacious (Programmer)
17 Jan 06 10:39
My fault.  I'm so used to doing this in Javascript that I used the array notation from that language instead of the parentheses from VBScript.

Lee
tsuji (TechnicalUser)
17 Jan 06 10:42
That's understood. Me, the same, Lee!
lennitw (Programmer) (OP)
17 Jan 06 14:07
Hey guys...thanks a million for the help.  That trick did work, but I still have one stumbling block that I thought I could figure out once this last hurdle was jumped.  What I need to do now is when the user fills out the for and clicks "Add Item" the form refreshes with the all previous data entered and a new blank row.  The problem I have with this script below is when you add a new row the times in the previous row disappear.

Thanks again!

================================

<!--#include file="include/appsettings.asp"-->

<%
'### Open Connection to DB
Set db = Server.CreateObject("ADODB.Connection")
db.Open ms47DSN   

cntX = request.form("cntX")
if cntX = "" then
    cntX = 2
else
    cntX = cntX + 1
end if

if request("additem") = "Add Item" then

elseif request("saveForm") = "Save" then
    response.write "SAVE DATA"
end if


%>
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>MS47</title>


<script language=VBScript>
    sub window_onLoad
        'PopulateTextbox
    end sub





    sub PopulateTextbox()
        dim x, scX, space_comp, taskid, minper, spacetype, newtime, checktime, varnum
                  x = 0
                  varnum = document.frmMS47.elements("cntY").value - 1
                  scX = 0
                  space_comp = ""
                  taskid = ""
                  minper = 0
                  

                do until x > LEN(document.frmMS47.elements("SpaceTypeTask" & varnum).value)
                    if right(left(document.frmMS47.elements("SpaceTypeTask" & varnum).value,x),1) = "-"  AND space_comp = "" then
                        space_comp = left(document.frmMS47.elements("SpaceTypeTask" & varnum).value,x-1)
                        scX = x
                        x = x + 1
                    end if
                    if right(left(document.frmMS47.elements("SpaceTypeTask" & varnum).value,x),1) = "-" AND taskid = "" then
                        taskid = right((left(document.frmMS47.elements("SpaceTypeTask" & varnum).value,(x-1))),x-scx-1)
                        xx = x
                        minper = right(document.frmMS47.elements("SpaceTypeTask" & varnum).value, LEN(document.frmMS47.elements("SpaceTypeTask" & varnum).value) - x)
                        x = LEN(document.frmMS47.elements("SpaceTypeTask" & varnum).value)
                    end if
                    
                    x = x + 1
                loop                  

                document.frmMS47.elements("TimePerSqFt" & varnum).value =  minper
                
                checktime = document.frmMS47.endtime0.value

                myDec = InStr(1, CStr(minper * document.frmMS47.elements("sqFT_Fix" & varnum).value), ".", vbTextCompare)
                    If myDec > 0 Then
                        roundUp = CDbl(Left(CStr(minper * document.frmMS47.elements("sqFT_Fix" & varnum).value), myDec)) + 1
                    Else
                        roundUp = minper * document.frmMS47.elements("sqFT_Fix" & varnum).value
                    End If

                newtime = (DATEADD("n",((roundUp)),checktime))        
                document.frmMS47.elements("endTime" & varnum).value =  formatdatetime(newtime,4)
                
                document.frmMS47.elements("startTime" & varnum).value =  formatdatetime(checktime,4)
                
    end sub
</script>

  <script>
   function PopUp(PopUpUrl){
    var ScreenWidth=window.screen.width;
    var ScreenHeight=window.screen.height;
    var movefromedge=0;
    placementx=300;
    placementy=100;
    WinPop=window.open(PopUpUrl,"","width=400,height=400,toolbar=0,location=0,directories=0,status=0,scrollbars=1,menubar=0,resizable=0,left="+placementx+",top="+placementy+",screenX="+placementx+",screenY="+placementy+",");
   }
  </script>


</head>

<body>
<form name=frmMS47 action="ms47form_test.asp" method=POST>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="775" id="AutoNumber1">
  <tr>
    <td colspan="2"><b><font size="2">U.S. POSTAL SERVICE</font></b></td>
    <td colspan="12" align="center">
    <p align="center"><b><font size="2">Identification</font></b></td>
  </tr>
  <tr>
    <td colspan="2" rowspan="2"><b>Preventive-Custodial Maintenance Route</b></td>
    <td align="center" colspan="2"><b><font size="2">WC</font></b></td>
    <td align="center" colspan="7"><b><font size="2">Equipment Acronym</font></b></td>
    <td align="center" colspan="2"><b><font size="2">Class</font></b></td>
    <td align="center"><b><font size="2">Number</font></b></td>
  </tr>
  <tr>
    <td align="center" colspan="2">&nbsp;</td>
    <td align="center" colspan="7">&nbsp;</td>
    <td align="center" colspan="2">&nbsp;</td>
    <td align="center">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><font size="1">Mail Processing-Building Equipment-Building
    Service / Component or System</font></td>
    <td align="center" colspan="4"><b><font size="2">Original Issuance Date</font></b></td>
    <td align="center" colspan="5"><b><font size="2">Date Last Revised</font></b></td>
    <td align="center" colspan="2"><b><font size="2">MMO No.</font></b></td>
    <td align="center"><b><font size="2">Est. Time (Hr &amp; ths.)</font></b></td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
    <td align="center" colspan="4">&nbsp;</td>
    <td align="center" colspan="5">&nbsp;</td>
    <td align="center" colspan="2">&nbsp;</td>
    <td align="center">&nbsp;</td>
  </tr>
  <tr>
    <td>
    <p align="center"><b><font size="2">Building &amp; Location</font></b></td>
    <td>
    <p align="center"><b><font size="2">START TIME</font></b></td>
    <td colspan="10" align="center">
    <p align="center"><b><font size="2">Frequency</font></b></td>
    <td align="center"><b><font size="2">Tour</font></b></td>
    <td align="center"><b><font size="2">Basic Work Week</font></b></td>
  </tr>
  <tr>
    <td rowspan="2">&nbsp;</td>
    <td rowspan="2">&nbsp;</td>
    <td align="center"><b><font size="2">T</font></b></td>
    <td align="center"><b><font size="2">D</font></b></td>
    <td align="center"><b><font size="2">W</font></b></td>
    <td align="center"><b><font size="2">BW</font></b></td>
    <td align="center"><b><font size="2">M</font></b></td>
    <td align="center"><b><font size="2">BM</font></b></td>
    <td align="center"><b><font size="2">Q</font></b></td>
    <td align="center"><b><font size="2">SA</font></b></td>
    <td align="center"><b><font size="2">A</font></b></td>
    <td align="center"><b><font size="2">BA</font></b></td>
    <td rowspan="2" align="center">&nbsp;</td>
    <td rowspan="2" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
  </tr>
</table>





<table border="1" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="775" id="AutoNumber2">
  <tr>
    <td width="125" colspan="2">
    <b>Custodian Name:</b></td>
    <td width="643" colspan="7" align="center">
      <p align="left">
      <select size="1" name="CustName">
      <option selected>-- Select Custodian --</option>
      </select></td>
  </tr>
  <tr>
    <td width="771" colspan="9"></td>
  </tr>
  <tr>
    <td width="50" rowspan="2">
    <p align="center"><b><font size="2">Item<br>#</font></b></td>
    <td width="72" rowspan="2">
    <p align="center"><b><font size="2">Equipment<br>Building<br>Identification</font></b></td>
    <td width="50" rowspan="2">
    <p align="center"><b><font size="2">Space<br>Type &amp; Task</font></b></td>
    <td width="110" colspan="2" align="center"><b><font size="2">CALCULATIONS</font></b></td>
    <td width="151" colspan="2" align="center"><b><font size="2">TIME</font></b></td>
    <td width="53" rowspan="2">
    <p align="center"><b><font size="2">Check<br>List #</font></b></td>
    <td width="267" rowspan="2">
    <p align="center"><b><font size="2">INSTRUCTIONS are Reminders<br>REFER TO MASTER CHECKLIST </font></b></td>
  </tr>
  <tr>
    <td width="50" align="center"><b><font size="1">SQ FOOT<BR>OR FIX</font></b></td>
    <td width="57" align="center"><b><font size="1">TIME PER<BR>SQ FT</font></b></td>
    <td width="67" align="center"><b><font size="2">FROM</font></b></td>
    <td width="81" align="center"><b><font size="2">TO</font></b></td>
  </tr>
  <tr>
    <td width="50">
    <p align="center"><b>1</b></td>
    <td width="238" colspan="4">
    <p align="center"><b><font size="4" color="#FF0000">SAFETY &amp; supplies</font></b></td>
    <td width="67">
    <p align="center">
        <%    starttime = request("startTime0")
            if starttime = "" then
                starttime = "00:00"
            end if %>
        <input type=text name='startTime0' size=5 maxlength=6 tabindex="1" value="<%=starttime%>"></td>
    <td width="81">
    <p align="center">
        <%    endTime = request("endTime0")
            if endTime = "" then
                endTime = "00:00"
            end if %>
        <input type=text name='endTime0' size=5 maxlength=6 tabindex="1" value="<%=endTime%>"></td>
    <td width="53">
    <p align="center"><b>ALL</b></td>
    <td width="267">
    <p align="center"><font size="2">F</font><b><font size="2">ollow all Safety
    Precautions / Get supplies &amp; Proceed to work area. </font></b></td>
    </tr>

  <%  '######  Begin Loop
      cntY = 2
      do until cntY = cntX + 1
  %>
      <tr>
        <td width="50" align="center">
        <p align="center"><b>x<%=cntX%>y<%=cnty%></b></td>
        <td width="72" align="center">&nbsp;</td>
        <td width="50" align="center">
          <p align="center">
          <select size="1" name="SpaceTypeTask<%=cntY%>" onChange="PopulateTextbox();">
                  <option selected>---</option>      
                  <%    sql = "SELECT * FROM RANGE ORDER BY space_comp, taskid;"
                       set rs = db.Execute(sql)
                   
                       If NOT(rs.BOF AND rs.EOF) then
                           do until rs.eof
                               if request("SpaceTypeTask"&cntY) = rs("space_comp")&"-"&rs("taskid")&"-"&rs("min_per") then
                                   spacetypeSELECTED = "SELECTED"
                               else
                                   spacetypeSELECTED = ""
                               end if
                               response.write "<option "&spacetypeSELECTED&" value='"&rs("space_comp")&"-"&rs("taskid")&"-"&rs("min_per")&"'>"&rs("space_comp")&rs("taskid")&"</option>"
                               rs.movenext
                           loop
                       end if  %>                       
          </select> <a title="View Space Types" href="javascript:PopUp('help.asp?helptype=spacetype')">?</a></td>
        <td width="50" align="center">
            <% sqFT_Fix = request("sqFT_Fix"&cntY)
               if sqFT_Fix = "" then
                       sqFT_Fix = 0
               end if
            %>
            <input type=text onBlur="checkNumeric(this,0,100000,'','.','');" onChange="PopulateTextbox();" name='sqFT_Fix<%=cntY%>' size=5 maxlength=6 tabindex="1" value="<%=sqFT_Fix%>">
        </td>
        <td width="57" align="center">
            <input type=text name="TimePerSqFt<%=cntY%>" size=5 maxlength=9 tabindex="1" value="<%=request("TimePerSqFt"&cntY)%>"></td>
        <td width="67" align="center">
            <% if cntY = 2 then
                    PrevStartTime = request("endtime0")
               else
                       PrevStartTime = request("endtime"&cntY-1)
               end if %>
            <input type=text name="startTime<%=cntY%>" size=5 tabindex=1 value="0" value="<%=PrevStartTime%>">
        </td>
        <td width="81" align="center">
            <input type=text name="endTime<%=cntY%>" size=5 tabindex=1 value="<%=request("endtime"&cntY-1)%>">
            <input onClick="PopulateTextbox();" type="button" value="+" name="B1"></td>
        <td width="53" align="center">&nbsp;</td>
        <td width="267" align="center"><input type="text" name="T1" size="20"></td>
      </tr>
  <%  '######  End Loop
      cntY = cntY + 1
      loop
  %>

  <tr>
      <td colspan="4" width="231">
          <p align="center"><input type="submit" value="Add Item" name="addItem">
          <input type="hidden" value="<%=cntX%>" name="cntX">
      </td>
      <td colspan="5" width="537">
          <p align="center"><input type="submit" value="Save" name="saveForm">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="submit" value="Cancel" name="cancelForm">
        <input type="text" name="cntY" size="1" value="<%=cntY%>"></td>
  </tr>
  
</table>
</form>


</body>


</html>
trollacious (Programmer)
17 Jan 06 17:50
One thing I learned in programming is that if you refer to an object's value more than twice, you should put it in a separate variable and use that instead.

CODE

sub PopulateTextbox()

dim x, scX, space_comp, taskid, minper, spacetype, newtime, checktime, varnum
dim spaceelement, spacevalue, sqftelement, sqftvalue
x = 0
varnum = document.frmMS47.elements("cntY").value - 1
scX = 0
space_comp = ""
taskid = ""
minper = 0
spaceelement = document.frmMS47.elements("SpaceTypeTask" & varnum)
spacevalue = spaceelement.value


do until x > LEN(spacevalue)
  if right(left(spacevalue,x),1) = "-"  AND space_comp = "" then
    space_comp = left(spacevalue,x-1)
    scX = x
    x = x + 1
  end if
  if right(left(spacevalue,x),1) = "-" AND taskid = "" then
    taskid = right((left(spacevalue,(x-1))),x-scx-1)
    xx = x
    minper = right(spacevalue, LEN(spacevalue) - x)
    x = LEN(spacevalue)
  end if
  x = x + 1
loop    

document.frmMS47.elements("TimePerSqFt" & varnum).value =  minper
  
checktime = document.frmMS47.endtime0.value

sqftelement = document.frmMS47.elements("sqFT_Fix" & varnum)
sqftvalue = sqftelement.value


myDec = InStr(1, CStr(minper * sqftvalue), ".", vbTextCompare)
If myDec > 0 Then
  roundUp = CDbl(Left(CStr(minper * sqftvalue), myDec)) + 1
Else
  roundUp = minper * sqftvalue
End If

newtime = (DATEADD("n",((roundUp)),checktime))
document.frmMS47.elements("endTime" & varnum).value =  formatdatetime(newtime,4)
  
document.frmMS47.elements("startTime" & varnum).value =  formatdatetime(checktime,4)
  
end sub

Lee
tsuji (TechnicalUser)
18 Jan 06 7:08
I do not look into the new problem raised---don't understand it (just guessing are you submit the page to itself or somethig?!..)

Remaining on the function. It seems it is scripted in a very obscur manner. Functionally, it is simply the below for the do until loop. (minper should be a number and when it is extracted from the .value, the field should have the value of form xxxxx...xx-yyy...yyy-nnn...nnn where n is decimal number. You should validate it in any case in order no error when it multiplies something after the loop.)

dim a
a=split(spacevalue,"-")
select case ubound(a)
    case 2
        space_comp=a(0)
        scx=len(space_comp)+1
        taskid=a(1)
        minper=a(2)    'should be a numeric! no validation anywhere
    case 1
        space_comp=a(0)
        scx=len(space_comp)+1
        'the rest are initialized values
    case 0
        'all initialized values
end select

The way it was scripted is very obscur and clumsy.
lennitw (Programmer) (OP)
18 Jan 06 7:53
Thanks Lee!

I got it working.  It now works exactly as I had planned.  I just wish there was a way to change a value in any of the fields and ALL the textboxes would immediately reflect the change.  As it is written, once you enter the data and add a row, you have to start all over to change a value from a previous row.
trollacious (Programmer)
18 Jan 06 9:55
There's a way to change the values like you want, but that should probably be a separate posting.  Since you have the number of the row, you can access all the other elements in other rows through function calls you write.

Lee
lennitw (Programmer) (OP)
18 Jan 06 11:09
I thought of that, but I don't know how many rows until they've stopped adding rows and I don't know how to make a simple code to account for however many rows are currently on the screen.  :(
trollacious (Programmer)
18 Jan 06 16:42
Every time a row is added, increment your element "cntY" value.  Then read it like you do with the function we've been working on here.

Lee
lennitw (Programmer) (OP)
19 Jan 06 9:55
On your suggestion, I've started a new thread.  Thanks a million for the help.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close