Microsoft KB Archive/259224

= HOW TO: Determine the User Name in a Data Access Page in an Access 2000 Project =

Article ID: 259224

Article Last Modified on 6/24/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q259224



Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies only to a Microsoft Access project (.adp).

IN THIS TASK
SUMMARY
 * Creating the SQL Database and Table
 * Writing a Stored Procedure to Return the User Name
 * Create the Data Access Page

REFERENCES



SUMMARY
You can determine the current network user name of a user who is logged on to a Microsoft Windows NT or a Microsoft Windows 2000 network by using Win32 API functions. However, because scripting languages such as VBScript or JScript do not support API calls for security purposes, you cannot use these functions in a data access page.

Microsoft SQL Server provides the SUSER_SNAME function to return the user name that is logged on to SQL Server. By using this function in a stored procedure, you can determine the network user name in a data access page in an Access project. This article shows you how to do this.

To demonstrate how this procedure works, this article asks you to do the following:
 * Create a SQL database and a table.
 * Write a stored procedure to return the user name.
 * Build the data access page with a script to filter records based on the return value from the stored procedure.

back to the top

Creating the SQL Database and Table
 Start Access 2000, click the Access database wizards, pages and projects option, and then click OK. On the General tab, click Project (New Database), and then click OK. Type a new file name for the project, and then click OK. On the first page of the Microsoft SQL Server Database Wizard, select or type the name of a server to use to create the database. Leave the user name and password blank. This will create the database using NT integrated security. You must have CREATE DATABASE permissions on the server to create a database on that server. Type the name of a new SQL database to be created on the SQL Server. Click Next, and then click Finish to create the new database.</li>  In the Database window, click Stored Procedures, and then click New. Replace the existing code with the following stored procedure: Create Procedure CreateTestTable As CREATE TABLE TestTable (   CustomerID nchar(5) NOT NULL,    CompanyName nvarchar(40) NOT NULL ,    ContactName nvarchar(30) NULL ,    ContactTitle nvarchar(30) NULL ,    Address nvarchar(60) NULL ,    City nvarchar(15) NULL ,    Region nvarchar(15) NULL ,    PostalCode nvarchar(10) NULL ,    Country nvarchar(15) NULL ,    UserName nvarchar(10) NULL

CONSTRAINT MyContraint PRIMARY KEY (CustomerID)

)   return  Close and save the stored procedure. </li> In the Database window, double-click the new stored procedure to run it. This creates a table on SQL Server. You must have CREATE TABLE permissions on the server to run this stored procedure.</li> After the table has been created, open it in Design View. Insert sample data into the new table, making sure to enter your network user name in the UserName field.</li></ol>

back to the top

Writing a Stored Procedure to Return the User Name
<ol>  In the Database window, click Stored Procedures, and then click New. Type the following stored procedure: Create Procedure spGetUserName As -- declare variables DECLARE @name varchar(30) ,@lenindex int ,@lenall int ,@username varchar(10) -- return the user logged into the SQL server SELECT @name = suser_sname SELECT @lenall = Len(@name) SELECT @lenindex = CHARINDEX('\', @name) -- clean up the variables and return a field named NTUserName SELECT SUBSTRING(@name, @lenindex+1, @lenall-@lenindex) AS NTUserName return Close and save the stored procedure. </li> In the Database window, double-click the spGetUserName stored procedure to run it. Note that one field and one record is returned with your network user name. Close the results.</li></ol>

back to the top

Create the Data Access Page
<ol> In the Database window, click Pages, and then click New. Click AutoPage: Columnar, select TestTable from the list of tables, and then click OK.</li> Open the page in Design view.</li> On the Tools menu, point to Macro, and then click Microsoft Script Editor.</li> Locate the Script Outline in the bottom-left corner of the screen.</li> Expand the Client Objects & Events folder, and expand the window object. Double-click the onload event. This will enter a client-side SCRIPT block for the event.</li>  Type the following code: 'Return the current user from the connection. 'This will return &quot;sa&quot; when using SQL authentication, and an empty 'string if using NT authentication. You must use a stored 'procedure to parse out the NT user name using SQL functions

dim rs, strUserName

'This property will return SSPI when using NT integrated security if MSODSC.Connection.Properties(&quot;Integrated Security&quot;)=&quot;SSPI&quot; then set rs=MSODSC.Connection.Execute(&quot;spGetUserName&quot;) strUserName=rs(&quot;NTUserName&quot;) rs.close: set rs=nothing

'if the connection is using SQL authentication, use the user id from 'the MSODSC connection else strUserName=MSODSC.Connection.Properties(&quot;User ID&quot;) end if

'apply a serverfilter to the page for the current user name MSODSC.RecordsetDefs.item(0).ServerFilter=&quot;UserName='&quot; & strUserName & &quot;'&quot; </li> Close and save the data access page.</li> To test the page, open it in Microsoft Internet Explorer 5 or later. Note that you only see records where the UserName field contains your user name.</li></ol>

back to the top

<div class="references_section">