Connecting Tech Pros Worldwide Help | Site Map

Help on exporting records to csv file

Newbie
 
Join Date: Sep 2009
Posts: 3
#1: Sep 18 '09
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.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <title>ECVS Report Generator</title>
  3. <head>
  4.  
  5. <script>
  6. function clearForms()
  7. {
  8. var i;
  9. for (i = 0; (i < document.forms.length); i++) {
  10. document.forms[i].reset();
  11. }
  12. }
  13. </script>
  14. </head>
  15. <body bgcolor="#FFFFFF" onLoad="clearForms()" onUnload="clearForms()" background="http://bytes.com/images/background1.png">
  16. <%
  17.  
  18.  
  19.  
  20. u_input1 = trim(request.form("u_input1"))
  21. u_input2 = trim(request.form("u_input2"))
  22. u_input3 = trim(request.form("u_input3"))
  23.  
  24. u_field1=request.form("u_field1")
  25. u_field2=request.form("u_field2")
  26. u_field3=request.form("u_field3")
  27.  
  28. if u_input1 <> "" then
  29.  
  30. accessdb="custmast" '
  31. table_name="customermast"
  32. cn="driver={microsoft access driver (*.mdb)};"
  33. cn=cn & "dbq=" & server.mappath(accessdb)
  34. set rs = server.createobject("ADODB.Recordset")
  35. sql = "select * from "& table_name &" where " & u_field1 & " like '%%" & u_input1 & "%%' And " & u_field2 & " like '%%" & u_input2 & "%%' And " & u_field3 &" like '%%" & u_input3 & "%%' "
  36.  
  37.  
  38. rs.Open sql, cn
  39.  
  40. if rs.eof or rs.bof then
  41. response.write "No results found..."
  42. observations=0
  43. else
  44. observations=1
  45. end if 'end check for observations
  46. end if 'end check for user input
  47. %>
  48.  
  49.  
  50.  
  51.  
  52. <%
  53. if observations > 0 then %>
  54.  
  55. <!--- This is where it specifies what to do if there are observations ---->
  56. <table>
  57. <tr>
  58. <% 'Write the field names
  59. for each table_element in rs.fields
  60. %>
  61.  
  62. <td><b><%= table_element.name%></B></TD><%
  63. next %>
  64. </tr>
  65. <tr>
  66. <% 'Write the values
  67. rs.movefirst
  68. do while not rs.eof
  69. for each cell in rs.fields %>
  70. <td><%= cell.value %></td><%
  71. next %>
  72. </tr>
  73. <% rs.movenext
  74. loop %>
  75. </table>
  76.  
  77.  
  78. <% end if 'end of check of obs for display%>
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. <br>
  90. <br>
  91. <br>
  92. <br>
  93. <br>
  94. <br>
  95. <br>
  96. <table align="center">
  97. <tr>
  98. <td>
  99. <form action ="" method="post">
  100.  
  101. <select name="u_field1" size="1">
  102. <option <% ' write out all the search fields and select
  103. if u_field1 = "state" or u_field1 = "" then
  104. response.write "selected "
  105. end if
  106. %>value="AccountNumber">Account Number</option>
  107. <option <%
  108. if u_field1 = "AccountName" then
  109. response.write "selected "
  110. end if
  111. %>value="AccountName">Account Name</option>
  112. <option <%
  113. if u_field1 = "SalesVolume" then
  114. response.write "selected "
  115. end if
  116. %>value="SalesVolume">Sales Volume</option>
  117. <option <%
  118. if u_field1 = "BusinessUnit" then
  119. response.write "selected "
  120. end if
  121. %>value="BusinessUnit">Business Unit</option><br>
  122. <option <%
  123. if u_field1 = "SalesRegion" then
  124. response.write "selected "
  125. end if
  126. %>value="SalesRegion">Sales Region</option>
  127. <option <%
  128. if u_field1 = "State" then
  129. response.write "selected "
  130. end if
  131. %>value="state">State</option>
  132. <option <%
  133. if u_field1 = "PriceGroup" then
  134. response.write "selected "
  135. end if
  136. %>value="PriceGroup">Price Group</option>
  137. <option <%
  138. if u_field1 = "DealerType" then
  139. response.write "selected "
  140. end if
  141. %>value="DealerType">Dealer Type</option>
  142. <option <%
  143. if u_field1 = "BillingType" then
  144. response.write "selected "
  145. end if
  146. %>value="BillingType">Address Type</option>
  147. <option <%
  148. if u_field1 = "StartDate" then
  149. response.write "selected "
  150. end if
  151. %>value="StartDate">Start Date</option>
  152. <option <%
  153. if u_field1 = "AccountRep" then
  154. response.write "selected "
  155. end if
  156. %>value="AccountRep">Account Rep</option>
  157.  
  158. </select>
  159. &nbsp; = &nbsp;
  160. <input type="text" name="u_input1" value="<%= u_input1 %>">
  161. <br><br>
  162. AND
  163. <br><br>
  164.  
  165. <select name="u_field2" size="1">
  166. <option <% ' write out all the search fields and select
  167. if u_field2 = "state" or u_field2 = "" then
  168. response.write "selected "
  169. end if
  170. %>value="AccountNumber">Account Number</option>
  171. <option <%
  172. if u_field2 = "AccountName" then
  173. response.write "selected "
  174. end if
  175. %>value="AccountName">Account Name</option>
  176. <option <%
  177. if u_field2 = "SalesVolume" then
  178. response.write "selected "
  179. end if
  180. %>value="SalesVolume">Sales Volume</option>
  181. <option <%
  182. if u_field2 = "BusinessUnit" then
  183. response.write "selected "
  184. end if
  185. %>value="BusinessUnit">Business Unit</option>
  186. <option <%
  187. if u_field2 = "SalesRegion" then
  188. response.write "selected "
  189. end if
  190. %>value="SalesRegion">Sales Region</option>
  191. <option <%
  192. if u_field2 = "State" then
  193. response.write "selected "
  194. end if
  195. %>value="state">State</option>
  196. <option <%
  197. if u_field2 = "PriceGroup" then
  198. response.write "selected "
  199. end if
  200. %>value="PriceGroup">Price Group</option>
  201. <option <%
  202. if u_field2 = "DealerType" then
  203. response.write "selected "
  204. end if
  205. %>value="DealerType">Dealer Type</option>
  206. <option <%
  207. if u_field2 = "BillingType" then
  208. response.write "selected "
  209. end if
  210. %>value="BillingType">Address Type</option>
  211. <option <%
  212. if u_field2 = "StartDate" then
  213. response.write "selected "
  214. end if
  215. %>value="StartDate">Start Date</option>
  216. <option <%
  217. if u_field2 = "AccountRep" then
  218. response.write "selected "
  219. end if
  220. %>value="AccountRep">Account Rep</option>
  221. </select>
  222.  
  223. &nbsp; = &nbsp;
  224.  
  225. <input type="text" name="u_input2" value="<%= u_input2 %>">
  226. <br><br>
  227. AND
  228. <br><br>
  229.  
  230. <select name="u_field3" size="1">
  231. <option <% ' write out all the search fields and select
  232. if u_field3 = "state" or u_field3 = "" then
  233. response.write "selected "
  234. end if
  235. %>value="AccountNumber">Account Number</option>
  236. <option <%
  237. if u_field3 = "AccountName" then
  238. response.write "selected "
  239. end if
  240. %>value="AccountName">Account Name</option>
  241. <option <%
  242. if u_field3 = "SalesVolume" then
  243. response.write "selected "
  244. end if
  245. %>value="SalesVolume">Sales Volume</option>
  246. <option <%
  247. if u_field3 = "BusinessUnit" then
  248. response.write "selected "
  249. end if
  250. %>value="BusinessUnit">Business Unit</option>
  251. <option <%
  252. if u_field3 = "SalesRegion" then
  253. response.write "selected "
  254. end if
  255. %>value="SalesRegion">Sales Region</option>
  256. <option <%
  257. if u_field3 = "State" then
  258. response.write "selected "
  259. end if
  260. %>value="state">State</option>
  261. <option <%
  262. if u_field3 = "PriceGroup" then
  263. response.write "selected "
  264. end if
  265. %>value="PriceGroup">Price Group</option>
  266. <option <%
  267. if u_field3 = "DealerType" then
  268. response.write "selected "
  269. end if
  270. %>value="DealerType">Dealer Type</option>
  271. <option <%
  272. if u_field3 = "BillingType" then
  273. response.write "selected "
  274. end if
  275. %>value="BillingType">Address Type</option>
  276. <option <%
  277. if u_field3 = "StartDate" then
  278. response.write "selected "
  279. end if
  280. %>value="StartDate">Start Date</option>
  281. <option <%
  282. if u_field3 = "AccountRep" then
  283. response.write "selected "
  284. end if
  285. %>value="AccountRep">Account Rep</option>
  286. </select>
  287. &nbsp; = &nbsp;
  288.  
  289. <input type="text" name="u_input3" value="<%= u_input3 %>">
  290. <br><br>
  291. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;
  292. &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;
  293. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  294.  
  295. <input type="submit" value="Submit">
  296. </form>
  297. </td>
  298. </tr>
  299. </table>
  300.  
  301. <p>&nbsp</p>
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#2: Sep 18 '09

re: Help on exporting records to csv file


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
Newbie
 
Join Date: Sep 2009
Posts: 3
#3: Sep 18 '09

re: Help on exporting records to csv file


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 !!
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#4: Sep 19 '09

re: Help on exporting records to csv file


Quote:

Originally Posted by jrnowlan View Post

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:
Expand|Select|Wrap|Line Numbers
  1. columnA,columnB,id
  2. val1a,val1b,1
  3. 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
Reply