SaiSantosh274
New Member
- Joined
- Nov 16, 2021
- Messages
- 7
- Office Version
- 365
- Platform
- Windows
- connect to mysql database.
- should give user input which table to download from database.
- 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.
- 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