If/Then with variables - ?

M

montiy

Copied below is some code that I've written for a page of mine, and it's not
working. Could someone please take a look and suggest what I might be doing
wrong? I'm very much a beginner at code-writing, so please keep your replies
on a beginner's level...Thanks!

I'm trying to accomplish the following:

1) The web page pulls three fields from a database into which the user is
entering data. (I've put validation forms on the entry form to be sure that
the user is entering minutes as an integer between greater than zero, seconds
as an integer between 0 and 59, and hundredths as an integer between 0 and
99.)

2) The three variables (Minutes50FreeBoys, Seconds50FreeBoys, and
Hundredths50FreeBoys) are assigned to the three database field values.

3) I want the results (when displayed in a table column that I've created)
to be displayed like such: "1:03.05" (for minutes, seconds, and hundredths of
a second). THUS...I need to be sure that the "seconds" field and the
"hundredths" field are both displayed as TWO DIGITS. So, I've tried to use
if/then statements to change any single-digits numbers (0 through 9) to
double-digits ("00", "01", "02", etc).

4) Then I want the result to display in the following format:

Minutes:Seconds.Hundredths (ex: 2:05.73)

Printed here is my current code. I'm GUESSING that I'm going wrong
somewhere in the handling of my variables (maybe in the if/then statements
where I'm changing a number to a text value?). I'd appreciate any help that
you're able to give...Thanks!

-- Nate

---------------------------

<%
Minutes50FreeBoys=FPFieldVal(fp_rs,"Minutes_Freestyle50yd")
Seconds50FreeBoys=FP_FieldVal(fp_rs,"Seconds_Freestyle50yd")
Hundredths50FreeBoys=FP_FieldVal(fp_rs,"Hundredths_Freestyle50yd")
If Seconds50FreeBoys=0 Then Seconds50FreeBoys="00"
If Seconds50FreeBoys=1 Then Seconds50FreeBoys="01"
If Seconds50FreeBoys=2 Then Seconds50FreeBoys="02"
If Seconds50FreeBoys=3 Then Seconds50FreeBoys="03"
If Seconds50FreeBoys=4 Then Seconds50FreeBoys="04"
If Seconds50FreeBoys=5 Then Seconds50FreeBoys="05"
If Seconds50FreeBoys=6 Then Seconds50FreeBoys="06"
If Seconds50FreeBoys=7 Then Seconds50FreeBoys="07"
If Seconds50FreeBoys=8 Then Seconds50FreeBoys="08"
If Seconds50FreeBoys=9 Then Seconds50FreeBoys="09"
If Hundredths50FreeBoys=0 Then Hundredths50FreeBoys="00"
If Hundredths50FreeBoys=1 Then Hundredths50FreeBoys="01"
If Hundredths50FreeBoys=2 Then Hundredths50FreeBoys="02"
If Hundredths50FreeBoys=3 Then Hundredths50FreeBoys="03"
If Hundredths50FreeBoys=4 Then Hundredths50FreeBoys="04"
If Hundredths50FreeBoys=5 Then Hundredths50FreeBoys="05"
If Hundredths50FreeBoys=6 Then Hundredths50FreeBoys="06"
If Hundredths50FreeBoys=7 Then Hundredths50FreeBoys="07"
If Hundredths50FreeBoys=8 Then Hundredths50FreeBoys="08"
If Hundredths50FreeBoys=9 Then Hundredths50FreeBoys="09"
%>
<%
=Minutes50FreeBoys & ":" Seconds50FreeBoys & "." & Hundredths50FreeBoys
%>
 
M

montiy

....OR...would it work better to bypass all the If/Then statements and just
attempt to display the variable as a Double? (I'm guessing a "Double" is a
two-digit number?)

I tried the following, but it didn't seem to work, either:

<%
Minutes50FreeBoys=FPFieldVal(fp_rs,"Minutes_Freestyle50yd")
Seconds50FreeBoys=FP_FieldVal(fp_rs,"Seconds_Freestyle50yd")
Hundredths50FreeBoys=FP_FieldVal(fp_rs,"Hundredths_Freestyle50yd")
%>
<%
=Minutes50FreeBoys & ":" & CDouble(Seconds50FreeBoys) & "." &
CDouble(Hundredths50FreeBoys)
%>
 
M

montiy

I figured it out! Just wanted to post it here in case it's a help to anyone
else, at some point. (Turns out that my initial mistake was leaving out a
"&" in the concatenation when displaying the results.)

-- Nate
-------------------------

<%
Minutes50FreeBoys=FP_FieldVal(fp_rs,"Minutes_Freestyle50yd")

Seconds50FreeBoys=FP_FieldVal(fp_rs,"Seconds_Freestyle50yd")

Hundredths50FreeBoys=FP_FieldVal(fp_rs,"Hundredths_Freestyle50yd")

If Seconds50FreeBoys < 10 Then Seconds50FreeBoys = "0" + Seconds50FreeBoys

If Hundredths50FreeBoys < 10 Then Hundredths50FreeBoys = "0" +
Hundredths50FreeBoys
%>

<%
=Minutes50FreeBoys & ":" & Seconds50FreeBoys & "." & Hundredths50FreeBoys
%>
 
D

David Berry

Hi Nate. Looks like you've been working hard and you're getting the hang of
things really well. As a suggestion, even though your IF .. THEN statement
works like this that's only true if it's on one line. As a good practice
you should always have an END IF in case the line accidentally gets wrapped
or you want to add more to it. For example:

If Seconds50FreeBoys < 10 Then
Seconds50FreeBoys = "0" + Seconds50FreeBoys
End If

Also, by doing it this way it makes it easier to add another condition. For
example:

If Seconds50FreeBoys < 10 Then
Seconds50FreeBoys = "0" + Seconds50FreeBoys
Else
Seconds50FreeBoys = "00"
End If

Another good practice with variables, as you saw with the calculation
problem you were having, is to define what type of variable it is when doing
math. Ex, using CInt or CDbl:

If Seconds50FreeBoys < 10 Then
Seconds50FreeBoys = "0" + CInt(Seconds50FreeBoys)
End If

This ensures that the values always come out as expected.

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top