VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Shows how to create a .csv file and store all data from a SQL Server Database table.

by karada (6 Submissions)
Category: Databases/Data Access/DAO/ADO
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Fri 17th August 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Shows how to create a .csv file and store all data from a SQL Server Database table.

API Declarations


'Microsoft ActiveX Data Objects 2.1 Object Library &
'Microsoft Excel 8.0

Dim myWo As New Excel.Application
Dim myData As New ADODB.Connection
Dim myRecset As New ADODB.Recordset


Rate Shows how to create a .csv file and store all data from a SQL Server Database table.




Public Function OpenDB(DBPath As String, RecPath As String, CSVFileName As String)
On Error Resume Next
myData.ConnectionString = "Provider=SQLOLEDB;Data source=myservername;UID=SA;pwd=;DataBase=" _
& DBPath

myData.Open

myRecset.CursorLocation = adUseClient
myRecset.LockType = adLockOptimistic
myRecset.CursorType = adOpenDynamic

myRecset.Open RecPath, myData

myWo.Workbooks.Add
myWo.Worksheets.Add
myWo.DisplayAlerts = False
myWo.Visible = False


For fname = 0 To myRecset.Fields.Count - 1
    
    myWo.ActiveCell(1, fname + 1) = myRecset.Fields(fname).Name
    myWo.ActiveCell(1, fname + 1).Font.Bold = True
Next


For i = 1 To myRecset.RecordCount
        For j = 1 To myRecset.Fields.Count
            If IsNull(myRecset.Fields(j)) Then
                x = y
            Else
        myWo.ActiveCell(i + 1, j) = myRecset.Fields(j - 1)
End If

Next j
    myRecset.MoveNext
Next i

myWo.ActiveWorkbook.SaveAs (CSVFileName)
myWo.ActiveWorkbook.Close
End Function

Private Sub Form_Load()
Call Opendb("pubs", "titles", "D:\karada.csv")
End Sub


Download this snippet    Add to My Saved Code

Shows how to create a .csv file and store all data from a SQL Server Database table. Comments

No comments have been posted about Shows how to create a .csv file and store all data from a SQL Server Database table.. Why not be the first to post a comment about Shows how to create a .csv file and store all data from a SQL Server Database table..

Post your comment

Subject:
Message:
0/1000 characters