1

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!

1
  • In your connection string, you have to provide which driver are you using? for example, oledb, oracleoledb.. etc... Commented Mar 18, 2014 at 18:02

2 Answers 2

1

Here you try.

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

Connection String for VBA

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.