Conditional in Loop not working

D

Drew

I am building an application for keeping track of user permissions here at
work. I have built the interfaces, and am now working on the processing
page for inserting to the database. I am having a problem with a
conditional in my loop not working correctly, I am sure that I have been
overlooking something, but cannot pin it down.


I have pared the code down quite a bit just so that I could resolve my
issues with it. I have a page named addAIMR.asp that has about 34
checkboxes, 2 dropdowns and 2 textboxes. I am trying to something like
this,

If checkbox is checked
AccessOption1 = 1 (which is the access option number)
Else
AccessOption1 = 0
End If

Then I am trying to loop through the code to insert only the ones that <> 0.
This should be rather straight forward, but I am running into issues... I
have inserted many Response.Write's throughout the code to see the
variables, and everything looks kosher, until it gets to the loop, then it
shows that it would insert all the records.

Here is my code for processAIMR.asp,

<%
Dim AIMRDate
Dim AuthName
Dim EmpID
Dim Comments

AIMRDate = Request.Form("Date")
AuthName = Request.Form("AuthName")
EmpID = Request.Form("EmpID")
Comments = Request.Form("Comments")

Dim AccessOption1
Dim AccessOption2
Dim AccessOption3
Dim AccessOption4
Dim AccessOption5

If Request.Form("chkStandard") = "1" Then
Response.Write("1 = Checked<br>")
AccessOption1 = "1"
Else
Response.Write("1 = Not Checked<br>")
AccessOption1 = "0"
End If
If Request.Form("chkEmail") = "1" Then
Response.Write("2 = Checked<br>")
AccessOption2 = "2"
Else
Response.Write("2 = Not Checked<br>")
AccessOption2 = "0"
End If
If Request.Form("chkVPN") = "1" Then
Response.Write("3 = Checked<br>")
AccessOption3 = "3"
Else
Response.Write("3 = Not Checked<br>")
AccessOption3 = "0"
End If
If Request.Form("chkOtherNetwork") = "1" Then
Response.Write("4 = Checked<br>")
AccessOption4 = "4"
Else
Response.Write("4 = Not Checked<br>")
AccessOption4 = "0"
End If
If Request.Form("chkPRAIS") = "1" Then
Response.Write("5 = Checked<br>")
AccessOption5 = "5"
Else
Response.Write("5 = Not Checked<br>")
AccessOption5 = "0"
End If

For i = 1 to 5
If (AccessOption & i) <> "0" Then
Response.Write("AccessOption" & i & " is not empty, therefore will be
inserted<br>")
'Insert 1 record for each option checked
Else
Response.Write("AccessOption" & i & " is empty, therefore will NOT be
inserted<br>")
End If
Next
%>

If I check 2 boxes, chkStandard and chkVPN, then I get the following written
to the page,

1 = Checked
2 = Not Checked
3 = Checked
4 = Not Checked
5 = Not Checked
AccessOption1 is not empty, therefore will be inserted
AccessOption2 is not empty, therefore will be inserted
AccessOption3 is not empty, therefore will be inserted
AccessOption4 is not empty, therefore will be inserted
AccessOption5 is not empty, therefore will be inserted

Can anyone spot where I am going awry?

TIA,
Drew
 
E

Evertjan.

Drew wrote on 07 nov 2005 in microsoft.public.inetserver.asp.general:
AccessOption5 = "0"
End If

For i = 1 to 5
If (AccessOption & i) <> "0" Then

You cannot do this:

(AccessOption & i)
This does not make a variable name in ASP-VBS

Use an array:

dim AccessOption()

AccessOption(5) = "0"
i=5
If AccessOption(i) <> "0" Then
....
 
M

McKirahan

Evertjan. said:
Drew wrote on 07 nov 2005 in microsoft.public.inetserver.asp.general:


You cannot do this:

(AccessOption & i)
This does not make a variable name in ASP-VBS

But this does:

If Eval("AccessOption" & i) <> "0" Then
 
P

Patrice

Really overkill here. There is no point using Eval when you can use arrays
instead for both execution efficiency and clarity...
 
B

Bob Barrows [MVP]

McKirahan said:

Mainly, because it starts another compiler. Instead of a single compiler
being applied to your code, you now have two compliers being spawned to,
well, compile the code. See:
http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx

Eval has its places, but its use should be considered as an absolute last
resort. In this case, there is a much better solution available (the array),
so eval should not be considered.

Bob Barrows
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top