Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using ENVIRON$("USERNAME")

Posted on 2004-08-02
9
Medium Priority
?
1,556 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 1200 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 32

Assisted Solution

by:jadedata
jadedata earned 800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

877 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