Capture SQL Server logged in db User name in Access 2007 Accdb
15 pts.
0
Q:
Capture SQL Server logged in db User name in Access 2007 Accdb
I have a SQL Server 2005 backend with an Access 2007 Accdb front-end application.  Security has been established via SQL Server database user groups with associated table-level permissions.

I would like to disable certain form functionality based on the logged in user.  Is there a way to "capture the SQL Server logged in user name" in the Access code?  Probably in the form_load/open event.



Software/Hardware used:
SQL Server 2005, Access 2007
ASKED: Aug 24 2009  11:14 PM GMT
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
0
46795 pts.
0
A:
 RATE THIS ANSWER
+1
Click to Vote:
  •   1
  •  0
  • AddThis Social Bookmark Button
The SQL Server system function suser_sname() will return the username of the login used to connect to the SQL Server. Wouldn't it be easier to have Access provide the NT Username of the person who opened the access file?
Last Answered: Aug 25 2009  3:15 AM GMT by Mrdenny   46795 pts.
0
0
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _



_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Ppeterso   15 pts.  |   Aug 26 2009  3:15PM GMT

How does Access capture the NT username? What is the advantage of Access providing the user name verses SQL Server?

btw..thanks for the above answer.

 

Randym   1410 pts.  |   Aug 27 2009  2:08PM GMT

If you use linked tables, the logon would be the same for all users of the Access application unless you relink the tables for each Access user. The same goes if you use DAO in VB code. The NT user user is specific to who ever is logged on at that PC running the application at that time. This code works in Access 2003 to get the OS user. You can also use Access security which can be separate from the OS security.

Option Compare Database
Option Explicit
Private Declare Function GetUserNameA Lib “advapi32.dll” (ByVal lpBuffer As String, nSize As Long) As Long

Public Function UserLoggedOn() As String
Dim cn As String
Dim ls As Long
Dim res As Long

cn = String(1024, 0)
ls = 1024
res = GetUserNameA(cn, ls)
If res <> 0 Then
UserLoggedOn = Mid(cn, 1, InStr(cn, Chr(0)) - 1)
Else
UserLoggedOn = “”
End If
End Function

 
0