using ENVIRON$("USERNAME")

Hello all,

I am using ENVIRON$("USERNAME") as criteria in a query. It works great locally on Windows 2000 & XP with Access 2K, XP, and 03. As soon as I put in on my server/domain it gives me an undefined error on either OS /version of access. The server is running 2K SP4 but does not have office installed on it. The SET command brings up the correct username on the workstations. Copying the front end to the local workstation does not work.

Any one have any suggestions / alternatives?

TY

Tim
LVL 2
mdbdevAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
rockiroadsConnect With a Mentor Commented:
Hi Tim

Environ always looks at your environment

so a better option might be to use the API call

place this code in a module

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

public function GetMyUsername() as String
    Dim s As String
    Dim lrv As Long
   
    s = String(1024, 0)
   
    lrv = GetUserName(s, 1024)
    If lrv <> 0 Then GetMyUsername = Mid(s, 1, InStr(s, Chr(0)) - 1) Else GetMyUsername = ""
End Sub
0
 
jadedataMS Access Systems CreatorCommented:
What is the context in which Environ$("UserName") is being used (post code)??

This is an Access function to retrieve the value of an OS variable...  It strikes me that without Access loaded on the machine that is running the code, there can be no successful function call...
0
 
mdbdevAuthor Commented:
Rock,

Thought about pulling it into a module. How can I use the value via the query (recordsource)?

Jade,

The calue is used in a query (recordsource) of a form as criteria where fieldname = Environ$("UserName"). The query is not a SQL string in VB rather just built in query builder.

I am loading office on the server now.

Tim
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
jadedataConnect With a Mentor MS Access Systems CreatorCommented:
A function call is a function call, no matter where its being called from.  

"Undefined function" means that a process can not "see" the function in its vast library of code in order to run the function.
0
 
rockiroadsCommented:
u should be able to use the function within the query
I tried it, for some reason the name GetMyUsername was playing ball, so I created a test function first, ensured it got called by a query then pasted my code in

my code

Public Function MyUName() As String
    Dim s As String
    Dim lrv As Long
   
    s = String(1024, 0)
   
    lrv = GetUserName(s, 1024)
    If lrv <> 0 Then MyUName = Mid(s, 1, InStr(s, Chr(0)) - 1) Else MyUName = ""
End Function


my query


SELECT MyUName()
0
 
mdbdevAuthor Commented:
Thanks guys,

I got the above code to work locally. Dont think installing office is an option as the client does not have office on his servers. Once I get my DNS issue resolved, I will test above code on domain.

TY AGAIN

Tim
0
 
jadedataMS Access Systems CreatorCommented:
How about using the MSAccess runtime engine to compensate?  You can do this if you have MSOffice Developer
0
 
Data-ManCOOCommented:
Use the code that rocki posted to get the user name and then populate a text box on a hidden form

I do that with username, machine name and a few other key values.

Then you can reference the value of the text box that stores the user name in a query.

Mike
0
 
mdbdevAuthor Commented:
thanks you gentlemen, Rock your code worked great (once I got the DNS issues resolved)

thanks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.