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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get computer name/number 1

Status
Not open for further replies.

jojones

Programmer
Dec 4, 2000
104
AU
I wonder if it is possible to get the name/number of the computer that a database is open/processing on. ie.. my PC on the network is number 544.

I want to identify the number of the computer and then write data to another database, but the path of the database will depend on which PC the processing is happening on.

Does this make sense? Hope so.
Jo :)
 
I have just realised that I can work this another way. If the database is open on my PC, for example, then it's name will be G:\..\..\db.mdb. (The db is actually on a colleagues C:\ which I map to my G:\.) I can get the currentdb.name and if the left 3 characters = G:\ then I know it is not running on the owners PC and can direct the output accordingly. If the left 3 characters = C:\ then I know it is running on the owners PC.

I am still interested in knowing how to identify the computer name/number if anyone knows this.

Thanks
Jo
 
Look at the options in Environ()
with Environ("user") I handle who's logon. I think the same is possible with comp. name
 
Yes, as long as you're using WinNT or 2000+ Environ$("UserName") will return the windows logon-name of the user, and Environ$("ComputerName") will return the Network name of the PC that is being used. Play with it and see for yourself, to get a list of all the options, try this:

Sub TestEnv()
Dim i As Integer
For i = 1 To 200
Debug.Print Environ$(i)
Next i

End Sub

Here's a list of the Environ functions.

ComSpec
FILE_SERVER
HOMEDRIVE
HOMEPATH
LOGONSERVER
NUMBER_OF_PROCESSORS
NWLANGUAGE
NWUSERNAME
OS
Os2LibPath
Path
PATHEXT
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
SMS_LOCAL_DIR
SystemDrive
SystemRoot
TEMP
TMP
USERDOMAIN
USERNAME
USERPROFILE
windir
WINDOWS_LOGIN Kyle [pc1]
 
However, if you're on a WinNT network but using a Win98 machine, you need this code. Copy the code and place it into a form called frmConnections. The form has two buttons, 'OK' and 'Update' and one unbound text box called 'LoggedOn'.

Just copy it and paste it into the class module and you're off!

Code:
Option Compare Database

' Declare a record type to break down the user info

Private Type UserRec
   bMach(1 To 32) As Byte  ' 1st 32 bytes hold machine name
   bUser(1 To 32) As Byte  ' 2nd 32 bytes hold user name
End Type

Private Sub Form_Open(Cancel As Integer)
   
   Me.LoggedOn.RowSource = WhosOn()

End Sub

Private Sub LoggedOn_DblClick(Cancel As Integer)
    Dim itm As Variant
    
    For Each itm In Me.LoggedOn.ItemsSelected
        TranslateMachineID (Me.LoggedOn.Column(0))
    Next itm

End Sub

Private Sub OKBtn_Click()
   
   DoCmd.Close

End Sub

Private Sub UpdateBtn_Click()
    
    Me.LoggedOn.RowSource = WhosOn()

End Sub

'-------------------------------------------------------------------------------------
'   Subject : WhosOn()
'   Purpose : Will read *.LDB file and read who's currently
'             logged on and their station name.
'
'             The LDB file has a 64 byte record.
'
'             The station name starts at byte 1 and is null
'             terminated.
'
'             Log-in names start at the 33rd byte and are
'             also null terminated.
'
'             I had to change the way the file was accessed
'             because the Input() function did not return
'             nulls, so there was no way to see where the
'             names ended.
'-------------------------------------------------------------------------------------
Private Function WhosOn() As String

On Error GoTo Err_WhosOn

   Dim iLDBFile As Integer, iStart As Integer
   Dim iLOF As Integer, i As Integer
   Dim sPath As String, x As String
   Dim sLogStr As String, sLogins As String
   Dim sMach As String, sUser As String
   Dim rUser As UserRec    ' Defined in General
   Dim dbCurrent As Database

' Get Path of current database.  Should substitute this code
' for an attached table path in a multi-user environment.

   Set dbCurrent = DBEngine.Workspaces(0).Databases(0)
   sPath = dbCurrent.Name
   dbCurrent.Close

' Iterate thru dbCurrent.LDB file for login names.

   sPath = Left(sPath, InStr(1, sPath, ".")) + "LDB"

' Test for valid file, else Error

   x = Dir(sPath)
   iStart = 1
   iLDBFile = FreeFile

   Open sPath For Binary Access Read Shared As iLDBFile
   iLOF = LOF(iLDBFile)
   Do While Not EOF(iLDBFile)
      Get iLDBFile, , rUser
      With rUser
         i = 1
         sMach = ""
         While .bMach(i) <> 0
            sMach = sMach & Chr(.bMach(i))
            i = i + 1
         Wend
         i = 1
         sUser = &quot;&quot;
         While .bUser(i) <> 0
            sUser = sUser & Chr(.bUser(i))
            i = i + 1
         Wend
      End With
      sLogStr = sMach & &quot; -- &quot; & sUser
      If InStr(sLogins, sLogStr) = 0 Then
         sLogins = sLogins & sLogStr & &quot;;&quot;
      End If
      iStart = iStart + 64 'increment to next record offset
   Loop
   Close iLDBFile
   WhosOn = sLogins

Exit_WhosOn:
   Exit Function

Err_WhosOn:
   If Err = 68 Then
      MsgBox &quot;Couldn't populate the list&quot;, 48, &quot;No LDB File&quot;
   Else
      MsgBox &quot;Error: &quot; & Err.Number & vbCrLf & Err.Description
      Close iLDBFile
   End If
   Resume Exit_WhosOn

End Function

Function TranslateMachineID(strStringToProcess As String)
    Dim intEndOfMachineID As Integer
    Dim strMachineID As String
    intEndOfMachineID = InStr(strStringToProcess, &quot;-&quot;) - 2
    strMachineID = Left(strStringToProcess, intEndOfMachineID)
    
    RetVal = Shell(&quot;nbtstat -a &quot; & strMachineID, vbNormalFocus)  'Get User ID
End Function
Onwards,

Q-
 
Here is a function that will work across environments

'-- reference, put after option explicit in module
Global Const JET_SCHEMA_USERROSTER = _
&quot;{947bb102-5d43-11d1-bdbf-00c04fb92675}&quot;

Function ADOUserRoster()

Dim cnn As New ADODB.Connection
Dim rst As ADODB.Recordset

' Open the connection for any mdb
cnn.Open &quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot; & _
&quot;Data Source=.\NorthWind.mdb;&quot;
' if you want current mdb
'set cnn = CurrentProject.Connection

' Open the user roster schema rowset
Set rst = cnn.OpenSchema(adSchemaProviderSpecific, , _
JET_SCHEMA_USERROSTER)

' Print the results to the debug window
Debug.Print rst.GetString

cnn.Close
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top