Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I get the Computer Name from within VBA

597 views
Skip to first unread message

Jonathan

unread,
Jan 25, 2009, 6:44:00 PM1/25/09
to
I am writing an Access database (Access 2003 on WinXP and VISTA) in VBA and I
would like to be able to read out the computer name of the PC the database is
going to be running on.
Is there a way to do this?

Thanks

Larry Serflaten

unread,
Jan 25, 2009, 8:59:11 PM1/25/09
to

"Jonathan" <Jona...@discussions.microsoft.com> wrote

One easy way is to use enviroment settings. For a complete list try this:

For i = 1 To 50
Debug.Print i, Environ(i)
Next

Specifically, for the computer name, try this:

MsgBox Environ("COMPUTERNAME")

Just be aware that these settings can be changed by the user, but for the most
part, are usually accurate....

HTH
LFS

Jonathan

unread,
Jan 26, 2009, 2:47:28 PM1/26/09
to
Dear Larry,
Thanks! - That's it - I don't know why I never came across this function.
Solved my problem in seconds.

Karl E. Peterson

unread,
Jan 26, 2009, 6:21:23 PM1/26/09
to

Here's another way that no user can muck up...

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA"
(ByVal lpBuffer As String, nSize As Long) As Long

Private Function CurrentMachineName() As String
Dim Buffer As String
Dim nLen As Long
Const CNLEN As Long = 15 ' Maximum computer name length
Buffer = Space$(CNLEN + 1)
nLen = Len(Buffer)
If GetComputerName(Buffer, nLen) Then
CurrentMachineName = Left$(Buffer, nLen)
End If
End Function

--
.NET: It's About Trust!
http://vfred.mvps.org


0 new messages