Get Computer Name/Identifier (1 Viewer)

ajetrumpet

Banned
Local time
Today, 09:48
Joined
Jun 22, 2007
Messages
5,638
This code will give you the name of the computer that you are on. For personal computers, I believe this is the name of the PC that you type into one of the textboxes on the welcome screen. I believe, by default for windows machines, the name is: Windows User Name + "-PC"

PHP:
Declare Function GetComputerName& Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long)
PHP:
Function GetIdentifier()

    Dim lpBuff As String * 1314
    GetComputerName lpBuff, Len(lpBuff)
        MsgBox lpBuff

End Function
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 00:48
Joined
Jan 20, 2009
Messages
12,851
I just use:
Environ$("COMPUTERNAME")

For the username:
Environ$("USERNAME")
 

Banana

split with a cherry atop.
Local time
Today, 07:48
Joined
Sep 1, 2005
Messages
6,318
Just as FYI:

Environ are indeed very simple and work well. However, they are not protected and can be altered by the users in the command prompt.

Since the calls to username and computer name are usually done in context of security, it may be preferable to check API which looks into protected areas of memory that users can't tamper with (short of hijacking the administrator privilege upon the computer and altering the parameters willy-nilly).
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 00:48
Joined
Jan 20, 2009
Messages
12,851
As usual, Banana adds a valuable insight. Thanks.
 

jd_boss_hogg

Registered User.
Local time
Today, 16:48
Joined
Aug 5, 2009
Messages
88
Hi All - i know this is an old posting, but hoping someone can still help with it...

All i want to do is set the value of a field to the user name or computer name of the person that has the database open. This is just to track who is doing what. I don;t need anything clever or foolproof, just very basic. So, in a macro i have the following argument to a setvalue:-

[Forms]![myform]![myfield], Environ("Computername")

But when run, the macro returns an error:
"the expression you entered has a function name that access can't find"

I'm running 2007, most of the users will run 2003.

thanks if anyone can help !
 

67flyer

Registered User.
Local time
Today, 07:48
Joined
Jul 29, 2006
Messages
49
you need a = after forms!myform!myfield instead of a , In 2007 i had an issue with just using the Environ("computername") I had to change it to vba.environ("computername").

so your comand should look like this: forms!myform!myfield = Environ("Computername")
 

Scooter172

New member
Local time
Today, 09:48
Joined
Dec 23, 2011
Messages
2
This works great for me.. after entering this in a module (name it what you want, I called mine user name) and then when you go to properties menu for the control you want select data tab, "default Value" type =UserNameWindows()

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

Function UserNameWindows() As String
    
    Dim lngLen As Long
    Dim strBuffer As String
    
    Const dhcMaxUserName = 255
    
    strBuffer = Space(dhcMaxUserName)
    lngLen = dhcMaxUserName
    If CBool(GetUserName(strBuffer, lngLen)) Then
        UserNameWindows = Left$(strBuffer, lngLen - 1)
    Else
        UserNameWindows = ""
    End If
End Function
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:48
Joined
Feb 28, 2001
Messages
27,142
If you actually used that syntax, you got caught on quoting. Double-quotes within double-quotes don't work so well unless you know the tricks.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 15:48
Joined
Feb 19, 2013
Messages
16,607
and you don't need the =

just

msgbox Environ$("USERNAME")

this is a 10 year old post, so don't think you need the $ either, certainly in this context
 

isladogs

MVP / VIP
Local time
Today, 15:48
Joined
Jan 14, 2017
Messages
18,209
All the old functions ending in $ still work for backwards compatibility but these days the $ is normally not used: Left$, Right$, Mid$, Trim$, Environ$ etc etc should all be written without the $

As already mentioned, the Environ function can be spoofed which could cause issues
The API method is more complex and also needs modifying to work in 64bit

There is another method of obtaining user name/computer name which cannot be spoofed and works in both 32-bit & 64-bit without modification:
CreateObject("WScript.Network").ComputerName
CreateObject("WScript.Network").UserName
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:48
Joined
May 7, 2009
Messages
19,233
All the old functions ending in $ still work for backwards compatibility but these days the $ is normally not used: Left$, Right$, Mid$, Trim$, Environ$ etc etc should all be written without the $
not entirely true.
n1.png
n2.png
 

isladogs

MVP / VIP
Local time
Today, 15:48
Joined
Jan 14, 2017
Messages
18,209
Hi @arnelgp
I have the greatest respect for the authors of that book and own a copy of their two volume Access 2000 Developers Handbook which are, in my opinion, the best Access reference books ever.

Anyway, I was already aware of that quote and whilst it may have made a difference back in 2000 when the book was published, it no longer has any significance. Recently the topic was discussed and tested by adezii in this thread at Bytes.com How to speed-up access to strings - Microsoft Access / VBA (bytes.com).. He ran repeated speed tests using Mid$ & Mid. The differences was negligible and in fact Mid was very slightly faster. See post #17 of that thread.

Thanks for those screenshots as it has just prompted me to download a free PDF of that book for my own use.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:48
Joined
May 7, 2009
Messages
19,233
tested by adezii i
the test means nothing. neither is it conclusive.
see the fluctuations of values there.
it means you can't really test on a system that has many "process" on the background.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:48
Joined
Feb 28, 2001
Messages
27,142
I have to side with ArnelGP on this one. Since adezii published his numbers, I ran a quick test on them from a statistical viewpoint. The standard deviations for either of the two sets of numbers are MUCH larger than the difference between the two numbers. The difference is 0.237 seconds, approximately, in sets of numbers with a standard deviation of 2.06 or 3.66 (the MID$ has the larger deviation).

I would have to say that statistically, that difference is insignificant. Using normal-curve methodology, there is much less than a 30% probability that the numbers are different. And that was an estimate because I misplaced my calculator that would compute such a thing.
 

isladogs

MVP / VIP
Local time
Today, 15:48
Joined
Jan 14, 2017
Messages
18,209
I agree completely about the variation. The point I was making is that the differences in times between e.g. Mid$ and Mid etc are negligible and there is no real benefit in using the $ versions of these functions with modem processors and reasonable RAM

Its easy enough to test these for yourself.
I've run several tests of my own using Left, Mid, LCase and Format and their $ equivalents in a select query.

The query contained about 2.6 million records and the test looped timing running each query 100 times
Indexes were removed prior to the tests.
Each test was run 20 times and averages, Min, Max, StDev etc calculated.
No other program was running during the tests but inevitably completion times do vary
The average time differences, between using $ or not, are very small ... less than the standard deviation for each test!

1614551911186.png


NOTE: with indexes restored, both tests run slightly faster and the time differences are even smaller
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 15:48
Joined
Jan 14, 2017
Messages
18,209
The VBA for Nerds handbook is about 9MB so too big to upload.
I got it from here . Its a free download
If you have problems I can email it to you
 

Isaac

Lifelong Learner
Local time
Today, 07:48
Joined
Mar 14, 2017
Messages
8,774
With all respect for everyone involved in this discussion, I think the idea of using Left$ because it is "statistically faster", would be about the same as removing a single human hair from the nose of a jet plane before it takes off, so that it has an easier time. :ROFLMAO:

This is definitely a variation along the lines of "it doesn't matter" quantity.
 

Users who are viewing this thread

Top Bottom