Get User at login

G

Guest

I have the following code when my app first opens. The point to the code is
to see who is logged into to windows and depending on who is logged in, it
will open 1 of 2 forms. I have tested it with myself logged in "jculver" and
for some reason it skips opening the "ARCManagement" form and opens the
other. I can't figure out why it isn't working. Any help?

Here is the code

Option Compare Database

Public Declare Function getUserName Lib _
"advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long
Public Const MAX_BUFFER_LENGTH = 100
Public TempEvent As String

Public Function getLoggedUserName() As String
Dim strBufferString As String
Dim lngResult As Long
strBufferString = String(MAX_BUFFER_LENGTH, "X")
lngResult = getUserName(strBufferString, MAX_BUFFER_LENGTH)
getLoggedUserName = Mid(strBufferString, 1, MAX_BUFFER_LENGTH)

If getLoggedUserName = "jculver" Then
DoCmd.OpenForm ("ARCManagement")
Else
DoCmd.OpenForm ("VOARCManagement")
End If

End Function
 
D

Douglas J Steele

What's returned by the API call is padded with a Null character at the end.
Since you're not stripping that Null character off, what you've got isn't
actually equal to jculver.

If you take a look at the code in http://www.mvps.org/access/api/api0008.htm
at "The Access Web", you'll see that you should actually pass a variable
containing the buffer size to the API, not a constant. That's because the
API actually changes the value of that field to be the length of the string
returned. Subtract 1 from that value to get the length of what's before the
Null character.
 
D

David Lloyd

James:

The username is appended with the remaining Xs in your buffered string. To
get the username, you need to remove these. For example:

getLoggedUserName = Trim(Replace(getLoggedUserName, "X", ""))

There appears to be one other special character appended on the end of the
username after the Xs are removed, however, in my tests, it does not use
this when making the comparison in the IF statement.

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I have the following code when my app first opens. The point to the code is
to see who is logged into to windows and depending on who is logged in, it
will open 1 of 2 forms. I have tested it with myself logged in "jculver" and
for some reason it skips opening the "ARCManagement" form and opens the
other. I can't figure out why it isn't working. Any help?

Here is the code

Option Compare Database

Public Declare Function getUserName Lib _
"advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long
Public Const MAX_BUFFER_LENGTH = 100
Public TempEvent As String

Public Function getLoggedUserName() As String
Dim strBufferString As String
Dim lngResult As Long
strBufferString = String(MAX_BUFFER_LENGTH, "X")
lngResult = getUserName(strBufferString, MAX_BUFFER_LENGTH)
getLoggedUserName = Mid(strBufferString, 1, MAX_BUFFER_LENGTH)

If getLoggedUserName = "jculver" Then
DoCmd.OpenForm ("ARCManagement")
Else
DoCmd.OpenForm ("VOARCManagement")
End If

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top