Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

"LOGON_USER" Server Variable in VBA

I developed a passive logon process in an ASP application on our Intranet.  I use "<%=(Request.ServerVariables("LOGON_USER"))%>" to request the identity of the user.  

The visitor is redirected to a logon failed screen if the userid isn't loaded in the database (every page will do this).

I'd like to set up a similiar process in an Access 2000 application.  Is there equivalent VBA code to this ASP code?

(_|_)
0
usafbuttcrack
Asked:
usafbuttcrack
  • 3
  • 3
  • 3
1 Solution
 
HumeCommented:
Basically you need the windows user logon name
The following code should suffice


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

Private Function CurrentUserName() As String
    Dim lSize As Long, count As Integer
    Dim sBuffer As String
    sBuffer = String(50, Chr(0))
    lSize = Len(sBuffer)
   
    Do
        Call GetUserName(sBuffer, lSize)
        CurrentUserName = sBuffer
        count = count + 1
    Loop Until Left(CurrentUserName, 1) <> Chr(0) Or count >= 3
End Function

Specialist Application Programmer
IS Developments
0
 
usafbuttcrackAuthor Commented:
Hume,

Wow! That was fast! I didn't expect such a quick response.

I won't be able to try this out until tomorrow.  My VBA skills are rudimemtary so I might have to ask for more help.

I'll let you know.

(_|_)
0
 
usafbuttcrackAuthor Commented:
Hume,

Wow! That was fast! I didn't expect such a quick response.

I won't be able to try this out until tomorrow.  My VBA skills are rudimemtary so I might have to ask for more help.

I'll let you know.

(_|_)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
shanesuebsahakarnCommented:
Alternatively:
environ("username")

will return the current Windows user name. eg:
MsgBox environ("username")
0
 
HumeCommented:
Think you'll find the environ("username") method only works correctly on NT based machines. Using the API will work on Win9x as well.
Depends on what platform your aiming at!

Specialist Application Programmer
IS Developments
0
 
shanesuebsahakarnCommented:
True, I've just gotten so used to using WinXP lately.
0
 
HumeCommented:
Sorry just had a thought
I use this procedure with a separate trimming procedure to remove the trailing null characters (chr(0))
The following amended code will trim any null chars from the username before returning it


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

Public Function CurrentUserName() As String
   Dim lSize As Long, count As Integer
   Dim sBuffer As String
   sBuffer = String(50, Chr(0))
   lSize = Len(sBuffer)
   Do
       Call GetUserName(sBuffer, lSize)
       count = count + 1
   Loop Until Left(sBuffer, 1) <> Chr(0) Or count >= 3
   CurrentUserName = Left(sBuffer, InStr(1, sBuffer, Chr(0)) - 1)
End Function

Specialist Application Programmer
IS Developments
0
 
usafbuttcrackAuthor Commented:
shanesuebsahakarn,

Thanks!  We are running Windows 2000 on all our machines with several 2000 and one NT servers.  Environ is a pretty cool function.  Thanks for letting me know about.  I'll be using it more and more for other things.  

Hume,

I bet your solution would work as well.  My VBA skills suck (gotta take a class or something).  Environ will allow me to select needed records using SQL.

(_|_)
0
 
shanesuebsahakarnCommented:
NP, glad I could help! BTW, Hume's function should also work in an SQL query too.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now