Solved

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

Posted on 2016-10-25
6
50 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
ID: 41859663
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
ID: 41859868
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
ID: 41860323
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 500 total points
ID: 41860742
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
1
 
LVL 1

Author Closing Comment

by:jriggin
ID: 41861022
That works!  Thanks
0
 

Expert Comment

by:James Spence
ID: 41901174
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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