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

Listbox on form showing users logged on to dbase. 4

Status
Not open for further replies.

metalboy

Technical User
Apr 1, 2004
97
GB
is it possible to have a form with a list box which shows all users logged onto the database?

Regards

Alex

“It is a mistake to think you can solve any major problems just with potatoes.”
Douglas Adams
 
1. create a new module mdl_UserName and paste the following code in it:

Code:
Option Compare Database
Option Explicit

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

Public Function UserName() As String

Dim lngValue As Long
Dim strUserName, strBuffer As String

strUserName = ""
strBuffer = Space(255)
lngValue = GetUserName(strBuffer, 255)
If lngValue Then
strUserName = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
End If
UserName = strUserName

End Function

2. create a table tbl_UserName with a stringfield UserName

3. assuming u have a startup form in your DB, put the following code in the on_open event of the form:

Code:
sub form_open()
docmd.runsql "insert into tbl_UserName (UserName) select '" & UserName & "';"
end sub

4. make sure, that your DB is not closed via the "x" of the application window (there are ways to disable this, let me know, if you need help), put the following code in a button, that closes the application:

Code:
sub cmd_close()
docmd.runsql "delete t.* from tbl_UserName t where t.UserName like '" & UserName & "';"
docmd.quit
end sub

5. the table tbl_USerName contains the usernames of the users logged on to yur db

HTH,
fly



Martin Serra Jr.
 
This is good stuff, but I have to ask, is there a way to include the machine name the user is on? Got a couple of systems that are "General Use"

ChaZ

Ascii dumb question, get a dumb Ansi
 
Provided the users aren't on Win9x box, you may consider this:
Environ("COMPUTERNAME")
Environ("USERNAME")

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Tracking users yourself is a redundant effort, since a user list is already maintained by Access:

The User List

The user list feature provides a way of determining who is currently connected to a Microsoft Jet database. The list can be obtained via the ADO programming interface and returns the following information for each user:
[ul]
[li]Name of the computer being used.[/li]
[li]Security name, that is, the user ID.[/li]
[li]Whether or not the user is currently connected to the database (A user's ID remains in the lock database until the last user disconnects or until the slot is reclaimed for a new user connection.).[/li]
[li]Whether or not the user connection was terminated normally.[/li]
[/ul]

Code:
    ' The user roster is exposed as a provider-specific
    ' schema rowset in the Jet 4 OLE DB provider.  You have to use
    ' a GUID to reference the schema, as provider-specific schemas
    ' are not listed in ADO's type library for schema rowsets
    
    Set rs = cn.OpenSchema(adSchemaProviderSpecific, , _
    "{947bb102-5d43-11d1-bdbf-00c04fb92675}")
    
    ' Output the list of all users in the current database.
    Debug.Print rs.Fields(0).Name, "", rs.Fields(1).Name, _
    "", rs.Fields(2).Name, rs.Fields(3).Name
    
    Do While Not rs.EOF
       Debug.Print rs.Fields(0), rs.Fields(1), _
       rs.Fields(2), rs.Fields(3)
       rs.MoveNext
    Loop

For more details, see the KB:

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
flyover789:

Sorry for hi-jacking the thread, but I would be interested to know how you disable the "x" for the Access window (unless you're just hiding the Access window??).

Thanks,

Graene

website:
 
Thanks for the star...

For viewers using Access Project (tables on an SQL server), Martin's (flyover789) code works fine with just a slight tweak on the SQL used for deletion. In that case, you need:

Code:
sub cmd_close()
DoCmd.runsql "Delete From tbl_UserName Where UserName Like '" & UserName & "';"
docmd.quit
end sub

I'm liking this a LOT!

So, a star to Martin from me.



HTH,
Bob [morning]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top