clean up repeating code

# Thread: clean up repeating code

1. Member
Join Date
Dec 1969
Posts
70

## clean up repeating code

Hi, I need some help with repetitive code and how to condense to one or more function(s). Here is what I need to do:<BR>Function GetTime(strDT)<BR>GetTime = DatePart("h", strDT) & ":" & DatePart("n", strDT)<BR>End Function<BR>(This function is working.)<BR>This is the part that needs condensed - <BR>&#060;% <BR>diff1 = DateDiff("n", GetTime(RS("rocact")), GetTime(RS("rocsch")))<BR>diff2 = DateDiff("n", GetTime(RS("louact")), GetTime(RS("lousch")))<BR>%&#062;<BR>(The rocact, rocsch, louact, lousch are just a few of the different actual and scheduled times within one record.)<BR>Then I need to do a Response.Write on the difference, and <BR>also check to see if this difference is &#062; -15 (minutes) and if so, make the actual textbox red. And, if the difference is less than zero, make the difference = 0.<BR>Any ideas how to go about this without having twenty or so diff1, diff2 calculations, and how to get the other comparisons to work?<BR>Thank you,<BR>-L

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Show code for ONE...

...and I&#039;ll show you how to make it into a SUB or FUNCTION for multiple use. But I&#039;m not sure from your description what all you do for one. So show all the code for one instance of a pair of names/values.<BR><BR>Incidentally...you are working too hard.<BR><BR>You can use TimeValue (built in VBS function) in place of your GetTime. And in your GetTime, you could have used (with more efficiency):<BR><BR>theDt = CDate(strDate) &#039; do this only once instead of multiple times<BR>GetTime = TimeSerial( Hour(theDt), Minute(theDt), 0 )<BR><BR>But, as I said, you could just use TimeValue and dump GetTime completely.<BR><BR>Peek around in the VBS docs some more. There might be several other built-in functions that you can use to advantage that you haven&#039;t discovered yet. I recommend reading through the entire list of functions, top to bottom, at *least* once...just to get familiar with what&#039;s there.<BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
11,334

## Interesting

I thought you were boycotting this forum and only staying in the advanced?<BR><BR>"Everytime I think I get out, they pull me right back in"<BR><BR>-- Michael Corleone<BR> Godfather III

4. Member
Join Date
Dec 1969
Posts
70

## RE: Show code for ONE...

&#060;% &#039; for a particular date<BR>Function GetTime(strDT)<BR>GetTime = DatePart("h", strDT) & ":" & DatePart("n", strDT)<BR>End Function<BR> if diff &#062; 0 then<BR> diff = 0<BR> elseif <BR> diff1 &#062; 0 then<BR> diff1 = 0<BR> elseif<BR> diff2 &#062; 0 then<BR> diff2 = 0<BR> elseif<BR> diff3 &#062; 0 then<BR> diff3 = 0<BR> elseif<BR> diff4 &#062; 0 then<BR> diff4 = 0<BR> elseif<BR> diff5 &#062; 0 then<BR> diff5 = 0<BR> end if<BR> diff1 = DateDiff("n", GetTime(RS("midact1")), GetTime(RS("midsch1")))<BR> diff2 = DateDiff("n", GetTime(RS("midact2")), GetTime(RS("midsch2")))<BR> diff3 = DateDiff("n", GetTime(RS("midact3")), GetTime(RS("midsch3")))<BR>%&#062;<BR> &#060;tr&#062;<BR> &#060;td&#062;&#060;font color="red" SIZE="2"&#062;&#060;B&#062;LATE BY:<BR>&#060;small&#062;(Minutes)&#060;/small&#062;&#060;/B&#062;&#060;/font&#062;&#060;/td&#062;<BR> &#060;td align="center"&#062;&#060;input type="text" name="late" value="&#060;%=diff1%&#062;" size="5"&#062;&#060;/TD&#062;<BR> &#060;td align="center"&#062;&#060;input type="text" name="late" value="&#060;%=diff2%&#062;" size="5"&#062;&#060;/TD&#062;<BR> &#060;td align="center"&#062;&#060;input type="text" name="late" value="&#060;%=diff3%&#062;" size="5"&#062;&#060;/TD&#062;<BR> &#060;/tr&#062;<BR> &#060;%<BR> diff1 = DateDiff("n", GetTime(RS("wooact1")), GetTime(RS("woosch1")))<BR> diff2 = DateDiff("n", GetTime(RS("newact1")), GetTime(RS("newsch1")))<BR> diff3 = DateDiff("n", GetTime(RS("ausact1")), GetTime(RS("aussch1")))<BR> diff4 = DateDiff("n", GetTime(RS("elyact1")), GetTime(RS("elysch1")))<BR> diff5 = DateDiff("n", GetTime(RS("mvact1")), GetTime(RS("mvsch1")))<BR>%&#062;<BR> &#060;tr&#062;<BR> &#060;td&#062;&#060;font color="red" SIZE="2"&#062;&#060;B&#062;LATE BY:<BR>&#060;small&#062;(Minutes)&#060;/small&#062;&#060;/B&#062;&#060;/font&#062;&#060;/td&#062;<BR> &#060;td align="center"&#062;&#060;input type="text" name="late" value="&#060;%=diff1%&#062;" size="5"&#062;&#060;/TD&#062;<BR> &#060;td align="center"&#062;&#060;input type="text" name="late" value="&#060;%=diff2%&#062;" size="5"&#062;&#060;/TD&#062;<BR> &#060;td align="center"&#062;&#060;input type="text" name="late" value="&#060;%=diff3%&#062;" size="5"&#062;&#060;/TD&#062;<BR> &#060;td align="center"&#062;&#060;input type="text" name="late" value="&#060;%=diff4%&#062;" size="5"&#062;&#060;/TD&#062;<BR> &#060;td align="center"&#062;&#060;input type="text" name="late" value="&#060;%=diff5%&#062;" size="5"&#062;&#060;/TD&#062;<BR> &#060;/tr&#062;<BR>&#060;% &#039; this part does not work<BR> for i=1 to i=5<BR> if "diff" & i &#060; -15 then<BR> td.backgroundColor = "RED"<BR> elseIf "diff" & i &#062; 0 then<BR> "diff" & i = 0<BR> end if<BR> next <BR>%&#062;<BR>I am new to this (for the most part) so any help you can provide would be greatly appreciated.<BR>:)<BR>-L

