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


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Oct 29, 1998

Export SQL Data to a Comma-Separated Value File

Use this code to create a comma-separated value (CSV) file from a recordset based on a SQL query. A number of applications, including Excel and Access, can read CSV files:

 Function CSVExport(sSQL As String, sDest As _
	String) As Boolean
On Error goto Err_Handler
	Set snpExport = db.OpenRecordset(sSQL, dbOpenSnapshot)
	Open App.Path & "\" & sDest For Output As #1
	For iLoop = 0 To snpExport.Fields.Count _
		 - 1     'Export Field Names 
		sFieldText = "" & _
			(snpExport.Fields(iLoop).Name)
		Write #1, sFieldText;
	Next
	Write #1,
	snpExport.MoveFirst
	Do While snpExport.EOF = False
		For iLoop = 0 To snpExport.Fields.Count - 1
			sFieldText = "" & (snpExport.Fields(iLoop))
			Write #1, sFieldText;
		Next
		Write #1,
		snpExport.MoveNext
	Loop
	CSVExport = True
	Exit Function
Err_Handler
	MSGBOX("Error: " & Err.Description)
	CSVExport=False
End Function
Kevin O'Donnel
 
Comment and Contribute

 

 

 

 

 


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

 

 

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