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

Posted on 2008-11-05
Last Modified: 2010-04-21
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
Question by:DHompster
    LVL 22

    Accepted Solution

    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

    LVL 22

    Expert Comment

    by:Kelvin Sparks
    If you are linking to SQL 2000, replace the reference to {SQL Native Client}
    with {SQL Server}


    Author Closing Comment

    Excellent!!!!  Thank you!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now