Exporting a DataGrid to a CSV File

Along the same lines as my posts on exporting a SalesLogix DataGrid (well, exporting the Recordset behind the grid) to Excel, Michael Cessna from SalesLogix shared this quick bit of code for dumping a Recordset to a CSV file (posted in the SalesLogix Business Partner newsgroups)

Option Explicit

Const adClipString = 2

' ExportRecordsetToCSV objRS, "C:Test.csv", True, True
Sub ExportRecordsetToCSV(Recordset, FileName, IncludeFieldNames, MoveFirst)

    Dim objFSO
    Dim objFile
    Dim iFieldCount
    Dim oFields()
    Dim strHeader

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.CreateTextFile(FileName, True)

    If MoveFirst Then
       Recordset.MoveFirst
    End If

    If IncludeFieldNames Then
       Redim oFields(Recordset.Fields.Count)
       For iFieldCount = 0 To Recordset.Fields.Count -1
           oFields(iFieldCount) = Recordset.Fields(iFieldCount).Name
       Next
       strHeader = Join(oFields, Chr(9))
       objFile.WriteLine(strHeader)
    End If

    objFile.Write(Recordset.GetString(adClipString))
    objFile.Close()

    Set objFile = Nothing
    Set objFSO = Nothing

    If MoveFirst Then
       Recordset.MoveFirst
    End If
End Sub

ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

1 Comment

  1. Doh! I knew that. I guess my mind just gets tripped up moving back and forth between legecy and VB Script..

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) and Creatio (bpm'online) news and product updates!

You have Successfully Subscribed!