?
Solved

VB write data to csv

Posted on 2012-03-13
17
Medium Priority
?
271 Views
Last Modified: 2012-03-14
I am trying to take records from an MS Access query in my windows application and create a csv with the records. Here is the code I have so far.

Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & GetPath() & "\" & DBName
        conn.Open()
        Dim csv As String = "\Paryoll.csv"
        Dim tw = System.IO.StreamWriter(GetPath() & csv)
        Dim sql = "SELECT * FROM qry_EarningsOT"
        Dim consql As New SqlCommand
        consql.CommandText = sql
        consql.Connection = conn
        Dim datRead As SqlDataReader
        datRead = consql.ExecuteReader
        Do Until datRead.Read = False
            tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
                         " , " & datRead.GetDouble(3) & " , " & datRead.GetString(4) & " , " & datRead.GetDouble(5))
        Loop
        tw.Close()
        conn.Close()
0
Comment
Question by:rawilken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
17 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 37718551
And what exactly is your question ? Isn't it working or do you get an error somewhere ?
0
 
LVL 9

Accepted Solution

by:
chwong67 earned 1050 total points
ID: 37718847
I assume the database is MS Access.

Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & getpath() & "\" & DBName
        conn.Open()
        Dim csv As String = "\Paryoll.csv"
        Dim tw As New System.IO.StreamWriter(getpath() & csv)
        Dim sql = "SELECT * FROM qry_EarningsOT"
        Dim consql As New OleDbCommand
        consql.CommandText = sql
        consql.Connection = conn
        Dim datRead As OleDbDataReader
        datRead = consql.ExecuteReader
        Do Until datRead.Read = False

            tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
                         " , " & datRead.GetDouble(3) & " , " & datRead.GetString(4) & " , " & datRead.GetDouble(5))
        Loop
        tw.Close()
        conn.Close()

Open in new window

0
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 

Author Comment

by:rawilken
ID: 37719963
There error I get is that the streamwriter is a type and cannot be used as an expression...

 Public Function PrintDate(strExport As String) As Boolean
        Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & GetPath() & "\" & DBName
        conn.Open()
        Dim csv As String = "\Paryoll.csv"
        Dim tw = System.IO.StreamWriter(GetPath() & csv)
        Dim sql = "SELECT * FROM qry_EarningsOT"
        Dim consql As New OleDb.OleDbCommand
        consql.CommandText = sql
        consql.Connection = conn
        Dim datRead As OleDb.OleDbDataReader
        datRead = consql.ExecuteReader
        Do Until datRead.Read = False
            tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
                         " , " & datRead.GetDouble(3) & " , " & datRead.GetString(4) & " , " & datRead.GetDouble(5))
        Loop
        tw.Close()
        conn.Close()
    End Function
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37720057
I think you forgot a NEW:

Dim tw  AS NEW System.IO.StreamWriter(GetPath() & csv)
0
 

Author Comment

by:rawilken
ID: 37720264
Error Message...

Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
    'Public Sub New(path As String)': Argument matching parameter 'path' narrows from 'Object' to 'String'.
    'Public Sub New(stream As System.IO.Stream)': Argument matching parameter 'stream' narrows from 'Object' to 'System.IO.Stream'.      C:\Data Control\Time and Pay\Time_and_Pay\Time_and_Pay\TimeAndPay.vb      84      18      Time_and_Pay
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37720273
0
 

Author Comment

by:rawilken
ID: 37720738
I get no obvious error messages with the code written this way..

Dim tw As StreamWriter
tw = (GetPath() & csv)

I still need to test it though.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37720834
just try:

Dim tw As System.IO.TextWriter = New StreamWriter(GetPath() & csv)
0
 

Author Comment

by:rawilken
ID: 37721223
Two things..
1 I assume this overwrites the existing csv file if there is one
2 How do I append another dataset to the same csv
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37721328
0
 

Author Comment

by:rawilken
ID: 37721682
Error thrown...
error-1.JPG
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37721717
one of the value supposed to be double (fields 3 and 5) is not valid.

try this:
dim f3 as double
double.tryparse(datRead.getdouble(3).tostring, f3)

dim f5 as double
double.tryparse(datRead.getdouble(5).tostring, f5)

and use f3 and f5 in your .WriteLine
0
 

Author Comment

by:rawilken
ID: 37721778
Specified cast on f5 is not valid

Do Until datRead.Read = False
            Dim f3 As Double
            Double.TryParse(datRead.GetDouble(3).ToString, f3)
            Dim f5 As Double
            Double.TryParse(datRead.GetDouble(5).ToString, f5)
            tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
                         " , " & datRead.GetDouble(f3) & " , " & datRead.GetString(4) & " , " & datRead.GetDouble(f5))
        Loop

That field is a double, currency format in my database.
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 450 total points
ID: 37721884
change to :

 tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
                         " , " & f3 & " , " & datRead.GetString(4) & " , " & f5)
0
 

Author Comment

by:rawilken
ID: 37722044
Same error message...
Speficied case is not valid.
Double.TryParse(datRead.GetDouble(5).ToString, f5)
0
 

Author Closing Comment

by:rawilken
ID: 37722443
I also had to modify for null values where they existed.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question