472,986 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

Classic "Who is logged-on" problem...

I have a form that I choose from a list of database names and a list
within a textbox comes up with the computer ID number and an associated
(from a table) user name. Is there a way from looking at this code to
tell the network ID name? I hope not to have to write the code with the
"Dev Ashish" API code, which I'm sure is great but I can't figure out
how to implement it in a form like mine. Please look at the below code
and tell if I can modify it to give network ID. I'm using A2000 and
without access security in place.
Thanks!

Option Compare Database
Option Explicit
Private Const ms_UNKNOWN As String = "Unknown User"

Private Function CreateListHeader() As String
Dim strFill As String
strFill = "No.;Computer ID;User Name;Connected?;Suspect State?;"
CreateListHeader = strFill
End Function

Private Sub cboDBName_Change()
Dim strDB As String
Dim strMsg As String
If IsBlank(Me.cboDBName) Then
strMsg = "Unable to perform operation without a valid database
name."
MsgBox strMsg, vbInformation, "Database Name is Blank"
Exit Sub
Else
strDB = Me.cboDBName
ShowUsersInDB (strDB)
End If

End Sub

Private Sub cboDBName_Click()
Dim strDB As String
Dim strMsg As String
strDB = Me.cboDBName
ShowUsersInDB (strDB)
End If

End Sub

Private Sub ShowUsersInDB(ByVal strDB As String)
Dim cnn As New ADODB.Connection
Dim rec As ADODB.Recordset
Dim fld As ADODB.Field
Dim intUser As Integer
Dim intFldCount As Integer
Dim varVal As Variant
Dim strPC As String
Dim strUser As String
Dim strConnected As String
Dim strSuspect As String
Dim strData As String
Dim strInfo As String

'get the db name
strFile = GrabDBPath(strDB)

DoCmd.Hourglass True
'if it gets here - see who is in the database
strData = CreateListHeader
strFile = strFile & strDB & DB_EXT

'open the connection and recordset
cnn.Open JET_PROVIDER & "Data Source=" & strFile & ";"
Set rec = cnn.OpenSchema(Schema:=adSchemaProviderSpecific,
SchemaId:=adhcUsers)

With rec
Debug.Print rec.RecordCount
Do Until .EOF
intUser = intUser + 1
strNo = CStr(intUser) & ";"
intFldCount = 0
For Each fld In .Fields
intFldCount = intFldCount + 1
varVal = fld.Value
Select Case intFldCount
Case 1
strPC = StripNullChar(varVal)
Case 2 'get the user name for the ID
If IsNull(varVal) Then
strUser = ms_UNKNOWN & ";"
Else
strUser = GrabUserName(strPC)
End If
Case 3
strConnected = StripNullChar(varVal)
Case 4 'suspected state
If IsNull(varVal) Then
strSuspect = "False" & ";"
Else
strSuspect = StripNullChar(varVal)
End If
End Select
Next
strData = strData & strNo & strPC & strUser & strConnected
& strSuspect
.MoveNext
Loop
End With

Me.lstUsers.RowSource = strData

rec.Close
Set rec = Nothing
Set fld = Nothing
Set cnn = Nothing
End Sub

Private Function GrabUserName(ByVal strPCID As String) As String

Dim cnn As New ADODB.Connection
Dim cmd As ADODB.Command
Dim rec As ADODB.Recordset
Dim strSQL As String
Dim strUser As String

Set cnn = CurrentProject.Connection
Set cmd = New ADODB.Command

strSQL = "SELECT * FROM tblUserIDs WHERE PCID Like '" &
Left$(strPCID, Len(strPCID) - 1) & "'"

cmd.ActiveConnection = cnn
cmd.CommandType = adCmdText
cmd.CommandText = strSQL

Set rec = cmd.Execute

If Not rec.EOF Then
strUser = rec(1)
Else
strUser = ms_UNKNOWN
End If

rec.Close
cnn.Close
Set cnn = Nothing

End Function

Apr 24 '06 #1
10 3037
I use this code.
Private Declare Function apiGetComputerName Lib "kernel32" Alias
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSMachineName() As String
'Returns the computername
Dim lngLen As Long, lngX As Long
Dim strCompName As String

