TPD Crime Statistics Search
<%
'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
'** Veign
'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
'** Copyright (C) 2006 Veign, All rights reserved
'** Authored By: Chris Hanscom
'**
'** You may not rent, lease, display or distribute copies of the Software to
'** others. The Software and the accompanying materials are copyrighted by
'** Veign and contain proprietary information.
'** Unauthorized copying of the Software or accompanying materials even if
'** modified, merged, or included with other software, or of the written materials,
'** is expressly forbidden. You may be held legally responsible for any
'** infringement of intellectual property rights that is caused or encouraged
'** by your failure to abide by the terms of this Agreement.
'**
'**
'** Copyright transfership is only legal binding with expressed written consent
'** from Veign and the Author.
'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
' Usage
'
' Dim oExport
' Set oExport = New cExportToCsv
'
' With oExport
' .IncludeHeaderRow = True
' .SQL = strSQL
' .Filename = "MyExportedFile"
' .ConnectionString = sConnectString
' .ExportToCSV
' End With
' Set oExport = Nothing
'
'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
' Writes all data in current recordset as comma separated value file
'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Const EXP_SEPERATOR = ","
Const EXP_VALUE_DELIMITOR = """"
'** Class Module:
Class cExportToCsv
Private mSQL
Private mFilename
Private mConnectionString
Private mRS
Private mIncludeHeaderRow
'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
'** Properties:
Public Property Get SQL()
SQL = mSQL
End Property
Public Property Let SQL(vData)
mSQL = vData
End Property
Public Property Get ConnectionString()
ConnectionString = mConnectionString
End Property
Public Property Let ConnectionString(vData)
mConnectionString = vData
End Property
Public Property Get Filename()
Filename = mFilename
End Property
Public Property Let Filename(vData)
mFilename = vData
End Property
Public Property Get IncludeHeaderRow()
IncludeHeaderRow = mIncludeHeaderRow
End Property
Public Property Let IncludeHeaderRow(vData)
mIncludeHeaderRow = vData
End Property
Public Sub ExportToCSV()
'Populate the Recordset object with a SQL query
Set mRS = Server.CreateObject("ADODB.Recordset")
mRS.Open mSQL, mConnectionString, adOpenForwardOnly, adLockReadOnly
' Clear out the existing HTTP header information
Response.Buffer = TRUE
Response.Clear
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "inline;filename=" & mFilename & ".csv"
'Check for header row
If mIncludeHeaderRow Then
Response.Write GetHeaderRow & vbCrLf
End If
If Not mRS.EOF Then
Dim fld, sDataRow
Do Until mRS.EOF
'Clear the row
sDataRow = ""
For Each fld In mRS.fields
sDataRow = sDataRow & EXP_VALUE_DELIMITOR & fld.Value & EXP_VALUE_DELIMITOR & EXP_SEPERATOR
Next
'Write out
Response.Write Left(sDataRow, Len(sDataRow)-1) & vbCrLf
mRS.MoveNext
Loop
End If
Response.End
'Cleanup
mRS.Close
Set mRS = Nothing
End Sub
Private Function GetHeaderRow()
Dim fld
Dim retVal
'Build a header row of all fields
For Each fld In mRS.fields
retVal = retVal & EXP_VALUE_DELIMITOR & fld.name & EXP_VALUE_DELIMITOR & EXP_SEPERATOR
Next
'Return the row minus the last seperator
GetHeaderRow = Left(retVal, Len(retVal)-1)
End Function
'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
End Class
%>