Solved

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

Posted on 2016-10-25
6
58 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

695 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