Conditional in Loop not working

Discussion in 'ASP General' started by Drew, Nov 7, 2005.

  1. Drew

    Drew Guest

    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
     
    Drew, Nov 7, 2005
    #1
    1. Advertising

  2. Drew

    Evertjan. Guest

    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)
     
    Evertjan., Nov 7, 2005
    #2
    1. Advertising

  3. Drew

    McKirahan Guest

    "Evertjan." <> wrote in message
    news:Xns9707E7B2EC739eejj99@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
     
    McKirahan, Nov 7, 2005
    #3
  4. 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"
     
    Bob Barrows [MVP], Nov 8, 2005
    #4
  5. Drew

    McKirahan Guest

    "Bob Barrows [MVP]" <> wrote in message
    news:uBNgEz$...
    > 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.
     
    McKirahan, Nov 8, 2005
    #5
  6. Drew

    Patrice Guest

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

    --
    Patrice

    "McKirahan" <> a écrit dans le message de
    news:...
    > "Bob Barrows [MVP]" <> wrote in message
    > news:uBNgEz$...
    > > 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.
    >
    >
     
    Patrice, Nov 8, 2005
    #6
  7. McKirahan wrote:
    > "Bob Barrows [MVP]" <> wrote in message
    > news:uBNgEz$...
    >> 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/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
    --
    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"
     
    Bob Barrows [MVP], Nov 8, 2005
    #7
  8. Bob Barrows [MVP] wrote:
    > McKirahan wrote:
    >> "Bob Barrows [MVP]" <> wrote in message
    >> news:uBNgEz$...
    >>> 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/archive/2003/11/01/53329.aspx
    >

    Oh! and don't forget to look at Part 2:
    http://blogs.msdn.com/ericlippert/archive/2003/11/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"
     
    Bob Barrows [MVP], Nov 8, 2005
    #8
  9. Drew

    Drew Guest

    Thanks for all the replies... I ended up using the array method.

    Thanks,
    Drew
     
    Drew, Nov 9, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    504
    Andy Dingley
    Dec 27, 2003
  2. Alec S.
    Replies:
    10
    Views:
    10,338
    Alec S.
    Apr 16, 2005
  3. Kristen

    Conditional Gridview Edit Column Not Working

    Kristen, Aug 31, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    282
    Kristen
    Aug 31, 2006
  4. bonus
    Replies:
    8
    Views:
    301
  5. Isaac Won
    Replies:
    9
    Views:
    460
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page