Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Using a Session Variable to Populate a SQL Statement

    Hi everyone,

    Ok so what I am basically trying to do is this:

    sql = "SELECT * FROM LiftPasses WHERE "&Session("QC_GroupType")&"= 'Yes'"
    set liftpassDB = doquery(sql)

    I have also tried:

    If liftpassDB(Session("QC_GroupType")) = "Yes" Then

    But with no joy, does anyone knoe if this is a) possible b) where i am going wrong or c) an alternative solution.

    What I basically what to do is look at the database, and extract the records where the "GroupType" is set to "Yes" in the database. In order to show results that are correct for that specific group dynamically.

    The GroupType is currently stored in a Session Variable.

    Thanks in advance.

    Alex

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,027
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Your SQL looks perfectly reasonable to me, assuming: (1) The value in the session variable really is the name of a field in the table and (2) You really are using a TEXT field of some kind and it really does have some records where the value is 'YES'.

    If this is an Access database and if the field in question is a Yes/No field, then the problem is that 'Yes' is never actually stored in such fields. Only from Access-the-program will you ever actually see a value as 'Yes'. From ADO, the field will be a boolean field, having True and False values.

    NOTE: True and False are *keywords*. You do *NOT* put apostrophes around them if you use them as such!

    If that is the case, just use
    Code:
    sql = "SELECT * FROM LiftPasses WHERE "&Session("QC_GroupType")&"= True"
    or, since comparing to True is really unnecessary,
    Code:
    sql = "SELECT * FROM LiftPasses WHERE "&Session("QC_GroupType")
    But in any case, you maybe should DEBUG to find out if the session value is what you think it is.

    So no matter which variant you use, you should be doing
    Code:
    sql = "SELECT * FROM LiftPasses WHERE "&Session("QC_GroupType")&"= 'Yes'" ' or other variant
    Response.Write "<hr>DEBUG SQL: " & sql & "<hr>"
    ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    alex4501 (08-30-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •