In Access 97 how can I get VBA to return the computer's User Name as displayed, for instance, when logging on to Microsoft Networking? Is there some API call which will generate this value? All help gratefully received.
There are API's that can do it, but the Environ("UserName" function that ETID suggested is by far the best way to get it. As a side note, if you need to know, the Newtork name of the PC, Environ("computername" will give that to you, you can also get info about the processor on the local machine, etc. It's a very useful function...
I feel a bit daft missing that, it's clearly described in the Help files.
However: I examined every Environment variable from 1 to 256 and nowhere could I find the User Name or the Computername. I Tried Environ("Computername", Environ("UserName", Environ("User Name" to no avail.
All three have returned empty strings. Any thoughts on what is wrong here?
As I write this I have my laptop PC networked to my desktop PC, with the network name for my Laptop set to "Rod" and the User Name set to "Siemens" (that's the name that appears in the User name: field of the network login dialogue box. The network name for the desktop PC is Peter and its Login User name is "Minnie"
I am not able to retrieve either of these names using the methods mentioned above.
For the record I am running Access97 in Win98.
My objective, in case it promts any ideas, is this.
I am running a new application across a ten station network and the users aren't very responsive, so I am trying to put together a system for logging the opening and closing of forms by each user so I can find out which bits they are using and which bits are redundant.
I could keep a local log on each machine but I was hoping to keep the records in a single table on the back-end db and have the identical function on each machine. I don't want to have to tailor the code to each machine as that will mean a unique copy of the front-end db each time I upgrade the system - which is quite often lately.
I need to be able to read the name of the local machine so the function can incorporate that name into the log record as it's saved to the table. Getting the Network Name would be the best option since the Network names have been set up to match the actual people who use the machines, hence I have named my desktop at home "Peter" to replicate my client's server.
Maybe you need to load a referance library....(under tools menu in VBA editor)....pick till you win from there, I don't know which one you'll need. :-(
This works just fine on my Windows 2000 machine. I do have VB6 installed though. And I had to declare the variable x, but otherwise it returns my user name and my computer name.
That may not be much help, but I figure every little bit helps.
-Dan
I'm not sure which reference you would need to add since it is running the function (If the references aren't there you should get a reference error and not an empty string).
I am using Win NT 4.0 here versus your Win 98, so maybe that's the problem? When I run through the Environ() function, I don't have 256 different variables available to me...
Dan-
Thanks for the input, although having VB6 installed isn't required (I have it at home but not at work, and it works both places)
Well, if we can't get your Environ() function to work, there's always the Win API calls...(as a side note are you running just WIn or is Novell in there too?)
Check out this thread: thread705-107463 , there is a post in there by jimovery that looks like it's on the right path for you...
Thanks to everyone for your comments. Unfortunately nothing seems to work on my system.
Kyle, I followed up on the post by jimovery and created a Public Module from his code as follows:
Option Compare Database
Option Explicit
Public Declare Function GetUserName Lib "Advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long
Public Function Return_User()
Dim sBuffer As String
Dim gUser As Variant
Dim lSize As Long
Return_User = IIf(gUser = "", "nothing", gUser)
End Function
When I call Retrun_User I get "nothing" indicating that return_user is empty.
Can you try it to see if it works for you.
In my App the set references are:
Visual Basic For Applications
Microsoft Access 8.0 Object Library
Microsoft DAO 3.5 Object Library
Microsoft Word 8.0 Object Library
Microsoft Forms 2.0 Object Library
Have you tried loading the "microsoft office 9.0 object library" and the "OLE automation library", as well as the "microsoft excel 9.0 object library" ....maybe you might have to install these from the CD or download them.
Rod, you're using Access '97 right? (the DAO 3.5 gives it away) well that's what I'm using and for me the function works with only:
Visual Basic For Applications
Microsoft Access 8.0 Object Library
Microsoft DAO 3.5 Object Library
Selected (default), plus, since you're getting "Nothing" as a return, the function is working properly, so it's probably not a reference issue... Do me a favor (I will try the code out today as soon as I get a chance) go into Excel and go to Tools-->Options and select the "General" tab. on the bottom there will be a line entitled "User Name". See what's in that field "on my PC it's my network Username"
I checked the Excel's Tools/Options/General/Username and it contains my first and last names. I also checked the File/Properties panel where Author contains just my lastname.
Likewise in Word, the Tools/Options/User Information/name box contains just my lastname. Whereas File/Properties/Author also contains just my lastname.
Now I notice that when I start an App's mdb file on my desktop and then a copy of that mdb on my laptop, as each machine loads the User license info appears centre screen. Now on each machine this info displays the User name and Company info which is unique to the particular Office installation on each machine. These happen to be different because I set them up differently albeit for no particular reason at the time. i.e. On the laptop the username is say "a" and the Companyname is say "b", while on the desktop the username is "x" and the company name "y".
Once the application is opened, i.e. Access has loaded and opened the relevant MDB file then the File/Properties/Summary tab on both machines displays an Author and Company name that matches the settings on the desktop - the same machine on which the App's MDB file was originally created.
The point being that Access cleary does access the unique user profile for the individual machine, even if only briefly while it's loading.
I have re-tried running the original fuction as suggested by ETID and removing all references except the three you mentioned but it still returns an empty string. I also tried adding references to OLE Automation, Excel 8.0, Word 8.0, Office 8.0, plus a few others without success.
By the way I modified the function to return the "Path" and that works fine returning the full Environment Path setting.
Rod,
I was going through the Environ function for another reason, and I noticed 2 variations on the username feature. I'm on NT and I've been using Environ("UserName", but there's another one, try Environ("NWUserName" and see if that returns anything... Kyle :
Rod,
I was just re-reading your last post, and I noticed you went into Excel and checked your username? Now I'm confused. Are you trying to return the name that is stored in the registry as the 'Registered User' of the application of the name that is used to log onto the network/machine?
Kyle and I, and probably others have been returning the network/machine log on name.
***Or*** Are you trying to return the name of the person who logs onto Access. The user name that would be found in the access workgroup .mdw file?
Thanks for that but NWUserName returned nothinng. Below is a list of the first 19 Environment variables on my machine. As you can see no references to any UserNames or ComputerName. This list was produced while the laptop and desktop were each accessing a file on the other.
You were right the first time. I am trying to identify either:
the Network name of the machine as it would appear in Network Neighborhood. For instance with my two machines Network Neighborhood displays two machine icons named Rod and Peter.
OR...
the UserName as it appears when entering the "Network Password for Microsoft Netorking" which appears at startup and if the Log Off option is selected from the Start Menu. Indeed the name appears beside the "Log Off" option on the Start Menu. E.G. On my laptop this appears as "Log Off Seimens" while on my desktop machine it appears as "Log Off Minnie".
So on the two machines:
Machine: LAPTOPDESKTOP
User name: Siemens MINNIE
Network Name: Rod Peter
The point is that these names are independently of my Access App and if I can get hold of either of them through Access VBA then I can write a single function, common to all instances of my Application, that creates an activity record in a table on the back-end database which will identify the action the user was taking (e.g. Opening or Closing), the object name (e.g. a form name or a report name), the a date/time stamp from the Now() function and the machine on which the action took place.
What I do not want is to write a unique procedure for each machine, (i.e. a procedure where I have to write the name of the machine into the code) since that would mean I have to create ten versions every time I perform the tiniest upgrade. If possible I would prefer to have a single procedure that is common to all workstations and which pulls up the host machine's Network name or User name.
This is especially important since I try wherever possible to distribute my upgrades as a single self extracting .exe zip-file attached to an email.
To underline this let me also also mention that I have not incorporated any User naming or permission granting protocols within in the Application. All workstations have equal rights.
Environ$("User" will not work under Windows 9x as it is a Windows NT environment variable. I can see no reason why you should not be able to retrieve the computer name though. In fact I have done this at home running Windows 98.
I'm almost certain this is not a references issue.
Sorry I can't be of more help, but at least I can guarantee there is no point in persuing Environ$("User" - there's no way this will ever work under Windows 9x.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.