Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to get current user via a data access page 1

Status
Not open for further replies.

NickKitchen

Technical User
Jul 18, 2003
6
GB
I have written a query that limits the rows in a table dependant on the user. This works fine. I have then created a Data Access Page to run the query. From within Access this works fine. However if I try and access the page from outside access I get a message saying the CurrentUser() function is undefined. I think this is due to the Jet interface. Does anyone know either a work round or a way of passing the current user to the query using a parameter?

Thanks,

Nick.
 
You might want to use the user's NT login name rather then CurrentUser. Here's a function that will return that info
Code:
'+********************************************************************************************
'*
'*  Function:	MMC_GetNTuserName
'*
'*  Author:		FancyPrairie
'*
'*	Date:		7/19/2002
'*
'*  Purpose:	Returns the user's NT logon name or "!!Error!!" if failed to get NT name
'*
'*  Call:       strUserName = GetNTuserName
'*
'-********************************************************************************************

Function GetNTuserName()

	dim strUserName
	Dim objNet

	On Error Resume Next  'In case we fail to create object then display our custom error

	Set objNet = CreateObject("WScript.NetWork") 

	If  Err.Number <> 0 Then   'If error occured then display notice
		msgbox &quot;Your security settings for the Intranet site are preventing this form from operating correctly.&quot; & vbcrlf & vbcrlf & &quot;To fix the problem, goto Tools|Internet Options|Security and select the Local INTRANET icon.  Then select the button labeled &quot;&quot;Customize Level...&quot;&quot; (located at the bottom of the window).&quot; & vbcrlf & vbcrlf & &quot;The option entitled &quot;&quot;Initialize and script ActiveX controls not marked as save&quot;&quot; MUST NOT be disabled.  Likewise, &quot;&quot;Run ActivX controls and plug ins&quot;&quot; must also NOT be disabled.&quot;,vbexclamation
		Set objNet = Nothing                        'Destroy the Object to free the Memory
		GetNTuserName = &quot;!!Error!!&quot;
    Else
		strUserName = objNet.UserName
		Set objNet = Nothing                        'Destroy the Object to free the Memory
		GetNTuserName = strUserName
	End if
	
End Function
 
Thanks FancyPrairie, but where would I use this function? The problem I have got is because the Jet Engine doesn't recognise half of the built in Access functions, including CurrentUser(), and won't recognise any custom functions either.
 
At the beginning of the HTML code that Access generates via the Data Access Page, you should see something like this:

<Style ...
</Style>
<Meta ....
<Body ....
<Object ...
<Meta ...
<Table ...

Between the <Meta tag and the <Table tag insert the code as follows:
Code:
<SCRIPT language=vbscript>
<!--
		
	dim gstrCurrentUser
-->
</SCRIPT>


<SCRIPT language=vbscript event=onload for=window>
<!--
	gstrCurrentUser = GetNTuserName
-->
</SCRIPT>

<SCRIPT language=vbscript>
<!--
'+********************************************************************************************
'*
'*  Function:    MMC_GetNTuserName
'*
'*  Author:        FancyPrairie
'*
'*    Date:        7/19/2002
'*
'*  Purpose:    Returns the user's NT logon name or &quot;!!Error!!&quot; if failed to get NT name
'*
'*  Call:       strUserName = GetNTuserName
'*
'-********************************************************************************************

Function GetNTuserName()

    dim strUserName
    Dim objNet

    On Error Resume Next  'In case we fail to create object then display our custom error

    Set objNet = CreateObject(&quot;WScript.NetWork&quot;) 

    If  Err.Number <> 0 Then   'If error occured then display notice
        msgbox &quot;Your security settings for the Intranet site are preventing this form from operating correctly.&quot; & vbcrlf & vbcrlf & &quot;To fix the problem, goto Tools|Internet Options|Security and select the Local INTRANET icon.  Then select the button labeled &quot;&quot;Customize Level...&quot;&quot; (located at the bottom of the window).&quot; & vbcrlf & vbcrlf & &quot;The option entitled &quot;&quot;Initialize and script ActiveX controls not marked as save&quot;&quot; MUST NOT be disabled.  Likewise, &quot;&quot;Run ActivX controls and plug ins&quot;&quot; must also NOT be disabled.&quot;,vbexclamation
        Set objNet = Nothing                        'Destroy the Object to free the Memory
        GetNTuserName = &quot;!!Error!!&quot;
    Else
        strUserName = objNet.UserName
        Set objNet = Nothing                        'Destroy the Object to free the Memory
        GetNTuserName = strUserName
    End if
    
End Function
-->
</SCRIPT>
 
Thanks - that would work fine if it wasn't for the security settings on my PC.

Don't suppose you know any method that gets round them do you?

Nick.
 
To get around the security settings, Select Tools from the menu bar then select Internet Options and then select the Security tab. Select Local Intranet settings (assuming you're putting this on your Intranet site) and select the button labeled Customize Settings... Then enable the options that are stopping you from excuting Active X controls, etc.

The problem is that all of your users will also have to have these settings set. I don't know how to do it programmatically (don't think you can for security reasons). However, your IT folks might be able to roll out the settings to your users. Out IT people say it's possible for XP machines but haven't tryed it yet.
 
Unfortunately I don't have the option of changing the settings - it's company policy to have them set that way and we can't change them.

Thanks anyway,

Nick.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top