Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I Need the computer name...

Status
Not open for further replies.

jhowley58

Programmer
Jun 25, 2001
207
0
0
US
Hi,

Ive got a set of access tables(.mdb) residing on a W2K server.

W2K and W98 Workstations running Access 2000 link to these tables.

Each workstation has different requirements associated with it.

I need some way to obtain the name if the workstation (it's network name?) using VBA from within Access 2000.

Any ideas guys?

I'll post this on the other access forums also


JohnH
 
Here are two fucntions, one to get all environment variables and another to just get the login ID. I got this from someone off of here so I take no credit.

-------------begin code-------

Option Compare Database
Option Explicit

Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
(ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long

Public Function GetUserLoginName(Optional StripNullTerminator As Boolean = True) As String
Dim strName As String
Dim strResult As String
strName = Space(255)
Call WNetGetUser("", strName, Len(strName))
strResult = Trim$(strName)
'Not sure we want this - should this be optional true or false
If StripNullTerminator Then
If Asc(Right$(strResult, 1)) = 0 Then
strResult = Left$(strResult, Len(strResult) - 1)
End If
End If
GetUserLoginName = strResult
End Function 'GetUserLoginName

Public Function GetEnvironmentSettings() As Variant
Dim strEnviron As String
Dim lngIndex As Long
Dim strMessage As String
Dim lngPathLength As Long
Dim avarEnviron As Variant
Dim varValue As Integer
Dim reference As String

lngIndex = 1
ReDim avarEnviron(1, lngIndex)
Do Until Len(Environ$(lngIndex)) = 0
strEnviron = Environ$(lngIndex) 'Get environment variables
ReDim Preserve avarEnviron(1, lngIndex)
avarEnviron(0, lngIndex) = Left$(strEnviron, InStr(strEnviron, "=") - 1)
avarEnviron(1, lngIndex) = Mid$(strEnviron, InStr(strEnviron, "=") + 1)
' MsgBox "Environ$(" & lngIndex & "): " & strEnviron
If Left$(strEnviron, 5) = "PATH=" Then
lngPathLength = Len(Environ$("PATH")) ' Get length.
strMessage = "PATH entry = " & lngIndex & " and length = " & lngPathLength
' Exit Do
End If
lngIndex = lngIndex + 1
Loop
strEnviron = ListEnvironmentArray(avarEnviron)
MsgBox "Environment variables for this PC:" & vbCrLf & vbCrLf & strEnviron
GetEnvironmentSettings = reference
End Function 'GetEnvironmentSettings

Private Function ListEnvironmentArray(ByRef ravarEnviron As Variant) As String
Dim ialng As Long
Dim str As String
For ialng = 1 To UBound(ravarEnviron, 2) 'Skip 0 because will always be empty
str = str & ravarEnviron(0, ialng) & ": " & ravarEnviron(1, ialng) & vbCrLf
Next ialng
ListEnvironmentArray = str
End Function

----------end code--------
 
The following function will return the name of the user's computer. Create a new module and insert this code.

Public Declare Function apiGetComputerName Lib "kernel32.dll" Alias "GetComputerNameA" (ByVal buffer As String, ByRef nsize As Long) As Boolean

Function GetComputerName() As String

'********************************
'* Declaration Specifications *
'********************************

Const MAXCHAR As Integer = 50 'Max. no. of chars. in computer name

Dim i As Integer 'Loop counter
Dim strHost As String 'Name of computer returned by function

'*****************************************************************************************
'* Get the name of the computer *
'* *
'* NOTE: All Windows API calls, except those dealing directly with COM, user LPSTRs. *
'* DLLs cannot change the size of an LPSTR string once it has been created. *
'* Consequently, the string passed to the DLL needs to be big enough to accept *
'* the data to be returned before you pass it to the DLL. This is the reason *
'* why I fill the string below with enough characters to create a buffer for the *
'* DLL to fill in. I filled it with Asc 0 because this value cannot be entered *
'* via the keyboard. Therefore, a ComputerName should not contain an Asc 0. *
'*****************************************************************************************

strHost = String(MAXCHAR, 0)
apiGetComputerName strHost, MAXCHAR

'******************************************************************************
'* Return the name of the computer to the caller (minus the Asc 0 code(s)). *
'******************************************************************************

For i = 1 To MAXCHAR

If (Asc(Mid(strHost, i, 1)) = 0) Then

If (i = 1) Then
GetComputerName = vbNullString
Err.Raise vbObjectError + 20100, "Error occcurred in function GetComputerName", "This function (GetComputerName) could not determine the name of your computer." & vbCrLf & vbCrLf & "(NOTE: One possible reason for this error is that the name of your computer is >= " & MAXCHAR & " characters.)" & vbCrLf & vbCrLf & "Contact IS Support, immediately, so that they can fix the problem."
Else
GetComputerName = Mid(strHost, 1, i - 1)
End If

Exit Function

End If

Next i


End Function
 
Thanks Fancy (lol! where do you get these names.. :eek:)

I'll try that as well - seems more like what I need actually..

JohnH
 
what about

Environ("computername")

on second thought, I don't believe that works in Win9x, but does in Win2000, should in WinXP, as well as should in WinNT.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top