Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:22:15+00:00 2026-06-12T11:22:15+00:00

I am trying to use a query result from a dynamic search in my

  • 0

I am trying to use a query result from a dynamic search in my webpage by accessing it with another button that will export the result to a comma delimited file. However, the export button won’t recognize that the query exists for some reason. I keep getting an error that says: The variable QUERY is undefined.

What I am trying to do is make it so that when the user hits the export button it will save the search in a comma delimited file. Right now I am using a function that will save the search in a variable called strOutput in comma delimited form. Then I was going to write this to a file.

Since it wouldn’t recognize the query I tried setting the variable inside the search function and using a cfparam tag at the top of the page so I could access it inside the export function. This did not work either. Maybe there is a better way to do this?

Here is what the code looks like:

This is the beginning of my search function:

<cfif structKeyExists(Form, "submit")>

<cfquery datasource="#Session.HousingDataSource#" name="query">
        SELECT *

This code is placed after the above code that runs a query and displays the results.

<cfif structKeyExists(Form,"export")>
     <cfset strOutput = QueryToCSV(
          query,"studentFirst,studentLast,studentNumber,Detail1,itemDate") />

<!---cffile
  action="WRITE"
  file="#filename#"
  output="#strOutput#"
/--->


</cfif>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:22:17+00:00Added an answer on June 12, 2026 at 11:22 am

    The query would need to be present in both cases as the query will not be remembered from one request to another… In the following example the query may not have been created when the second request is made.

    <cfif StructKeyExists(form, "submit")>
        <!--- This only gets run on the first request --->
        <cfquery datasource="#session.HousingDataSource#" name="query">
            ...
        </cfquery>
    </cfif>
    
    <cfif StructKeyExists(form, "export")>
        <!--- This only gets run on the second request --->
        <cfset output = QueryToCSV(query, "studentFirst,studentLast,studentNumber,Detail1,itemDate")/>
        <cffile action="write" file="#filename#" output="#output#"/>
    </cfif>
    

    If you update the code as follows then when you export it will run the query again to export it…

    <cfif StructKeyExists(form, "submit") || StructKeyExists(form, "export")>
        <!--- This gets run both times --->
        <cfquery datasource="#session.HousingDataSource#" name="query">
            ...
        </cfquery>
    </cfif>
    
    <cfif StructKeyExists(form, "export")>
        <cfset output = QueryToCSV(query, "studentFirst,studentLast,studentNumber,Detail1,itemDate")/>
        <cffile action="write" file="#filename#" output="#output#"/>
    </cfif>
    

    Or you can save it in the session…

    <cfif StructKeyExists(form, "submit")>
        <cfquery datasource="#session.HousingDataSource#" name="query">
            ...
        </cfquery>
        <cfset session.query = query/>
    </cfif>
    
    <cfif StructKeyExists(form, "export") && StructKeyExists(session, "query")>
        <cfset output = QueryToCSV(session.query, "studentFirst,studentLast,studentNumber,Detail1,itemDate")/>
        <cffile action="write" file="#filename#" output="#output#"/>
    </cfif>
    

    Or the application scope.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use a result from one query as input for another
I am trying to use a dynamic linq query to retrieve an IEnumerable<T> from
I'm trying to use LINQ To Objects to create a query that will give
I am trying to use some query result to generate another query and execute
I'm trying to use the advanced query syntax as linked in the Search documentation.
I'm trying to use Google Analytics API to query internal searches that happen on
I'm trying to use SSIS to extract XML representation of a query result set
i'm having some trouble trying to use a query result as an array First
I'm trying to use the result of one query as part of a WHERE
I have as a result from a dynamic query a table with only one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.