lngLen = 16
strCompName = String$(lngLen, 0)
lngX = apiGetComputerName(strCompName, lngLen)
If lngX <> 0 Then
fOSMachineName = Left$(strCompName, lngLen)
Else
fOSMachineName = ""
End If
End Function

If you put =fOSMachineName() in the control source of a unbound text
box, it should give you what you want. I also use this code to return
the user name. Same thing, put =GetNTUser() in the control source of
an unbound text box.

Private Declare Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Function GetNTUser() As String
Dim strUserName As String
'Create a buffer
strUserName = String(100, Chr$(0))
'Get user name
GetUserName strUserName, 100
'Strip the rest of the buffer
strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
GetNTUser = strUserName
End Function

Parasyke wrote:
I have a form that I choose from a list of database names and a list
within a textbox comes up with the computer ID number and an associated
(from a table) user name. Is there a way from looking at this code to
tell the network ID name? I hope not to have to write the code with the
"Dev Ashish" API code, which I'm sure is great but I can't figure out
how to implement it in a form like mine. Please look at the below code
and tell if I can modify it to give network ID. I'm using A2000 and
without access security in place.
Thanks!

Option Compare Database
Option Explicit
Private Const ms_UNKNOWN As String = "Unknown User"

Private Function CreateListHeader() As String
Dim strFill As String
strFill = "No.;Computer ID;User Name;Connected?;Suspect State?;"
CreateListHeader = strFill
End Function

Private Sub cboDBName_Change()
Dim strDB As String
Dim strMsg As String
If IsBlank(Me.cboDBName) Then
strMsg = "Unable to perform operation without a valid database
name."
MsgBox strMsg, vbInformation, "Database Name is Blank"
Exit Sub
Else
strDB = Me.cboDBName
ShowUsersInDB (strDB)
End If

End Sub

Private Sub cboDBName_Click()
Dim strDB As String
Dim strMsg As String
strDB = Me.cboDBName
ShowUsersInDB (strDB)
End If

End Sub

Private Sub ShowUsersInDB(ByVal strDB As String)
Dim cnn As New ADODB.Connection
Dim rec As ADODB.Recordset
Dim fld As ADODB.Field
Dim intUser As Integer
Dim intFldCount As Integer
Dim varVal As Variant
Dim strPC As String
Dim strUser As String
Dim strConnected As String
Dim strSuspect As String
Dim strData As String
Dim strInfo As String

'get the db name
strFile = GrabDBPath(strDB)

DoCmd.Hourglass True
'if it gets here - see who is in the database
strData = CreateListHeader
strFile = strFile & strDB & DB_EXT

'open the connection and recordset
cnn.Open JET_PROVIDER & "Data Source=" & strFile & ";"
Set rec = cnn.OpenSchema(Schema:=adSchemaProviderSpecific,
SchemaId:=adhcUsers)

With rec
Debug.Print rec.RecordCount
Do Until .EOF
intUser = intUser + 1
strNo = CStr(intUser) & ";"
intFldCount = 0
For Each fld In .Fields
intFldCount = intFldCount + 1
varVal = fld.Value
Select Case intFldCount
Case 1
strPC = StripNullChar(varVal)
Case 2 'get the user name for the ID
If IsNull(varVal) Then
strUser = ms_UNKNOWN & ";"
Else
strUser = GrabUserName(strPC)
End If
Case 3
strConnected = StripNullChar(varVal)
Case 4 'suspected state
If IsNull(varVal) Then
strSuspect = "False" & ";"
Else
strSuspect = StripNullChar(varVal)
End If
End Select
Next
strData = strData & strNo & strPC & strUser & strConnected
& strSuspect
.MoveNext
Loop
End With

Me.lstUsers.RowSource = strData

rec.Close
Set rec = Nothing
Set fld = Nothing
Set cnn = Nothing
End Sub

Private Function GrabUserName(ByVal strPCID As String) As String

Dim cnn As New ADODB.Connection
Dim cmd As ADODB.Command
Dim rec As ADODB.Recordset
Dim strSQL As String
Dim strUser As String

