Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export an ASP generated query to .csv, or .txt file

Status
Not open for further replies.

ABinBoston

Programmer
Nov 6, 2001
22
US
I have a database of contacts. I have a search page that I can search on multiple criteria, and display the resulting records. Let' s say I searched for contacts located in FL, and had the list on an ASP page...I'd like to click a button and export, or save that recordsetas a .txt file, .csv file or similar. Then the user can use that as a datasource for a ms word mail merge.

Thanks! - AB
 
Ok, call me lazy, but I think this will help. I did the same thing except I pulled two values from a form and wrote them to a file. I'm just going to give you that code, sound good?
'write file
Function WriteToFile(FileName, Contents, Append)
'On Error Resume Next

If Append = True Then
iMode = 8
Else
iMode = 2
End If

Set oFs = Server.CreateObject("Scripting.FileSystemObject")
Set oTextFile = oFs.OpenTextFile(FileName, iMode, True)
oTextFile.WriteLine Contents
oTextFile.Close
Set oTextFile = Nothing
Set oFS = Nothing

End Function

'Implementation
set oFs = Server.CreateObject("Scripting.FileSystemObject")
oFs.CreateTextFile strPathToFile, True
For x = 1 to Request.Form.Count
fieldName = Request.Form.Key(x)
fieldValue = Request.Form.Item(x)
'*** this is where you manually format the output to the file by line
strToFile = fieldName & ", " & fieldValue & ",*"
If fieldValue <> &quot;Ok&quot; Then
WriteToFile strPathToFile, strToFile, True
End If
Next

HAPPY PROGRAMMING
sjuarez1979
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top