VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Export sql data to a CSV File

by Waty Thierry (60 Submissions)
Category: Databases/Data Access/DAO/ADO
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Tue 30th March 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Export sql data to a CSV File

Rate Export sql data to a CSV File



   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site         : www.geocities.com/ResearchTriangle/6311/
   ' * E-Mail           : [email protected]
   ' * Date             : 25/11/98
   ' * Time             : 15:06
   ' * Module Name      : Database_Module
   ' * Module Filename  : Database.bas
   ' * Procedure Name   : CSVExport
   ' * Parameters       :
   ' *                    db As DAO.Database
   ' *                    sSQL As String
   ' *                    sDest As String
   ' **********************************************************************
   ' * Comments         : Export sql data to a CSV File
   ' *
   ' *
   ' **********************************************************************

   Dim record        As Recordset
   Dim nI            As Long
   Dim nJ            As Long
   Dim nFile         As Integer
   Dim sTmp          As String
   
   On Error GoTo Err_Handler
   
   Set record = db.OpenRecordset(sSQL, DAO.dbOpenDynaset, DAO.dbReadOnly)
   
   ' *** Open output file
   nFile = FreeFile
   
   Open sDest For Output As #nFile
   
   ' *** Export fields name
   For nI = 0 To record.Fields.Count - 1
      sTmp = "" & (record.Fields(nI).Name)
      Write #nFile, sTmp;
   Next
   Write #nFile,

   If record.RecordCount > 0 Then
      record.MoveLast
      record.MoveFirst
   
      For nI = 1 To record.RecordCount
         For nJ = 0 To record.Fields.Count - 1
            sTmp = "" & (record.Fields(nJ))
            Write #nFile, sTmp;
         Next
         Write #nFile,
         record.MoveNext
      Next
   End If
   
   Close #nFile
   CSVExport = True
   
   Exit Function
   
Err_Handler:
   MsgBox ("Error: " & Err.Description)

   CSVExport = False
   
End Function


Download this snippet    Add to My Saved Code

Export sql data to a CSV File Comments

No comments have been posted about Export sql data to a CSV File. Why not be the first to post a comment about Export sql data to a CSV File.

Post your comment

Subject:
Message:
0/1000 characters