Set cnn = CurrentProject.Connection
Set cmd = New ADODB.Command

strSQL = "SELECT * FROM tblUserIDs WHERE PCID Like '" &
Left$(strPCID, Len(strPCID) - 1) & "'"

cmd.ActiveConnection = cnn
cmd.CommandType = adCmdText
cmd.CommandText = strSQL

Set rec = cmd.Execute

If Not rec.EOF Then
strUser = rec(1)
Else
strUser = ms_UNKNOWN
End If

rec.Close
cnn.Close
Set cnn = Nothing

End Function


Apr 25 '06 #2
I use this code.
Private Declare Function apiGetComputerName Lib "kernel32" Alias
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSMachineName() As String
'Returns the computername
Dim lngLen As Long, lngX As Long
Dim strCompName As String

lngLen = 16
strCompName = String$(lngLen, 0)
lngX = apiGetComputerName(strCompName, lngLen)
If lngX <> 0 Then
fOSMachineName = Left$(strCompName, lngLen)
Else
fOSMachineName = ""
End If
End Function

If you put =fOSMachineName() in the control source of a unbound text
box, it should give you what you want. I also use this code to return
the user name. Same thing, put =GetNTUser() in the control source of
an unbound text box.

Private Declare Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Function GetNTUser() As String
Dim strUserName As String
'Create a buffer
strUserName = String(100, Chr$(0))
'Get user name
GetUserName strUserName, 100
'Strip the rest of the buffer
strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
GetNTUser = strUserName
End Function

Parasyke wrote:
I have a form that I choose from a list of database names and a list
within a textbox comes up with the computer ID number and an associated
(from a table) user name. Is there a way from looking at this code to
tell the network ID name? I hope not to have to write the code with the
"Dev Ashish" API code, which I'm sure is great but I can't figure out
how to implement it in a form like mine. Please look at the below code
and tell if I can modify it to give network ID. I'm using A2000 and
without access security in place.
Thanks!

Option Compare Database
Option Explicit
Private Const ms_UNKNOWN As String = "Unknown User"

Private Function CreateListHeader() As String
Dim strFill As String
strFill = "No.;Computer ID;User Name;Connected?;Suspect State?;"
CreateListHeader = strFill
End Function

Private Sub cboDBName_Change()
Dim strDB As String
Dim strMsg As String
If IsBlank(Me.cboDBName) Then
strMsg = "Unable to perform operation without a valid database
name."
MsgBox strMsg, vbInformation, "Database Name is Blank"
Exit Sub
Else
strDB = Me.cboDBName
ShowUsersInDB (strDB)
End If

End Sub

Private Sub cboDBName_Click()
Dim strDB As String
Dim strMsg As String
strDB = Me.cboDBName
ShowUsersInDB (strDB)
End If

End Sub

Private Sub ShowUsersInDB(ByVal strDB As String)
Dim cnn As New ADODB.Connection
Dim rec As ADODB.Recordset
Dim fld As ADODB.Field
Dim intUser As Integer
Dim intFldCount As Integer
Dim varVal As Variant
Dim strPC As String
Dim strUser As String
Dim strConnected As String
Dim strSuspect As String
Dim strData As String
Dim strInfo As String

'get the db name
strFile = GrabDBPath(strDB)

DoCmd.Hourglass True
'if it gets here - see who is in the database
strData = CreateListHeader
strFile = strFile & strDB & DB_EXT

'open the connection and recordset
cnn.Open JET_PROVIDER & "Data Source=" & strFile & ";"
Set rec = cnn.OpenSchema(Schema:=adSchemaProviderSpecific,
SchemaId:=adhcUsers)

