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

How do I get the Logon_User? 1

Status
Not open for further replies.

riarc

Programmer
Dec 6, 2001
44
US
I a VB app and I want to know the user that has logged onto the computer so I can enter a audit log in my DB. How can I do that?
 
Hi,
What exactly are you using, are you using VB 6 as a front end to an Access DB?

I know in VBA you can use the Environ$("UserNAme") to get the logged-on user in Windows NT (won't work for 9x).

I hope this is of some help... Kyle ::)
 
We are using VB 6 and the client PC are win 2000, some are XP. and the server is 2000. My DB is a SQL server.
 
So the user's don't have individual logins to the SQL server...?

Well, I don't know if the function works in VB or on Win 2000 but it should, so create a new project and try:

msgbox Environ$("UserName")

and see what you get. Other than that I can't be of any help, since I'm not aware of any way to get the username off the lan besides the Environ$() function or the API call which is the EXACT same thing... Kyle ::)
 
That works.. thanks..I give the users access to a global DB where I have a table that tells me what applications the users can access and access level. I use NT auth. for that. But I don't give them rights application databases. I create a proxy acount for each app and I grant rights to the proxy. Thanks for your help.
 
Tbanks KyleS. That deserves a star. I had not known about the Eviron$ function. I checked on msdn.microsoft.com to be sure that it was present in VB.Net and it is. I'm not writing any more code, including Control Arrays, that does not translate to "native" VB.Net i.e. no VB6 compatibility classes. Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks, John, and that's good to know that it's in VB.NET as well (I don't want to rewrite any more than I have to either...)

riarc, glad to hear it works for you (and now I know for a fact that the variable works in Win 2K as well as XP)

John, one more thin, what's the address to check on variables like that?

Thanks again, Kyle Kyle ::)
 
How about using the GetUserName function in the WinAPI:

Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

nSize = Len(sUser)
nSuccess = GetUserName(sUser, nSize)
If nSuccess <> 0 Then
sUserName = Left(sUser, nSize - 1)
Else
'cant get it
End If

or use the WSH if it's installed:

Err.Clear
On Error Resume Next
Set oNetwork = CreateObject(&quot;WScript.Network&quot;)
If Err.Num = 0 Then
sUserName = oNetwork.UserName
Else
'cant get it
End If Jon Hawkins
 
Just because I want to share.....
Some of what you're talking about is beyond me - just - but I only discovered 'environ()/environ$()' 2 weeks ago after much heartache trying to understand (unsuccessfully) api calls.

For anyone just looking for information there is a list of parameters that can be applied to environ($) if you type set at a dos prompt you get this list and the current settings. This will save time if you are trying to discover whether or not the environ($) function will work for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top