By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

Conditional in Loop not working

P: n/a
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
Nov 7 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
....

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Nov 7 '05 #2

P: n/a
"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.242...
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


But this does:

If Eval("AccessOption" & i) <> "0" Then
Nov 7 '05 #3

P: n/a
McKirahan wrote:
This does not make a variable name in ASP-VBS


But this does:

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


Yeah, but Eval is evil :-)
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Nov 8 '05 #4

P: n/a
"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
McKirahan wrote:
This does not make a variable name in ASP-VBS


But this does:

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


Yeah, but Eval is evil :-)


Why?

I understand that it is deprecated in JavaScript
but this is VBScript and it solves his problem.
Nov 8 '05 #5

P: n/a
Really overkill here. There is no point using Eval when you can use arrays
instead for both execution efficiency and clarity...

--
Patrice

"McKirahan" <Ne**@McKirahan.com> a écrit dans le message de
news:L4********************@comcast.com...
"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
McKirahan wrote:
> This does not make a variable name in ASP-VBS

But this does:

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


Yeah, but Eval is evil :-)


Why?

I understand that it is deprecated in JavaScript
but this is VBScript and it solves his problem.

Nov 8 '05 #6

P: n/a
McKirahan wrote:
"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
McKirahan wrote:
This does not make a variable name in ASP-VBS

But this does:

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


Yeah, but Eval is evil :-)


Why?


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/ar.../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
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Nov 8 '05 #7

P: n/a
Bob Barrows [MVP] wrote:
McKirahan wrote:
"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
McKirahan wrote:

> This does not make a variable name in ASP-VBS

But this does:

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

Yeah, but Eval is evil :-)


Why?


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/ar.../01/53329.aspx

Oh! and don't forget to look at Part 2:
http://blogs.msdn.com/ericlippert/ar.../04/53335.aspx

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Nov 8 '05 #8

P: n/a
Thanks for all the replies... I ended up using the array method.

Thanks,
Drew
Nov 9 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.