With rec
Debug.Print rec.RecordCount
Do Until .EOF
intUser = intUser + 1
strNo = CStr(intUser) & ";"
intFldCount = 0
For Each fld In .Fields
intFldCount = intFldCount + 1
varVal = fld.Value
Select Case intFldCount
Case 1
strPC = StripNullChar(varVal)
Case 2 'get the user name for the ID
If IsNull(varVal) Then
strUser = ms_UNKNOWN & ";"
Else
strUser = GrabUserName(strPC)
End If
Case 3
strConnected = StripNullChar(varVal)
Case 4 'suspected state
If IsNull(varVal) Then
strSuspect = "False" & ";"
Else
strSuspect = StripNullChar(varVal)
End If
End Select
Next
strData = strData & strNo & strPC & strUser & strConnected
& strSuspect
.MoveNext
Loop
End With

Me.lstUsers.RowSource = strData

rec.Close
Set rec = Nothing
Set fld = Nothing
Set cnn = Nothing
End Sub

Private Function GrabUserName(ByVal strPCID As String) As String

Dim cnn As New ADODB.Connection
Dim cmd As ADODB.Command
Dim rec As ADODB.Recordset
Dim strSQL As String
Dim strUser As String

Set cnn = CurrentProject.Connection
Set cmd = New ADODB.Command

strSQL = "SELECT * FROM tblUserIDs WHERE PCID Like '" &
Left$(strPCID, Len(strPCID) - 1) & "'"

cmd.ActiveConnection = cnn
cmd.CommandType = adCmdText
cmd.CommandText = strSQL

Set rec = cmd.Execute

If Not rec.EOF Then
strUser = rec(1)
Else
strUser = ms_UNKNOWN
End If

rec.Close
cnn.Close
Set cnn = Nothing

End Function


Apr 25 '06 #3
Sorry, should have included this. It does away with API calls, although
you will have to have a reference to Windows Script Host Object Model

Public Function GetUserName() As String
Dim wshNet As Object
Set wshNet = CreateObject("WScript.Network")
GetUserName = wshNet.username
Set wshNet = Nothing
End Function

Apr 25 '06 #4
Thanks, but I need a little more coaching... so does the below clue you
into the error? (the database name appears in the combobox but the
pesky #Name? appears in the textbox. (BTW I'll have several users to be
listed in the textbox and need it to be in a column type style).
THANKS!

I have a form created without binding to anything.

I created a combobox named cboDBName with its binding being SELECT
[tblDatabases].[DBName] FROM tblDatabases;
placed in the RowSource property.

I created a textbox named txtGetUsers with its binding being
=fOSMachineName()
placed in the ControlSource property.
I pasted this into the code for the form (I deleted some comments and
lines to simplify):

Option Compare Database
Option Explicit
Private Declare Function apiGetComputerName Lib "kernel32" Alias
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSMachineName() As String

Dim lngLen As Long, lngX As Long
Dim strCompName As String
lngLen = 16
strCompName = String$(lngLen, 0)
lngX = apiGetComputerName(strCompName, lngLen)
If lngX <> 0 Then
fOSMachineName = Left$(strCompName, lngLen)
Else
fOSMachineName = ""
End If
End Function

Private Declare Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Function GetNTUser() As String
Dim strUserName As String
strUserName = String(100, Chr$(0))
GetUserName strUserName, 100
strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
GetNTUser = strUserName
End Function

e.cboDBName

Apr 25 '06 #5
Try this link: http://www.mvps.org/access/modules/mdl0055.htm
it has a small access app that looks at the ldb file and will tell you
all of the users that are logged into mdb that you reference.
really neat and small, and the code for the form can be imported into
any app that you want or run standalone out of this app.
It has all the code you need.....

Apr 25 '06 #6
This seems better. Thanks! Much cleaner code. Is there a way to change
the database name textbox to a combobox and have it do a lookup of an
existing table of the databases and their respective paths?
Again.. Thanks!

Apr 26 '06 #7
That is what I did when I implemented it.

I created a table with the full path and db name and in my case I added
a combo box above the existing txtbox. And then in the afterupdate
event of the combo had it load the txt box with the table name. And
then as part of that same code I had it execute the code that is
executed when you push the button to get the information.

Ron

Apr 26 '06 #8
How do I get the combobox to load the chosen value into the textbox?
I tried in the afterupdate the line:

Private Sub cboDBname_AfterUpdate()
Me!txtDBPath = Me!cboDBname
End Sub

But this didn't populate the textbox.

