user2119980 user2119980 - 1 year ago 111
SQL Question

Using ADO/DAO Connection to Download data from SQL Server

I am trying to figure out how to download using an ADO/DAO connection in Access VBA to get the contents of a table from SQL server. I am trying to avoid using a linked table because the DB requires a password and I keep running into issues with getting it to not ask for the login info. Are there any ideas or references for me to start with on this matter?

Answer Source

It appears either way you'll need to provide SQL credentials.

There's more involved without linking a table, basically you'd want a recordset for the source and the "target" table to iterate over.

targetrs = CurrentDb.OpenRecordset("Target", dbOpenTable)

Dim Con As New ADODB.Connection
Dim sqlStr As String 
    Con.Open _
        "Provider = sqloledb;" & _
        "Data Source=SqlServer;" & _
        "Initial Catalog=MyDB;" & _
        "User ID=sa;" & _

Dim rsSource As New ADODB.Recordset 
    rsSource.Open "select * from SOURCE", Con

    do until rsSource.eof
        for each field in rsSource
            targetrs.fields(field.Name) = rsSource.fields(field.Name)

Since you still have to have the credentials, you could dynamically link the table instead:

docmd.TransferDatabase acLink,"ODBC Database",
"ODBC;Driver={SQL Server};Server=MySQLServer;Database=MYSQLDB;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download