I wrote this function when I needed to quickly generate Excel compatible
text files. It does what you need, and with only a little adjustment can add
extra things like headings.

Function generateCSVFileFromSql(fileName, strSql)
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    NewCSVFile = CreateUniqueFileName(fileName, "/write")
    path = Server.MapPath("/write") & "\" & NewCSVFile
    set file = fso.opentextfile(path, 2, TRUE)
    Set objRec = dbConnection.Execute(strSql)
    While objRec.EOF = False
        For Counter = 0 To objRec.Fields.Count - 1
            file.write objRec.Fields.Item(Counter)
            If Counter < objRec.Fields.Count Then
                file.Write ","
            End If
        Next
        file.writeLine
        objRec.MoveNext
    Wend
    Set objRec = Nothing
    file.close
    set file = nothing
    set fso = nothing
    generateTextFile = NewCSVFile
End Function


Scott
----- Original Message -----
From: "Jason Burton" <[EMAIL PROTECTED]>
To: "ActiveServerPages" <[EMAIL PROTECTED]>
Sent: Wednesday, October 09, 2002 12:26 PM
Subject: Formatted database page to text file ...


> Does anyone have any idea how I could go about
> for example..
>
> Calling a database query.. (results)
> then writing the results to a text file via ASP using
> the File system object? Im having problems with the
> loop process. Could someone give me some advice?
>
> Id like to format a text file, then have it
> automatically spit the results per the way I want..
> like
>
> "Message....   These are your records.. There are
> <record count>. " .. Then list all the records.. But
> with HTML support. etc.
>
> Without purchasing an expensive component Id just
> really like to know how to write to a file using the
> file system object.. (with the loop so the database
> query can show in the file.)
>
> Thanks, lemme know.
>
> Jason
>
>
> __________________________________________________
> Do you Yahoo!?
> Faith Hill - Exclusive Performances, Videos & More
> http://faith.yahoo.com
>
> ---
> You are currently subscribed to activeserverpages as:
[EMAIL PROTECTED]
> To unsubscribe send a blank email to
%%email.unsub%%
>

__________________________________________________

Do you Yahoo!?

Faith Hill - Exclusive Performances, Videos & More

http://faith.yahoo.com


---
You are currently subscribed to activeserverpages as: [email protected]
To unsubscribe send a blank email to [EMAIL PROTECTED]

Reply via email to