PDA

View Full Version : I receive a #name? error appears?



wedd
10-27-2010, 02:20 AM
I wrote the following code to retrieve a username and computer name for the access 2007 databse I created. I see a #Name? error appearing. Are there any reasons why this error appears as the code seems to be correct.:dunno

Thanks for your ideas :clap:


modUserName
Option Compare Database ' This code was originally written by Dev Ashish.' It is not to be altered or distributed,' except as part of an application.' You are free to use it in any application,' provided the copyright notice is left unchanged.'' Code Courtesy of Dev Ashish at The Access Web (http://www.mvps.org/access/)Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As LongFunction fOSUserName() As String' Returns the network login name Dim lngLen As Long, lngX As Long Dim strUserName As String strUserName = String$(254, 0) lngLen = 255 lngX = apiGetUserName(strUserName, lngLen) If lngX <> 0 Then fOSUserName = Left$(strUserName, lngLen - 1) Else fOSUserName = "" End IfEnd Function

modComputerName
Option Compare Database ' This code was originally written by Dev Ashish.' It is not to be altered or distributed,' except as part of an application.' You are free to use it in any application,' provided the copyright notice is left unchanged.'' Code Courtesy of Dev AshishPrivate Declare Function apiGetComputerName Lib "kernel32" Alias _"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As LongFunction fOSMachineName() As String'Returns the computername Dim lngLen As Long, lngX As Long Dim strCompName As String lngLen = 16 strCompName = String$(lngLen, 0) lngX = apiGetComputerName(strCompName, lngLen) If lngX <> 0 Then fOSMachineName = Left$(strCompName, lngLen) Else fOSMachineName = "" End IfEnd Function

Imdabaum
10-27-2010, 06:15 AM
It depends on how you are setting your control's value. Which control shoes #Name? and what is the control source?

wedd
10-27-2010, 06:54 AM
The control source for user name and computer name...It's a network login feature I am trying to create... the source code is Environ("Computername") and Environ("Username") for the other control source

Imdabaum
10-27-2010, 08:27 AM
So in the textbox do you see =Environ("Computername") or =Environ("Username")?

Also as I recently discovered the Environ() variables can be hacked. So it's better to use the functions you used above. fOSUsername() and fOSComputerName

wedd
10-27-2010, 09:59 AM
I have 2 text boxes, 1 displaying =Environ("Computername") and a different one displaying =Environ("Username"); i've also written the separate codes in the Control source for each one respectively.

wedd
10-27-2010, 10:01 AM
the function names you recommended that I use. Can I use it for a network workstation, too?

Imdabaum
10-27-2010, 11:03 AM
I have 2 text boxes, 1 displaying =Environ("Computername") and a different one displaying =Environ("Username"); i've also written the separate codes in the Control source for each one respectively.

Not sure why that isn't working. I have used that before. Anyone know if Access 2007 has changed the way it uses the Environ variables? I just tested it here and it doesn't work, but 2 months ago at my previous contract they were on 2003 and it did work there. I know the getUser api call is better. I just was curious why it doesn't work anymore?

As a work around you could use the methods you created in your first post. These will be more reliable.

I also created a public function getEnviron(str as string) as String to return the Environ(str) value and setting the value to that seems to work as well. I'm not sure why =Environ$("Username") and =Environ("UserName") are not working though.

hansup
10-27-2010, 10:00 PM
Not sure why that isn't working. I have used that before. Anyone know if Access 2007 has changed the way it uses the Environ variables? I just tested it here and it doesn't work, but 2 months ago at my previous contract they were on 2003 and it did work there. I know the getUser api call is better. I just was curious why it doesn't work anymore?

I suspect that is due to Sandbox Mode. See what you make of this link: Functions and properties in Access 2007 blocked by sandbox mode (http://office.microsoft.com/en-us/access-help/functions-and-properties-in-access-2007-blocked-by-sandbox-mode-HA001230190.aspx)

Perhaps the default Sandbox setting changed from Access 2003 to 2007.