简体   繁体   中英

Problems with ADODB connection in VBA

I have a VB.NET function that I want to connect to use to connect to a SQL Server database and return a recordset. I am new to VB and to SQL, so I hope it is something simple, but I am sure that my connection string and SQL query are correct, as they were checked by a teammate. I have a copy of the Exception message following the code

' Retrieve Record Set 
Private Function GetRecords() As ADODB.Recordset
    Dim conn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    With conn
        .ConnectionString = "Server=12.345.678.9;Database=db01;User Id=userid01;Password=passwordyword"
        .Open() ' Exception Thrown Here
        rs.Open("SELECT * FROM [Table] ;", "sql")
        rs.Close()
        .Close()
    End With
    Return rs
End Function

Exception Message:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in ThisProject.dll but was not handled in user code
Additional information: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Any and all help would be greatly appreciated! Thanks!

Here you try.

m_sConnStr = "Provider='SQLOLEDB';Data Source='MySqlServer';" & _ 
 "Initial Catalog='Northwind';Integrated Security='SSPI';"

Connection String for VBA

This should work. Note that the connection also needs passed in the rs.Open

Private Function GetRecords() As ADODB.Recordset
    Dim conn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    With conn
        .ConnectionString = "Driver={SQL Server};Server=12.345.678.9;Database=db01;Uid=userid01;Pwd=passwordyword;"
        .Open() ' Exception Thrown Here
        rs.Open("SELECT * FROM [Table] ;", conn)
        rs.Close()
        .Close()
    End With
    Return rs
End Function

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM