Solved

using ENVIRON$("USERNAME")

Posted on 2004-08-02
9
1,553 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:mdbdev
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 300 total points
ID: 11694355
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
 
LVL 32

Expert Comment

by:jadedata
ID: 11694365
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
 
LVL 2

Author Comment

by:mdbdev
ID: 11694603
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 32

Assisted Solution

by:jadedata
jadedata earned 200 total points
ID: 11694647
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 11694735
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
 
LVL 2

Author Comment

by:mdbdev
ID: 11695511
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
 
LVL 32

Expert Comment

by:jadedata
ID: 11695816
How about using the MSAccess runtime engine to compensate?  You can do this if you have MSOffice Developer
0
 
LVL 18

Expert Comment

by:Data-Man
ID: 11695986
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
 
LVL 2

Author Comment

by:mdbdev
ID: 11702138
thanks you gentlemen, Rock your code worked great (once I got the DNS issues resolved)

thanks again
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question