Any clues?

Thanks!

Apr 26 '06 #9
Nevermind the above, I figured that much out but should I write my code
like this in the afterupdate of the combo?:

'Code for Afterupdate event of combobox
Private Sub cboDBname_AfterUpdate()
Me!txtDBPath = Me!cboDBname

Private Sub cmdExecute_Click()
On Error GoTo ErrHandler

With Me.txtDBPath
If Not IsNull(.Value) Then
If Len(Dir$(.Value, vbNormal)) Then
Me.lbxLDBInfo.RowSourceType = vbNullString
If (Me.chkUserRoster.Enabled And (Me.chkUserRoster))
Then
m_blnUseRosterLayout = True
Me.optDisplayOptions.Enabled = False
Call sUseUserRoster
Else
Me.optDisplayOptions.Enabled = True
m_blnUseRosterLayout = False
Call sDisplayUsers
End If
Me.lbxLDBInfo.RowSourceType = "fListFill"
End If
End If
End With
ExitHere:
Exit Sub
ErrHandler:
With Err
MsgBox "Error: " & .Number & vbCrLf & .Description, _
vbCritical Or vbOKOnly, .Source
End With
Resume ExitHere
End Sub
'end code for afterupdate event for combobox

I tried this but my user box was not populated.

Apr 26 '06 #10
Actually I have done it a couple of ways.

On one of my apps I added the following to the onopen event and it
works fine.

==============================
'V:\TEAM FOLDERS\Kelly's Team\EMEA MACD BR Pilot\EMEA MACD PROJ\EMEA
DB\EMEA DB Tables.mdb
Me.txtDBPath = "V:\SHARE FOLDER\Billing Validation\Billing Validation
Tables.mdb"
' If Len(.Value) Then
' Me.chkUserRoster.Enabled = fIsJet4DB(.Value)
Me.chkUserRoster.Enabled = True
Call cmdExecute_Click
' End If
================================================== ====

Apr 27 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vik Rubenfeld | last post by:
I'm a newbie to Apache. This week I installed my first Apache 2.0 server, and it's working fine. I then installed PHP. When I ran the PHP test file ("test.php"), the actual text contents of the...
99
by: Jim Hubbard | last post by:
It seems that Microsoft not only does not need the classic Visual Basic developer army (the largest army of developers the world has ever seen), but now they don't need ANY Windows developer at a...
0
by: gswitz | last post by:
I have written a Windows Service that watches a Lotus Notes InBox for emails, detaches attachments from the emails and attaches them to a different Lotus Notes Database. The service works...
6
by: Fan Ruo Xin | last post by:
Last monday, I tried to create a working table, and failed because of 'Log Full'. There were two applications running at that time - one is autoload (during the split phase), another one is "insert...
1
by: DB_2 | last post by:
Greetings, I was searching Google for ways to turn off transaction logging for some queries. I came across this old post from Feb 2003: > From: fareeda (fareeda@pspl.co.in) > Subject: Re:...
1
by: Daniel Chou | last post by:
Hello, I have two questions about "not logged initially": 1. Before using "alter table tbname activate not logged initially", should the table be created with "not logged initially"? 2....
5
by: Dan C Douglas | last post by:
I have just installed VS.NET 2003 on my computer. I have a project that I have been developing on VS.NET 2002. I haven't upgraded this project to VS.NET 2003 yet and I am still developing it in...
3
by: aydeejay | last post by:
I'm trying to troubleshoot an issue where users are not able to bind with LDAP via "GetObject" through our ASP Classic Intranet if they stay logged in overnight (beyond their allowed login hours). ...
0
by: =?Utf-8?B?cmZsYXphcm8=?= | last post by:
Hi All, We are trying to build an automation utility to configure OS. I found a way to automate the process below via registry: Control Panel -Taskbar and Start Menu ->Start Menu Tab --Select...
1
by: =?Utf-8?B?SkI=?= | last post by:
Greetings, I am getting an "Access is denied" Error when calling objects from the AdminIndexServerClass from an ASP.NET application. I use this object to perform a simple rescan on a Catalog after...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.