download certain tables from mysql server to particular location as csv files

SaiSantosh274

New Member
Joined
Nov 16, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
  1. connect to mysql database.
  2. should give user input which table to download from database.
  3. selected table should get downloaded to particular location and save as a csv file. note csv name should be tablename.csv.

please find my code.
  1. Sub connect()
    Dim Password As String
    Dim SQLStr As String
    'OMIT Dim Cn statement
    Dim Server_Name As String
    Dim User_ID As String
    Dim Database_Name As String
    'OMIT Dim rs statement

    Set rs = CreateObject("ADODB.Recordset") 'EBGen-Daily
    Server_Name = "localhost"
    Database_Name = "testdb" ' Name of database
    User = "root" 'id user or username
    Password = "zxcasdQWE123" 'Password

    SQLStr = "SELECT * FROM vector"

    Set Cn = CreateObject("ADODB.Connection") 'NEW STATEMENT
    Cn.Open "Driver={MySQL ODBC 8.0 Unicode Driver};Server=" & _
    Server_Name & ";Database=" & Database_Name & _
    ";User=" & User & ";Password=" & Password & "; Option=3;"

    rs.Open SQLStr, Cn, adOpenStatic

    Dim myArray()
    Dim ostream As Object

    myArray = rs.GetRows()

    kolumner = UBound(myArray, 1)
    rader = UBound(myArray, 2)

    Set ostream = CreateObject("ADODB.Stream")
    ostream.Open
    'ostream.WriteText "hi, hello" & vbNewLine & "how, are" ' test input. not for any use
    ostream.SaveToFile ("C:\Users\asus\Downloads\vector.csv")
    ostream.Close

    rs.Close
    Set rs = Nothing
    Cn.Close
    Set Cn = Nothing
    End Sub
have connected to my database and selected a table for eg and put into array. now i am struck with how to download that table as csv's. and make user input on which table to select from database. Can anyone help me with it.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top