Dynamic Variable Names - Syntax?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken Halley

    Dynamic Variable Names - Syntax?

    How does one refer to a variable name using a variable within a variable
    name??? For example, I want to perform a SQL query and use the results of
    the query to determine which variable to assign a value to. Let's say I've
    dimmed 12 variables based on the months called "featured_x_pro duct" where x
    is the month. I'd like to refer to the variable with something
    like: featured_" & R("month") & "_product where R("month") is the from the
    current row in the db. I don't know the correct syntax or method for doing
    so. A full example is provided below. Thanks in advance for you help!

    dim featured_jan_pr oduct
    dim featured_feb_pr oduct
    dim featured_mar_pr oduct
    dim featured_apr_pr oduct
    ....

    SELECT status, month from products where year='2004' AND
    page='featured_ products'

    While not R.EOF
    If R("status") = "sold" Then
    featured_" & R("month") & "_product = "Sold Out!" // (choose the
    variable based on the "month" value in the current row)
    Else
    featured_" & R("month") & "_product = "Buy Now!"
    End If
    R.MoveNext
    Wend




  • Aaron Bertrand - MVP

    #2
    Re: Dynamic Variable Names - Syntax?

    Take a look at eval and exec in the VBScript documentation.



    "Ken Halley" <ken@alpacanati on.com> wrote in message
    news:ORf0fDKpDH A.2592@TK2MSFTN GP10.phx.gbl...[color=blue]
    > How does one refer to a variable name using a variable within a variable
    > name??? For example, I want to perform a SQL query and use the results of
    > the query to determine which variable to assign a value to. Let's say[/color]
    I've[color=blue]
    > dimmed 12 variables based on the months called "featured_x_pro duct" where[/color]
    x[color=blue]
    > is the month. I'd like to refer to the variable with something
    > like: featured_" & R("month") & "_product where R("month") is the from the
    > current row in the db. I don't know the correct syntax or method for[/color]
    doing[color=blue]
    > so. A full example is provided below. Thanks in advance for you help!
    >
    > dim featured_jan_pr oduct
    > dim featured_feb_pr oduct
    > dim featured_mar_pr oduct
    > dim featured_apr_pr oduct
    > ...
    >
    > SELECT status, month from products where year='2004' AND
    > page='featured_ products'
    >
    > While not R.EOF
    > If R("status") = "sold" Then
    > featured_" & R("month") & "_product = "Sold Out!" // (choose the
    > variable based on the "month" value in the current row)
    > Else
    > featured_" & R("month") & "_product = "Buy Now!"
    > End If
    > R.MoveNext
    > Wend
    >
    >
    >
    >[/color]


    Comment

    • Ray at

      #3
      Re: Dynamic Variable Names - Syntax?

      You can use the Eval function to do this, but I suggest not using Eval. It
      makes for the most confusing and sloppy code you'll ever see.

      How about using an array?

      Dim featured_produc ts(11) ''vbscrip arrays are zero based

      If your storing your months in your database as "jan", "feb", "mar", and so
      on, you'll have to deal with them with a select Case or some other thing.
      Or, use a dictionary object or something. Let's try with a dictionary
      object first.

      Dim oDict
      oDict.Add "jan", 0
      oDict.Add "feb", 1
      oDict.Add "mar", 2
      oDict.Add "apr", 3
      oDict.Add "may", 4
      oDict.Add "jun", 5
      oDict.Add "jul", 6
      oDict.Add "aug", 7
      oDict.Add "sep", 8
      oDict.Add "oct", 9
      oDict.Add "nov", 10
      oDict.Add "dec", 11


      While Not r.EOF
      If r("status") = "sold" Then
      featured_produc ts(oDict(r("mon th"))) = "Sold out"
      Else
      ''''etc.


      My mind has been sleeping all week, so if this makes no sense or if I make
      no sense, accept my apologies.

      Ray at work





      "Ken Halley" <ken@alpacanati on.com> wrote in message
      news:ORf0fDKpDH A.2592@TK2MSFTN GP10.phx.gbl...[color=blue]
      > How does one refer to a variable name using a variable within a variable
      > name??? For example, I want to perform a SQL query and use the results of
      > the query to determine which variable to assign a value to. Let's say[/color]
      I've[color=blue]
      > dimmed 12 variables based on the months called "featured_x_pro duct" where[/color]
      x[color=blue]
      > is the month. I'd like to refer to the variable with something
      > like: featured_" & R("month") & "_product where R("month") is the from the
      > current row in the db. I don't know the correct syntax or method for[/color]
      doing[color=blue]
      > so. A full example is provided below. Thanks in advance for you help!
      >
      > dim featured_jan_pr oduct
      > dim featured_feb_pr oduct
      > dim featured_mar_pr oduct
      > dim featured_apr_pr oduct
      > ...
      >
      > SELECT status, month from products where year='2004' AND
      > page='featured_ products'
      >
      > While not R.EOF
      > If R("status") = "sold" Then
      > featured_" & R("month") & "_product = "Sold Out!" // (choose the
      > variable based on the "month" value in the current row)
      > Else
      > featured_" & R("month") & "_product = "Buy Now!"
      > End If
      > R.MoveNext
      > Wend
      >
      >
      >
      >[/color]


      Comment

      • Ken Halley

        #4
        Re: Dynamic Variable Names - Syntax?

        Thanks Ray. The situation is a little more complicated than my example in
        that I have two pieces of the variable name that I want to make dynamic, the
        month and the category (see below). So the single array idea doesn't solve
        it. Any other thoughts?

        The variable format is: category_month_ color

        R("category") & "_" & R("month") & "_color = "#0000FF"



        "Ray at <%=sLocation% >" <myfirstname at lane34 dot com> wrote in message
        news:Oj52iMKpDH A.2584@TK2MSFTN GP09.phx.gbl...[color=blue]
        > You can use the Eval function to do this, but I suggest not using Eval.[/color]
        It[color=blue]
        > makes for the most confusing and sloppy code you'll ever see.
        >
        > How about using an array?
        >
        > Dim featured_produc ts(11) ''vbscrip arrays are zero based
        >
        > If your storing your months in your database as "jan", "feb", "mar", and[/color]
        so[color=blue]
        > on, you'll have to deal with them with a select Case or some other thing.
        > Or, use a dictionary object or something. Let's try with a dictionary
        > object first.
        >
        > Dim oDict
        > oDict.Add "jan", 0
        > oDict.Add "feb", 1
        > oDict.Add "mar", 2
        > oDict.Add "apr", 3
        > oDict.Add "may", 4
        > oDict.Add "jun", 5
        > oDict.Add "jul", 6
        > oDict.Add "aug", 7
        > oDict.Add "sep", 8
        > oDict.Add "oct", 9
        > oDict.Add "nov", 10
        > oDict.Add "dec", 11
        >
        >
        > While Not r.EOF
        > If r("status") = "sold" Then
        > featured_produc ts(oDict(r("mon th"))) = "Sold out"
        > Else
        > ''''etc.
        >
        >
        > My mind has been sleeping all week, so if this makes no sense or if I make
        > no sense, accept my apologies.
        >
        > Ray at work
        >
        >
        >
        >
        >
        > "Ken Halley" <ken@alpacanati on.com> wrote in message
        > news:ORf0fDKpDH A.2592@TK2MSFTN GP10.phx.gbl...[color=green]
        > > How does one refer to a variable name using a variable within a variable
        > > name??? For example, I want to perform a SQL query and use the results[/color][/color]
        of[color=blue][color=green]
        > > the query to determine which variable to assign a value to. Let's say[/color]
        > I've[color=green]
        > > dimmed 12 variables based on the months called "featured_x_pro duct"[/color][/color]
        where[color=blue]
        > x[color=green]
        > > is the month. I'd like to refer to the variable with something
        > > like: featured_" & R("month") & "_product where R("month") is the from[/color][/color]
        the[color=blue][color=green]
        > > current row in the db. I don't know the correct syntax or method for[/color]
        > doing[color=green]
        > > so. A full example is provided below. Thanks in advance for you help!
        > >
        > > dim featured_jan_pr oduct
        > > dim featured_feb_pr oduct
        > > dim featured_mar_pr oduct
        > > dim featured_apr_pr oduct
        > > ...
        > >
        > > SELECT status, month from products where year='2004' AND
        > > page='featured_ products'
        > >
        > > While not R.EOF
        > > If R("status") = "sold" Then
        > > featured_" & R("month") & "_product = "Sold Out!" // (choose[/color][/color]
        the[color=blue][color=green]
        > > variable based on the "month" value in the current row)
        > > Else
        > > featured_" & R("month") & "_product = "Buy Now!"
        > > End If
        > > R.MoveNext
        > > Wend
        > >
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Aaron Bertrand - MVP

          #5
          Re: Dynamic Variable Names - Syntax?

          > month and the category (see below). So the single array idea doesn't
          solve[color=blue]
          > it. Any other thoughts?[/color]

          Uh, a two dimensional array?

          dim arrayName(<<<?> >>, 2)
          arrayName(0, 0) = R("category")
          arrayName(0, 1) = R("month")
          arrayName(0, 2) = "#0000FF"

          Dictionary objects can also hold complex types like arrays or even other
          dictionary objects. I really think others are on the right track when they
          suggest staying away from eval/exec... I just offered it as a possible
          alternative.


          Comment

          • Ray at

            #6
            Re: Dynamic Variable Names - Syntax?

            I strongly suggest you find another approach that involves arrays or
            something else. If you write a page that is laden with Evals and Execs,
            it's going to be impossible to understand a week later when you look at it.
            This is just my opinion.

            Ray at home

            "Ken Halley" <ken@alpacanati on.com> wrote in message
            news:OP1fNhNpDH A.688@TK2MSFTNG P10.phx.gbl...[color=blue]
            > Thanks Ray. The situation is a little more complicated than my example in
            > that I have two pieces of the variable name that I want to make dynamic,[/color]
            the[color=blue]
            > month and the category (see below). So the single array idea doesn't[/color]
            solve[color=blue]
            > it. Any other thoughts?
            >
            > The variable format is: category_month_ color
            >
            > R("category") & "_" & R("month") & "_color = "#0000FF"
            >[/color]


            Comment

            Working...