Guest User

Untitled

a guest
Jun 21st, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. Table: [CurrentConnections]
  2. computerName Text(255), Primary Key
  3.  
  4. Table: [ConnectionLog]
  5. computerName Text(255), Primary Key
  6. userName Text(255)
  7.  
  8. Public Sub GetCurrentlyConnectedMachines()
  9. Dim cdb As DAO.Database, rst As DAO.Recordset
  10. Dim fso As Object '' FileSystemObject
  11. Dim lck As Object '' ADODB.Stream
  12. Dim lockFileSpec As String, lockFileExt As String, tempFileSpec As String
  13. Dim buffer() As Byte
  14.  
  15. Set cdb = CurrentDb
  16. cdb.Execute "DELETE FROM CurrentConnections", dbFailOnError
  17. Set rst = cdb.OpenRecordset("SELECT computerName FROM CurrentConnections", dbOpenDynaset)
  18.  
  19. lockFileSpec = Application.CurrentDb.Name
  20. If Right(lockFileSpec, 6) = ".accdb" Then
  21. lockFileExt = ".laccdb"
  22. Else
  23. lockFileExt = ".ldb"
  24. End If
  25. lockFileSpec = Left(lockFileSpec, InStrRev(lockFileSpec, ".", -1, vbBinaryCompare) - 1) & lockFileExt
  26.  
  27. '' ADODB.Stream cannot open the lock file in-place, so copy it to %TEMP%
  28. Set fso = CreateObject("Scripting.FileSystemObject") '' New FileSystemObject
  29. tempFileSpec = fso.GetSpecialFolder(2) & "" & fso.GetTempName
  30. fso.CopyFile lockFileSpec, tempFileSpec, True
  31.  
  32. Set lck = CreateObject("ADODB.Stream") '' New ADODB.Stream
  33. lck.Type = 1 '' adTypeBinary
  34. lck.Open
  35. lck.LoadFromFile tempFileSpec
  36. Do While Not lck.EOS
  37. buffer = lck.Read(32)
  38. rst.AddNew
  39. rst!computerName = DecodeSZ(buffer)
  40. rst.Update
  41. buffer = lck.Read(32) '' skip accessUserId, (almost) always "Admin"
  42. Loop
  43. lck.Close
  44. Set lck = Nothing
  45. rst.Close
  46. Set rst = Nothing
  47. Set cdb = Nothing
  48. fso.DeleteFile tempFileSpec
  49. Set fso = Nothing
  50. End Sub
  51.  
  52. Private Function DecodeSZ(buf() As Byte) As String
  53. Dim b As Variant, rt As String
  54. rt = ""
  55. For Each b In buf
  56. If b = 0 Then
  57. Exit For '' null terminates the string
  58. End If
  59. rt = rt & Chr(b)
  60. Next
  61. DecodeSZ = rt
  62. End Function
  63.  
  64. Private Sub Form_Load()
  65. Dim cdb As DAO.Database, rst As DAO.Recordset
  66. Dim wshNet As Object '' WshNetwork
  67.  
  68. Set wshNet = CreateObject("Wscript.Network")
  69. Set cdb = CurrentDb
  70. Set rst = cdb.OpenRecordset("SELECT * FROM ConnectionLog", dbOpenDynaset)
  71. rst.FindFirst "ComputerName=""" & wshNet.computerName & """"
  72. If rst.NoMatch Then
  73. rst.AddNew
  74. rst!computerName = wshNet.computerName
  75. Else
  76. rst.Edit
  77. End If
  78. rst!userName = wshNet.userName
  79. rst.Update
  80. Set wshNet = Nothing
  81. End Sub
  82.  
  83. SELECT CurrentConnections.computerName, ConnectionLog.userName
  84. FROM CurrentConnections LEFT JOIN ConnectionLog
  85. ON CurrentConnections.computerName = ConnectionLog.computerName
  86. ORDER BY ConnectionLog.userName;
  87.  
  88. Private Sub Form_Load()
  89. UpdateFormData
  90. End Sub
  91.  
  92. Private Sub cmdRefresh_Click()
  93. UpdateFormData
  94. End Sub
  95.  
  96. Private Sub UpdateFormData()
  97. GetCurrentlyConnectedMachines
  98. Me.Requery
  99. End Sub
  100.  
  101. Private Sub Form_Unload(Cancel As Integer)
  102. Dim wshNet As Object
  103. Dim deleteSQL As String
  104.  
  105. Set wshNet = CreateObject("WScript.Network")
  106. DoCmd.SetWarnings False
  107. deleteSQL = "DELETE tblCurrentConnections.* " & _
  108. "FROM tblCurrentConnections WHERE[computerName] = '" & wshNet.computerName & "';"
  109. DoCmd.RunSQL deleteSQL
  110. DoCmd.SetWarnings True
  111. End Sub
Add Comment
Please, Sign In to add comment