Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB6
Expertise: Intermediate
Jun 10, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Export to a text file with quoted fields

As explained in another item in this Tip Bank, you can use the Recordset's GetString method to export a Recordset to a comma-delimited or tab-delimited text file. However, if you are exporting string fields that might include commas or tabs, you should include the fields' value within quotes. At a first glance, this seems to rule out the GetString method, but you can resort to the following trick:

Dim rs As New ADODB.Recordset, tmp As String
rs.Open "authors", "DSN=pubs", adOpenForwardOnly, , adCmdTable

Open "c:\authors.dat" For Output As #1

' output the opening quotes for the first
' field of the first row
Print #1, """"

' output the rest of the recordset
Do Until rs.EOF
    ' blocks of 100 rows each
    tmp = rs.GetString(, 100, """,""", """" & vbCrLf & """", "")

    If rs.EOF Then
        ' drop the double quote char printed in excess
        tmp = Left$(tmp, Len(tmp) - 1)
    End If
    Print #1, tmp;
Close #1

Unfortunately, you can't decide which fields are to be quoted, so this is an all-or-nothing decision.
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date