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

User identification

Status
Not open for further replies.

Katana2003

Programmer
Jun 25, 2003
23
GB
I am currently creating a user front end for an estimating system that creates standard quotes. On the bottom of every quote that is created there is a signed by.I am looking to pick up the name of who ever is logged in on the machine is this possible. If so I am open for any suggestions on the code.

 
Application.UserName will get the name that is stored in the User Name area of the Options Menu. But that is only specific to the Application (say, Excel).

There are ways to get the "logon" name of the user but the "Logon" name might not be a proper name. It may only be a "User" name which you would then need to compare to a list of names in order to get the "Real" name.

Does that make any sense?

********************
What's the best way to get the answers you need?? See FAQ222-2244 for details!
 
I use this code - it has always worked well for me. Stick it in a separate module works best.

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


Regards

Richard
 
But again, if the Login name is in the form of:
"FirstLetterOfFirstName" & "LastName"
(for example: "bsmith" or "Blee" or "mRogers")
as is common with many companys, this would not be very good use as a "Signed By" field. You would need some way to recognize "bsmith" as Bryan Smith and "Blee" as Bruce Lee etc...


********************
What's the best way to get the answers you need?? See FAQ222-2244 for details!
 
The company that i work for uses full first name and full second name for the loggin so what is your suggestion on the code ssVBAdev



 
Well, If the full First Name and Full Last Name are separated by a space (ie Bill Smith) then Excellent!! tbl's code above should work (although I haven't look through it - I'm sure it works).

But, if the names are separated by, say a "_" or something like that then you will need to spearate the names in code... not hard to do. Just some string manipulation. I would direct you to loook into the mid() left() right() and len() functions.

********************
What's the best way to get the answers you need?? See FAQ222-2244 for details!
 
There are 2 separate issues here. Application.UserName will just look up the registered name of the Excel application as set under TOOLS/OPTIONS. You can set this to anything you like. My user logon detection detects the person who is currently logged on to the PC. Once you have the log-on name you can convert it easily to any other text as required with set of CASE or IF statements and a list of possible users, so that fred_blogs translates to Fred Bloggs etc.

Richard
 
tbl:
You said "Application.UserName will just look up the registered name of the Excel application as set under TOOLS/OPTIONS. You can set this to anything you like.
That is what I said in my first response to Katana2003. I know people can set this to whatever they want. In my experience, I know people tend to set this to their full name (with a space in between the names) so that they can use it in letters and such. Not the best method, I know, but it is still 1 method.

You also said "My user logon detection detects the person who is currently logged on to the PC. Once you have the log-on name you can convert it easily to any other text as required with set of CASE or IF statements and a list of possible users, so that fred_blogs translates to Fred Bloggs etc."

I also covered this in my last post. But, If you have "fred_blogs", you don't even need "If" statements. It is simple string manipulation.
But, if you have "timothyjones" how do write code to know where the "First Name" and "Last Name" begin and end? If there is no separator you would have to have a list of every first name possible in order to separate the two. Not every Login name is going to be "Fred Jones" or "Fred_Jones." They might be "fredjones" or "fjones" (as I have attempted to explain).

I'm not saying that it cannot be done, I am simply saying that there are many ways that this can be done to suit Katana's needs, BUT, there could be problems if all the possibilities are not thought of.

********************
What's the best way to get the answers you need?? See FAQ222-2244 for details!
 
thanks for all the help i have got it working the coding tips were exalent thanks again

 
Sorry about this but the coding you gave me worked once and when I re-booted the computer it has just stopped working
What should I do and also where should I be placing this code should i put it in a text box and if so when function should I place it under
e.g. (on click)

Sorry about this I am just having a bad day.
 
You can cut and paste tbl's code directly to any Module in your project or it can be put in the Form to which the call for the function GetUserLoginName() will be made. I would suggest putting it in a separate Module.

Then, call the function from anywhere you need it in your code. ie:

txtUserName.Value = GetUserLoginName



********************
What's the best way to get the answers you need?? See FAQ222-2244 for details!
 
Thanks again i have done some testing and it is working fine now had a re-start and still working thanks

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top