Results 1 to 11 of 11

Thread: [RESOLVED] mdb database to csv file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Resolved [RESOLVED] mdb database to csv file

    My program needs to export its database to a csv file. Ive done this in .net I used the listview as the source of the csv file. But I cant seem to find a way to do this in VB6.

    Any way is acceptable, I just really need to export my .mdb to .csv file on button click

    Thanks a lot!

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: mdb database to csv file

    VB6 also has a listview, try searching for exporting data from listview to excel.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: mdb database to csv file

    Where you say '.mdb' do you mean every Table in the Database or just one particular Table?. If the latter, I don't see the need for a ListView
    Code:
    Private Sub Command1_Click()
    '
    ' Assumes an Open ADODB Connection named 'con'
    '
    Dim rs As ADODB.Recordset
    Dim strOut As String
    Dim strSQL As String
    Dim intFile As Integer
    Dim strTable As String
    Dim strFile As String
    Dim lngI As Long
    Set rs = New ADODB.Recordset
    strTable = "MyTable"                         'this is the table to export to .csv
    strFile = "C:\MyDir\" & strTable & ".csv"    'this is the file to be created
    intFile = FreeFile
    Open strFile For Output As intFile
    strSQL = "SELECT * FROM " & "[" & strTable & "]"
    rs.Open strSQL, con, adOpenStatic, adLockOptimistic
    If Not (rs.EOF And rs.BOF) Then
        '
        'if you don't want the column names as the first record in the .csv file
        ' then remove this code
        '
        For lngI = 0 To rs.Fields.Count - 1
            strOut = strOut & rs.Fields(lngI).Name & ","
        Next lngI
        strOut = Left$(strOut, Len(strOut) - 1)
        Print #intFile, strOut
        strOut = vbNullString
        '
        ' end of column name output code
        '
        Do
            For lngI = 0 To rs.Fields.Count - 1
                strOut = strOut & rs.Fields(lngI).Value & ","
            Next lngI
            strOut = Left$(strOut, Len(strOut) - 1)
            Print #intFile, strOut
            strOut = vbNullString
            rs.MoveNext
        Loop Until rs.EOF
        MsgBox "Table " & strTable & " has been exported to " & strFile
    Else
        MsgBox "Table " & strTable & " is Empty"
    End If
    Close intFile
    rs.Close
    Set rs = Nothing
    End Sub
    Last edited by Doogle; Apr 25th, 2013 at 02:10 AM. Reason: Modified to cope with a Table having less than 2 columns

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: mdb database to csv file

    Thanks Doogle, I just need to export a particular table, I will try your code and get back to this thread as quick as I can. Thank you!

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: mdb database to csv file

    You may want to lookup the recordset getstring function

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: mdb database to csv file

    You don't need any controls to do this. Even a VB6 program with no forms at all can be used for this kind of thing.

    One of the faster to write and quite versatile options is to use the Jet 4.0 Text IISAM, which works much like the older ODBC Text Driver. These can use default options for the text files involved or you can provide more detailed control over the format of the text files by using a [tt]schema.ini[/file]. It isn't perfect but it does work very well and should already be part of your toolkit of VB6 kowledge. The plus is that in most cases you can even write a quick WSH script in VBScript to do this with no performance impact.

    This even works from VB6's retarded cousin VBA.

    Saying you want to export as "CSV" doesn't tell us much, since there are many options. Header row or not? Quote string fields or not? What date format is needed?


    Once you have a connection opened to your database exporting is as easy as:
    Code:
                DBConn.Execute "SELECT * INTO [Text;Database=" _
                             & App.Path _
                             & "].[Export1.csv] " _
                             & "FROM [Table]", , _
                               adCmdText Or adExecuteNoRecords

    The attached demo creates an empty MDB with one table and imports a supplied text file into it. Then it displays this in a flegrid (just for grins), and then it exports the table in two different CSV formats.

    See:

    Schema.ini File (Text File Driver) for some of the basics, and:

    Processing Text Databases for a tutorial and very useful download SchemaNotes.rtf.
    Attached Files Attached Files

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: mdb database to csv file

    dilettante, Thanks for your comprehensive solution, I really appreciate it. The issue is now resolved with better result than anticipated. Thanks!

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: [RESOLVED] mdb database to csv file

    dilettante, I have one more question, with your way of exporting to csv, how can I exclude the column name on the first line of the exported file.

  9. #9
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] mdb database to csv file

    The column header row can be excluded by specifying ColNameHeader=False in your schema.ini file. The example project I posted above does this for the Export1.csv file it creates.

    This is also covered in the notes and docs I linked to above.

  10. #10
    New Member
    Join Date
    Feb 2014
    Posts
    6

    Re: [RESOLVED] mdb database to csv file

    Hi I need to export all the tables from the mdb file.Can any one help me on this

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: [RESOLVED] mdb database to csv file

    Quote Originally Posted by karthick200 View Post
    Hi I need to export all the tables from the mdb file.Can any one help me on this
    Please start your OWN thread. This one is already Resolved--not appropriate to 'steal' another's. In the meantime, search the forum.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width