Use VBA to link to SQL Server tables using File DSN instead of System DSN

My Access application currently uses System DSN links to a SQL Server DB.  I have a routine that allows the linked tables to be switched back and forth between the live data and a test database.  I now have some users who don't have the System DSN data source.  Can I use a similar VBA routine to use the File DSNs instead of the System DSNs?

The connection string I currently use is as follows:

DoCmd.TransferDatabase acLink, "ODBC Database", "ODBC;DSN=" & DSN & ";Description=" & DSN & ";APP=Microsoft Office 2003;WSID=" & LinkWSID & ";DATABASE=" & DSN & ";TABLE=dbo." & rs!ContainerName, acTable, "dbo." & rs!ContainerName, rs!ContainerName, 0
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kelvin SparksCommented:
I prefer to link using a dsn-less connection. It does mean that you cannot manually link tables, but removes the need for a dsn altogether.

The code below is out of an existing app of mine, but could be adapted to work.


''Build the dsn-less connection string
strServer = DLookup("[ServerName]", "USysDatabaseLink", "[LinkID] = " & lLink)
strDatabase = DLookup("[DatabaseName]", "USysDatabaseLink", "[LinkID] = " & lLink)
strConnect = "ODBC;Driver={SQL Native Client};Server=" & strServer & ";Database=" & strDatabase & ";Trusted_Connection=yes"
sSQL = "SELECT * FROM USysTables where Not IsNull(RemoteName);"
Set rsTables = db.OpenRecordset(sSQL, dbOpenSnapshot)
Do Until rsTables.EOF
    For Each tdf In db.TableDefs
        If tdf.Name = rsTables!TableName Then
            db.TableDefs.Delete rsTables!TableName
            Exit For
        End If
        Set tdf = db.CreateTableDef(rsTables!TableName, dbAttachSavePWD, rsTables!RemoteName, strConnect)
        db.TableDefs.Append tdf

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelvin SparksCommented:
If you are linking to SQL 2000, replace the reference to {SQL Native Client}
with {SQL Server}

DHompsterAuthor Commented:
Excellent!!!!  Thank you!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.