How do I get ms-access to connect to ms-sql as a different user?

前端 未结 6 1367
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 20:04

How do I get ms-access to connect (through ODBC) to an ms-sql database as a different user than their Active Directory ID?

I don\'t want to specify an account in th

6条回答
  •  一个人的身影
    2021-01-06 20:28

    I think you can get this to work the way you want it to if you use an "ODBC DSN-LESS connection"

    If you need to, keep your ODBC DSN's on your users' machines using windows authentication. Give your users read-only access to your database. (If they create a new mdb file and link the tables they'll only be able to read the data.)

    Create a SQL Login which has read/write permission to your database.

    Write a VBA routine which loops over your linked tables and resets the connection to use you SQL Login but be sure to use the "DSN-Less" syntax.

    "ODBC;Driver={SQL Native Client};" &
           "Server=MyServerName;" & _
           "Database=myDatabaseName;" & _
           "Uid=myUsername;" & _
           "Pwd=myPassword"
    

    Call this routine as part of your startup code.

    A couple of notes about this approach:

    • Access seems to have an issue with the connection info once you change from Read/Write to Read Only and try going back to Read/Write without closing and re-opening the database (mde/mdb) file. If you can change this once at startup to Read/Write and not change it during the session this solution should work.

    • By using a DSN - Less connection you are able to hide the credentials from the user in code (assuming you're giving them an mde file you should be ok). Normally hard-coding connection strings isn't a good idea, but since you're dealing with an in-house app you should be ok with this approach.

提交回复
热议问题