5. Senior Member
Join Date
Dec 1969
Posts
96,118

...for one field at a time.<BR><BR>You seem to be trying to combine an understanding of client side scripting (e.g., td.backgroundColor) with coding for server side. Nope. Doesn&#039;t work that way.<BR><BR>Let&#039;s start all over...<BR><BR>&#060;%<BR>Function MakeLatenessTD( actual, scheduled )<BR>&nbsp; &nbsp; lateBy = DateDiff( "n", TimeValue(actual), TimeValue(scheduled) )<BR>&nbsp; &nbsp; If lateBy &#062; 0 Then lateBy = 0 &#039; on time! or better<BR>&nbsp; &nbsp; If lateBy &#060; -15 Then <BR>&nbsp; &nbsp; &nbsp; &nbsp; td = "&#060;TD bgColor=&#039;red&#039;"<BR>&nbsp; &nbsp; Else<BR>&nbsp; &nbsp; &nbsp; &nbsp; td = "&#060;TD"<BR>&nbsp; &nbsp; End If<BR>&nbsp; &nbsp; td = td & " align=&#039;center&#039;&#062;"<BR>&nbsp; &nbsp; td = td & "&#060;input type=&#039;text&#039; name=&#039;late&#039; value=&#039;" & lateBy & "&#039; size=5&#062;"<BR>&nbsp; &nbsp; td = td & "&#060;/TD&#062;" & vbNewLine<BR>&nbsp; &nbsp; MakeLatenessTD = td<BR>End Function<BR>%&#062;<BR><BR>And then you use it thus:<BR><BR>&#060;% <BR>...<BR>Response.Write MakeLatenessTD( RS("wooact1"), RS("woosch1") ) <BR>...<BR>%&#062;<BR><BR>HOWEVER... Why are you making these TEXT input boxes??? Especially, why are you giving them all the same name??? If this is just for a report, and is not meant to be edited, then you probably want to replace<BR><BR>&nbsp; &nbsp; td = td & "&#060;input type=&#039;text&#039; name=&#039;late&#039; value=&#039;" & lateBy & "&#039; size=5&#062;"<BR><BR>with simply<BR><BR>&nbsp; &nbsp; td = td & lateBy <BR><BR>Why put the value into a form field, at all? Especially into a form field that has the same name as all the others, which would make it pretty useless if you submitted the form and tried to work out which number meant what on the next page.<BR><BR>

6. Member
Join Date
Dec 1969
Posts
70

Can you recommend something that I could read that would provide me with the knowledge to do this on my own?<BR>By the way - thank you - this works. Now, I just need to understand how and why it works. Can you give me some direction?<BR>THank you!<BR>-L

7. Senior Member
Join Date
Dec 1969
Posts
96,118