« ColdFusion spellchecker plugin for TinyMCE 3.0 | Web standards awareness among CF developers »

April 3, 2008

ColdFusion tip: Dynamic column names in ValueList()

Today I discovered that the ValueList() function in ColdFusion only works with a static argument in the form "query.column". So you can't use this function if you need to dynamically determine the column name at runtime. Luckily there is a nice little hack to work around this problem.

The ValueList() function returns a list of values from a particular query column. You can achieve the same result with the following expression:

ArrayToList(query[column])

Posted at 3:41 PM in ColdFusion

Comments

1. Ferg says:

Thank you thank you thank you!!!

Posted on September 8, 2008 at 1:07 PM

2. Marker says:

Looking for this for a long time, can't understand why Adobe does not fix this issue...
But it doesn't work for me... should this also work with QueriesOfQueries?


I'm passing a cf-query with 2 columns:
Branche_id and Branche_Name


qTempTable= myQuery
thisIDField= the Column

The Table has 6 rows.

I get the following Error:

[Table (rows 6 columns BRANCHE_ID, BRANCHE_NAME): [BRANCHE_ID: coldfusion.sql.QueryColumn@e0726f] [BRANCHE_NAME: coldfusion.sql.QueryColumn@178ec6d] ] is not indexable by BRANCHE_ID

just don't get it :(

thx in advance
marker

Posted on September 18, 2008 at 5:40 PM

3. Marker says:

Hmm, can't see how I use the function, here it is again...

ArrayToList(qTempTable[thisIDField])

Posted on September 18, 2008 at 5:43 PM

4. Richard Davies says:

Marker,

Does this work?

ArrayToList(qTempTable["BRANCHE_ID"])

I know it defeats the purpose of using ArrayToList, but it might help us debug the problem.

Posted on September 20, 2008 at 5:00 PM

5. Todd says:

AWESOME!!!!! Could have been using this for YEARS now!!!! better late than never ;)

Posted on October 23, 2008 at 5:44 PM

6. pseudoart says:

Thanks a lot - I should've thought of that, but luckily a google search on "coldfusion valuelist query" put this page in 5th place. :P

You've just saved me (or rather, my code) from being sloppy. ;)

Btw, this worked fine for me:

ArrayToList(attributes.selectedQuery[attributes.idRow])

Posted on January 1, 2009 at 4:18 PM

7. Danny says:

And does anyone know why this works? Kind of makes my head spin. Of course doing something like a valuelist that returns an array of values would be nice.

Posted on March 21, 2009 at 7:29 PM

8. Jonathan says:

It's been a while since you wrote this post, but I wanted to say thank you! CF has its share of quirks and I was relieve to find an elegant solution to a code problem.

Posted on December 14, 2009 at 2:42 PM

9. Gary says:

Nice tip, thanks!

Posted on August 15, 2012 at 10:08 AM