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!

Using Array Content for Comparison in If...Then 1

Status
Not open for further replies.

pupadu

Programmer
Mar 18, 2005
24
US
Hello all. I'll do my best to explain this. I've been having a bit of trouble figuring this one out. I have this application that queries the database (SQL Server) based on personnel and appointment date. I then perform GETROWS on the recordset for one field only (appttime) and this data is stored into an array. Please keep in mind that is only the section of the code where I'm running into a problem. The code follows:

Dim varChoseTime

Set objRS = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT persid, apptdate, appttime FROM appointments WHERE persid=" & intApptPersid & " AND apptdate='" & varApptdate & "'"

objRS.Open strSQL, conn

varChoseTime = objRS.GetRows(, , "appttime")


My data is as follows:

09:00:00
12:00:00
14:30:00

My problem is I can't figure out how to take this data and compare it in an If...Then statement as such:

If var0600am = "Yes" and varChoseTime <> "06:00:00" Then
Response.Write "<tr><td align=right>6:00 AM</td>" & "<td><input type=radio name=appttime value=06:00:00></td></tr>"
End If
If var0900am = "Yes" and varChoseTime <> "09:00:00" Then
Response.Write "<tr><td align=right>9:00 AM</td>" & "<td><input type=radio name=appttime value=09:00:00></td></tr>"
End If
If var1200pm = "Yes" and varChoseTime <> "12:00:00" Then
Response.Write "<tr><td align=right>12:00 PM</td>" & "<td><input type=radio name=appttime value=12:00:00></td></tr>"
End If
If var230pm = "Yes" and varChoseTime <> "14:30:00" Then
Response.Write "<tr><td align=right>2:30 PM</td>" & "<td><input type=radio name=appttime value=14:30:00></td></tr>"
End If
If var400pm = "Yes" and varChoseTime <> "16:00:00" Then
Response.Write "<tr><td align=right>4:00 PM</td>" & "<td><input type=radio name=appttime value=16:00:00></td></tr>"
End If


This works except it only looks at the first item in the array. It doesn't look at all the items in the array for each If..Then statement. All of these times are supposed to display if there are not in the array list.

Any and all help will be greatly appreciated.
 
GuitarDave78, thanks for the quick response!

I'm a novice at ASP, so could you elaborate a little more. Preferably by examples.

Thanks much! :)
 
ok, if you have an array aTest with the values 1 2 3 4 in it you need to loop through it to get each value, so
Code:
for n = 0 to ubound(aTest)
  response.write(aTest(n))
next
i should end up with an out put of 1234

So in your case you would need somthing like
Code:
for n = 0 to ubound(varChoseTime)
If var0600am = "Yes" and varChoseTime(n) <> "06:00:00" Then
Response.Write "<tr><td align=right>6:00 AM</td>" & "<td><input type=radio name=appttime value=06:00:00></td></tr>"
End If
If var0900am = "Yes" and varChoseTime(n)<> "09:00:00" Then
Response.Write "<tr><td align=right>9:00 AM</td>" & "<td><input type=radio name=appttime value=09:00:00></td></tr>"
End If
If var1200pm = "Yes" and varChoseTime(n)<> "12:00:00" Then
Response.Write "<tr><td align=right>12:00 PM</td>" & "<td><input type=radio name=appttime value=12:00:00></td></tr>"
End If
If var230pm = "Yes" and varChoseTime(n)<> "14:30:00" Then
Response.Write "<tr><td align=right>2:30 PM</td>" & "<td><input type=radio name=appttime value=14:30:00></td></tr>"
End If
If var400pm = "Yes" and varChoseTime(n)<> "16:00:00" Then
Response.Write "<tr><td align=right>4:00 PM</td>" & "<td><input type=radio name=appttime value=16:00:00></td></tr>"
End If
next

}...the bane of my life!
 
The loop is fine, but the logic is probably flawed. If you want each if statement to look at each element in the array, and only write the string if the value doesn't match ANY element in the array, that logic won't work. Right now it writes the string for EVERY element it doesn't match, potentially writing each string several times. I don't think that's what you want. You'll need a flag variable for each condition, initialized to false. Within the loop, set it to true if you find a true condition. DO NOT set it to false if you find a false condition, otherwise you'll probably override any true condition found. After you've test each condition on each element of the array, check each of your flags. If it is still false, it never matched any element of the array, so then you can output the message.



Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thanks again for appreciated help, GuitarDave78! However, I'm getting an error:

Microsoft VBScript runtime error '800a0009'
Subscript out of range

/appttime.asp, line 189


Any other suggestions?
 
Thanks to you also, TsDragon. However, I'm not sure I follow what you're saying. If I set my logic to true, it's not suppose to do anything. However, if it's false I want it to response.write my html code.

From what I can see the if...then statements work aside from the fact that it only looks at the first item in the array and not the rest.

Nevertheless, I do welcome any and all suggestions and advice for this problem. So if you could elaborate a bit further, I would greatly appreciate it.
 
the trouble is that if you set var0600am = "Yes" then teh logic If var0600am = "Yes" and varChoseTime(n) <> "06:00:00" is true several times

try this out to see what he meens

Code:
varChoseTime = array("09:00:00","12:00:00","14:30:00")
var0600am = "Yes"


