dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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;
Loop
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.

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date