Help on exporting records to csv file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrnowlan
    New Member
    • Sep 2009
    • 3

    Help on exporting records to csv file

    I have the following code that takes form data from a picklist and uses the input values to query an access database. Currently everything works, but it writes the results to the same page when submit is selected. I would like (if possible) to write the results to a csv file that could be saved on the users computer. Is this possible? If so can some one give me a little help with this ?
    Here is the code that I have.
    Code:
    <html>
    <title>ECVS Report Generator</title>
    <head>
    
    <script>
    function clearForms()
    {
    var i;
    for (i = 0; (i < document.forms.length); i++) {
    document.forms[i].reset();
    }
    }
    </script>
    </head>
    <body bgcolor="#FFFFFF" onLoad="clearForms()" onUnload="clearForms()" background="http://bytes.com/images/background1.png">
    <%
    
    
    
    u_input1 = trim(request.form("u_input1"))
    u_input2 = trim(request.form("u_input2"))
    u_input3 = trim(request.form("u_input3"))
    
    u_field1=request.form("u_field1")
    u_field2=request.form("u_field2")
    u_field3=request.form("u_field3")
    
    if u_input1 <> "" then
    
    accessdb="custmast" '
    table_name="customermast"
    cn="driver={microsoft access driver (*.mdb)};"
    cn=cn & "dbq=" & server.mappath(accessdb)
    set rs = server.createobject("ADODB.Recordset")
    sql = "select * from "& table_name &" where " & u_field1 & " like '%%" & u_input1 & "%%' And " & u_field2 & " like '%%" & u_input2 & "%%' And " & u_field3 &" like '%%" & u_input3 & "%%' "
    
    
    rs.Open sql, cn
    
    if rs.eof or rs.bof then
    response.write "No results found..."
    observations=0
    else
    observations=1
    end if 'end check for observations
    end if 'end check for user input
    %>
    
    
    
    
    <%
    if observations > 0 then %>
    
    [B]<!--- This is where it specifies what to do if there are observations ---->[/B]
    <table>
    <tr>
    <% 'Write the field names
    for each table_element in rs.fields
    %>
    
    <td><b><%= table_element.name%></B></TD><%
    next %>
    </tr>
    <tr>
    <% 'Write the values
    rs.movefirst
    do while not rs.eof
    for each cell in rs.fields %>
    <td><%= cell.value %></td><%
    next %>
    </tr>
    <% rs.movenext
    loop %>
    </table>
    
    
    <% end if 'end of check of obs for display%>
    
    
    
    
    
    
    
    
    
    
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <table align="center">
    <tr>
    <td>
    <form action ="" method="post">
    
    <select name="u_field1" size="1">
    <option <% ' write out all the search fields and select
    if u_field1 = "state" or u_field1 = "" then
    response.write "selected "
    end if
    %>value="AccountNumber">Account Number</option>
    <option <%
    if u_field1 = "AccountName" then
    response.write "selected "
    end if
    %>value="AccountName">Account Name</option>
    <option <%
    if u_field1 = "SalesVolume" then
    response.write "selected "
    end if
    %>value="SalesVolume">Sales Volume</option>
    <option <%
    if u_field1 = "BusinessUnit" then
    response.write "selected "
    end if
    %>value="BusinessUnit">Business Unit</option><br>
    <option <%
    if u_field1 = "SalesRegion" then
    response.write "selected "
    end if
    %>value="SalesRegion">Sales Region</option>
    <option <%
    if u_field1 = "State" then
    response.write "selected "
    end if
    %>value="state">State</option>
    <option <%
    if u_field1 = "PriceGroup" then
    response.write "selected "
    end if
    %>value="PriceGroup">Price Group</option>
    <option <%
    if u_field1 = "DealerType" then
    response.write "selected "
    end if
    %>value="DealerType">Dealer Type</option>
    <option <%
    if u_field1 = "BillingType" then
    response.write "selected "
    end if
    %>value="BillingType">Address Type</option>
    <option <%
    if u_field1 = "StartDate" then
    response.write "selected "
    end if
    %>value="StartDate">Start Date</option>
    <option <%
    if u_field1 = "AccountRep" then
    response.write "selected "
    end if
    %>value="AccountRep">Account Rep</option>
    
    </select>
    &nbsp; = &nbsp;
    <input type="text" name="u_input1" value="<%= u_input1 %>">
    <br><br>
    AND
    <br><br>
    
    <select name="u_field2" size="1">
    <option <% ' write out all the search fields and select
    if u_field2 = "state" or u_field2 = "" then
    response.write "selected "
    end if
    %>value="AccountNumber">Account Number</option>
    <option <%
    if u_field2 = "AccountName" then
    response.write "selected "
    end if
    %>value="AccountName">Account Name</option>
    <option <%
    if u_field2 = "SalesVolume" then
    response.write "selected "
    end if
    %>value="SalesVolume">Sales Volume</option>
    <option <%
    if u_field2 = "BusinessUnit" then
    response.write "selected "
    end if
    %>value="BusinessUnit">Business Unit</option>
    <option <%
    if u_field2 = "SalesRegion" then
    response.write "selected "
    end if
    %>value="SalesRegion">Sales Region</option>
    <option <%
    if u_field2 = "State" then
    response.write "selected "
    end if
    %>value="state">State</option>
    <option <%
    if u_field2 = "PriceGroup" then
    response.write "selected "
    end if
    %>value="PriceGroup">Price Group</option>
    <option <%
    if u_field2 = "DealerType" then
    response.write "selected "
    end if
    %>value="DealerType">Dealer Type</option>
    <option <%
    if u_field2 = "BillingType" then
    response.write "selected "
    end if
    %>value="BillingType">Address Type</option>
    <option <%
    if u_field2 = "StartDate" then
    response.write "selected "
    end if
    %>value="StartDate">Start Date</option>
    <option <%
    if u_field2 = "AccountRep" then
    response.write "selected "
    end if
    %>value="AccountRep">Account Rep</option>
    </select>
    
    &nbsp; = &nbsp;
    
    <input type="text" name="u_input2" value="<%= u_input2 %>">
    <br><br>
    AND
    <br><br>
    
    <select name="u_field3" size="1">
    <option <% ' write out all the search fields and select
    if u_field3 = "state" or u_field3 = "" then
    response.write "selected "
    end if
    %>value="AccountNumber">Account Number</option>
    <option <%
    if u_field3 = "AccountName" then
    response.write "selected "
    end if
    %>value="AccountName">Account Name</option>
    <option <%
    if u_field3 = "SalesVolume" then
    response.write "selected "
    end if
    %>value="SalesVolume">Sales Volume</option>
    <option <%
    if u_field3 = "BusinessUnit" then
    response.write "selected "
    end if
    %>value="BusinessUnit">Business Unit</option>
    <option <%
    if u_field3 = "SalesRegion" then
    response.write "selected "
    end if
    %>value="SalesRegion">Sales Region</option>
    <option <%
    if u_field3 = "State" then
    response.write "selected "
    end if
    %>value="state">State</option>
    <option <%
    if u_field3 = "PriceGroup" then
    response.write "selected "
    end if
    %>value="PriceGroup">Price Group</option>
    <option <%
    if u_field3 = "DealerType" then
    response.write "selected "
    end if
    %>value="DealerType">Dealer Type</option>
    <option <%
    if u_field3 = "BillingType" then
    response.write "selected "
    end if
    %>value="BillingType">Address Type</option>
    <option <%
    if u_field3 = "StartDate" then
    response.write "selected "
    end if
    %>value="StartDate">Start Date</option>
    <option <%
    if u_field3 = "AccountRep" then
    response.write "selected "
    end if
    %>value="AccountRep">Account Rep</option>
    </select>
    &nbsp; = &nbsp;
    
    <input type="text" name="u_input3" value="<%= u_input3 %>">
    <br><br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    
    <input type="submit" value="Submit">
    </form>
    </td>
    </tr>
    </table>
    
    <p>&nbsp</p>
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    It is possible to return a CSV file rather than an HTML page after the user hits submit. Whether the user saves the file must be left to him. Let me know if that's good enough for you, if it is, I will try to help with the code.

    Jared

    Comment

    • jrnowlan
      New Member
      • Sep 2009
      • 3

      #3
      Thanks for the response. That would be great. I was hoping to pop up a dialog box for the user to select save file or something similar to downloading a file from a site. Any help would be greatly appreciated !!

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        Originally posted by jrnowlan
        Thanks for the response. That would be great. I was hoping to pop up a dialog box for the user to select save file or something similar to downloading a file from a site. Any help would be greatly appreciated !!
        OK, a CSV file is just a text file that might look like this:
        Code:
        columnA,columnB,id
        val1a,val1b,1
        val2a,val2b,2
        So the first step is to be able to put all of the data you have in a string formatted as a CSV file. Can you do that yet?

        Jared

        Comment

        Working...