for n = 0 to ubound(varChoseTime)
If var0600am = "Yes" and varChoseTime(n) <> "06:00:00" Then
Response.Write "<tr><td align=right>6:00 AM</td>" & "<td><input type=radio name=appttime value=06:00:00></td></tr>"
elseif var0900am = "Yes" and varChoseTime(n)<> "09:00:00" Then
Response.Write "<tr><td align=right>9:00 AM</td>" & "<td><input type=radio name=appttime value=09:00:00></td></tr>"
elseIf var1200pm = "Yes" and varChoseTime(n)<> "12:00:00" Then
Response.Write "<tr><td align=right>12:00 PM</td>" & "<td><input type=radio name=appttime value=12:00:00></td></tr>"
elseIf var230pm = "Yes" and varChoseTime(n)<> "14:30:00" Then
Response.Write "<tr><td align=right>2:30 PM</td>" & "<td><input type=radio name=appttime value=14:30:00></td></tr>"
elseIf var400pm = "Yes" and varChoseTime(n)<> "16:00:00" Then
Response.Write "<tr><td align=right>4:00 PM</td>" & "<td><input type=radio name=appttime value=16:00:00></td></tr>"
End If

next

}...the bane of my life!
 
A "subscript out of range" error happens when your code tries to get an array element that doesnt exist.

Suppose you have an array variable named Foo with 3 elements numbered 1-3 ...
Foo(0) <-- Subscript out of range
Foo(1) <-- OK
Foo(2) <-- OK
Foo(3) <-- OK
Foo(4) <-- Subscript out of range
 
was just about to say that :)
can you get a value doing this

response.write(ubound(varChoseTime))
if it is 0 then you are not pulling any times out of the database

}...the bane of my life!
 
as for the logic, this is not the bestest way of doing it but it will help for now, with out having to set flags etc
basically join the array into a string and then see if the time is in that string using instr
Code:
'***use your get rows bit here***
varChoseTime = array("09:00:00","12:00:00","14:30:00")
var0600am = "Yes"
var1200pm = "Yes"
var400pm = "Yes"
'***use your get rows bit here***

sTime = join(varChoseTime,",")

If var0600am = "Yes" and instr(sTime,"06:00:00") <=0 Then
Response.Write "<tr><td align=right>6:00 AM</td>" & "<td><input type=radio name=appttime value=06:00:00></td></tr>"
End If
If var0900am = "Yes" and instr(sTime,"09:00:00") <=0 Then
Response.Write "<tr><td align=right>9:00 AM</td>" & "<td><input type=radio name=appttime value=09:00:00></td></tr>"
End If
If var1200pm = "Yes" and instr(sTime,"12:00:00") <=0 Then
Response.Write "<tr><td align=right>12:00 PM</td>" & "<td><input type=radio name=appttime value=12:00:00></td></tr>"
End If
If var230pm = "Yes" and instr(sTime,"14:30:00") <=0 Then
Response.Write "<tr><td align=right>2:30 PM</td>" & "<td><input type=radio name=appttime value=14:30:00></td></tr>"
End If
If var400pm = "Yes" and instr(sTime,"16:00:00") <=0 Then
Response.Write "<tr><td align=right>4:00 PM</td>" & "<td><input type=radio name=appttime value=16:00:00></td></tr>"
End If

}...the bane of my life!
 
If I set my logic to true, it's not suppose to do anything. However, if it's false I want it to response.write my html code.

Then instead of saying

[tt] if flag then[/tt]

say

[tt] if not flag then[/tt]


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Okay, GuitarDave78, I tried the response.write ubound(varChoseTime) and received 0.

My question is how is that I can receive a list items in the array if I do this:

for each item in varChoseTime
Response.Write item & "<br>"
next


but not when I use the response.write ubound(varChoseTime).

Again any and all help is greatly appreciated. I have to figure this out because it's the core of the application. Thanks again for all help.
 
TsDragon, I also tried your if not flag then suggestion. I received the following error

Microsoft VBScript runtime error '800a0009'
Subscript out of range

/appttime.asp, line 190

I know now that this means that the code is trying to get an array element that doesn't exist.

Any other suggestions or advice? Thanks in advance.
 
I'll admit that one has stumped me.

if the for each works you can adapt teh original loop to

Code:
for each item in varChoseTime
If var0600am = "Yes" and item <> "06:00:00" Then
Response.Write "<tr><td align=right>6:00 AM</td>" & "<td><input type=radio name=appttime value=06:00:00></td></tr>"
End If
next

But you still need to change the logic a bit or use the instr method i showed you. if join doesnt work you could use
Code:
for each item in varChoseTime
  sTime = sTime &","& item
next
but i will get good coder in here shouting at me for that kind of advice ;)

}...the bane of my life!
 
Also, GuitarDave78, I tried your join...instr suggestions and I received this error.


Microsoft VBScript runtime error '800a000d'

Type mismatch: 'join'

/appttime.asp, line 183


Again, any suggestions and advice is greatly appreciated.
 
GuitarDave78, that last loop that you gave me did the trick!

I used the following:

for each item in varChoseTime
sTime = sTime & "," & item
next

If var0630am = "Yes" and instr(sTime,"06:30:00") <=0 Then
Response.Write "<tr><td align=right>6:30 AM</td>" & "<td><input type=radio name=appttime value=06:30:00></td></tr>"
End If


Worked like a charm! I can't thank you enough. All of you who gave your time to help me with this dilemma, I humbly thank you all. :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top