Im totally new in vb script. Im trying to connect with mi local mysql db with this code:
Option Explicit
Private CN As ADODB.Connection
Function Connect(Server As String, Database As String) As Boolean
Set CN = New ADODB.Connection
On Error Resume Next
With CN
.ConnectionString = "Driver={MySQL ODBC 5.3 Driver};Server=" & _
Server & ";Database=" & Database & _
";Uid=user;Pwd=password;"
.Open
End With
If CN.State = 0 Then
Connect = False
Else
Connect = True
End If
End Function
Function Query(SQL As String)
Dim RS As ADODB.Recordset
Dim Field As ADODB.Field
Dim Col As Long
Set RS = New ADODB.Recordset
RS.Open SQL, CN, adOpenStatic, adLockReadOnly, adCmdText
If RS.State Then
Col = 1
For Each Field In RS.Fields
Cells(1, Col) = Field.Name
Inc Col
Next Field
Cells(2, 1).CopyFromRecordset RS
Set RS = Nothing
End If
End Function
Function Disconnect()
CN.Close
End Function
Private Sub SQL_Click()
Dim SQL As String
Dim Connected As Boolean
SQL = "Select * from table1"
Connected = Connect("localhost", "table")
If Connected Then
Call Query(SQL)
Call Disconnect
Else
MsgBox "Could Not Connect!"
End If
End Sub
Always shows could not conect. ADODB library is ok, my db parameters works in manual imports from mysql, and the driver version is correct. I cant figured out what is happening...
Some help?
Thanks!!!
It seems like your driver name in the connection string is wrong.
Try: "Driver={MySQL ODBC 5.3 ANSI Driver}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.