Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript Problem - Increment variable 1

Status
Not open for further replies.

lennitw

Programmer
Jan 10, 2006
13
US
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>
 
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
 
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>
 
Have to fallback more thoroughly on vbs syntax.
[tt]>document.frmMS47.elements["SpaceTypeTask" & varnum][/tt]
[tt]document.frmMS47.elements[red]([/red]"SpaceTypeTask" & varnum[red])[/red][/tt]
 
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
 
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>
 
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
[red]dim spaceelement, spacevalue, sqftelement, sqftvalue[/red]
x = 0
varnum = document.frmMS47.elements("cntY").value - 1
scX = 0
space_comp = ""
taskid = ""
minper = 0
[red]spaceelement = document.frmMS47.elements("SpaceTypeTask" & varnum)
spacevalue = spaceelement.value[/red]

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

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

[red]sqftelement = document.frmMS47.elements("sqFT_Fix" & varnum)
sqftvalue = sqftelement.value[/red]

myDec = InStr(1, CStr(minper * [red]sqftvalue[/red]), ".", vbTextCompare)
If myDec > 0 Then
  roundUp = CDbl(Left(CStr(minper * [red]sqftvalue[/red]), myDec)) + 1
Else
  roundUp = minper * [red]sqftvalue[/red]
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
 
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.)
[tt]
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
[/tt]
The way it was scripted is very obscur and clumsy.
 
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.
 
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
 
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. :(
 
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
 
On your suggestion, I've started a new thread. Thanks a million for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top