Solved

Use DSN-less Connection to Link MS SQL Views Without UID Warning

Posted on 2016-10-25
6
32 Views
Last Modified: 2016-11-24
Hi, Experts!

I have an MS Access 2016 application that uses a connection string in code to re-link all SQL tables when the application is opened.  It works fine with tables, however if I link a view the user always gets the "Select UID" window for each view.  Is there a way around this?

Here's my code.  There is a local table called "tblAppTables" that has the SQL name and local name of all the tables/views.

Public Function RefreshAppTables() As Boolean
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strConnection As String

strConnection = "DRIVER=SQL Server;SERVER=192.168.0.25;DATABASE=MyDB;UID=MyUse;PWD=MYPassword"

Set db = CurrentDb
Set rs = db.OpenRecordset("tblAppTables")

DoCmd.SetWarnings False
DoCmd.Hourglass True

rs.MoveFirst
While Not rs.EOF
    On Error Resume Next
        DoCmd.DeleteObject acTable, rs("LocalName")
    On Error GoTo 0
    DoCmd.TransferDatabase acLink, "ODBC", "ODBC;" & strConnection, acTable, rs("SQLName"), rs("LocalName"), False, True
    rs.MoveNext
Wend

MsgBox "All Tables Refreshed"

DoCmd.SetWarnings True
DoCmd.Hourglass False

End Function

Open in new window


Thanks!
0
Comment
Question by:jriggin
6 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
Comment Utility
When Access links to an object (table or View) is must know the primary key (or unique index) to use with this link (or that link becomes read only).
What you need to do is to trap the error when it happens and then, based on the name of it (the view) create a unique index in access for it before moving onto the next view). Easy is there's only a few views, but could a real issue if many.

If you only need read only link, then you can trap and ignore that message)


Kelvin
0
 

Expert Comment

by:Christopher Deen
Comment Utility
Microsoft provides drivers on Windows for non-SQL database systems such ... 3but there are close links between unixODBC and Easysoft, and ... "DSN=dsn;UID=uid;PWD=pwd" but it can be constructed without a DSN by specifying a driver ... To find out what tables are accessible from a connection ch, use.
https://www.justcerts.com/
0
 
LVL 1

Author Comment

by:jriggin
Comment Utility
There is no error to trap that I can trap in VBA.  I am connecting to MS SQL 2102, and every time I link a view, manually or through code, I get a window that says, "Select Unique Identifier."  A view of course does not have a UID/key and I have not been able to find how to give it one.
0
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

 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 500 total points
Comment Utility
I use similar code, but not the DoCmd.TransferDatabase

Try this alternative

Do Until rs.EOF
    For Each tdf In db.TableDefs
        If tdf.Name = rs!LocalName Then
            ''Build the dsn-less connection string
            db.TableDefs.Delete rs!LocalName
            Exit For
        End If
    Next
        strConnect = "ODBC;Driver={SQL Server};Server=192.168.0.25;DatabaseMyDB;UID=MyUse;PWD=MYPassword"
        Debug.Print strConnect
        Set tdf = db.CreateTableDef(rsLocalName, dbAttachSavePWD, rs!SQLName, strConnect)
        db.TableDefs.Append tdf
    rs.MoveNext
Loop



Kelvin
0
 
LVL 1

Author Closing Comment

by:jriggin
Comment Utility
That works!  Thanks
0
 

Expert Comment

by:James Spence
Comment Utility
ExamCertify gives in a flash downloadable exam planning materials for multi-seller exams, including Cisco, CompTIA, Microsoft, Oracle, HP, IBM and that's just the beginning.

http://www.examcertify.com/
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Viewers will learn how the fundamental information of how to create a table.

728 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

9 Experts available now in Live!

Get 1:1 Help Now