How to Get the User Name of the Person Currently Using the Database via VBA

Need to know who in your business added a particular record? Or who updated it last? It’s a pretty common requirement, but unfortunately not one that Microsoft Access has built in functionality for. Simply adding a “last updated by” field to your table can (in many cases) be enough to meet most needs, but just how do you know who updated the record if Access can’t tell you? A little VBA code is all you need, as this article will show you.

No need to create your own “login” form to capture user name

Ms Access DatabaseUnless your database has its own login form or mechanism, then the only way you’ll be able to know who is currently using/logged in to the database is to use one of the Windows Operating System files – specifically the “mpr.dll” file. If you’ve not used external dll’s in your code before, it can seem a little daunting, but as you’ll see in the code below, it’s actually pretty straight forward.

So – let’s define what we want our code to do…

“Let me have the name of the currently logged in user”

Firstly, the code:

Declare Function GetUser Lib "mpr.dll" Alias "WNetGetUserA" (ByVal Name As String, ByVal UserName As String, Length As Long) As Long

Public Function GetUserName() As String
    Dim returnStatus As Integer
    Dim Name, UserName As String
    'Allocate space for user name to be stored in
    UserName = Space(255)

    ' Call the aliased function in the mpr.dll file.
    returnStatus = GetUser(Name, UserName, 255)
    
    If returnStatus = 0 Then
        'Strip the username from the returned string
        UserName = Left(UserName, InStr(UserName, Chr(0)) - 1)
    Else
        ' An error occurred.
        GetUserName = "Error retrieving the user name."
        End
    End If

    GetUserName = UserName

End Function

The code, explained

The Currently Logged In User

The first thing you’ll see is that we’re making a reference to an external function, contained within the mpr.dll file. We do this by declaring a local name (GetUser) for the public function contained in the dll (in this instance, WNetGetUserA). – we also define what parameters are necessary for the function call – in this case the “Name”, “UserName”, and “Length” parameters. The only one we’re really interested in here is “UserName” but the remainder have to be supplied too as that is how the function is set up within the dll.

Now that has been done, we create our own function, from which we’ll be making a call to this external function. The code for our function is pretty minimal but let’s run through the key elements:

  1.  “UserName = Space(255)” – if you step through the code in the debugger, what you will see is that when the “GetUser()” function is called, the “UserName” variable that we pass to it actually gets overwritten. In order for that to happen, the variable needs enough memory allocated for the data to be stored in, so we just allocate 255 space chars to the variable as that should be more than enough for most (if not all) user names to be contained in.
  2. We capture the return value from the function call in the “returnStatus” variable. A zero (0) here means the call was successful, anything else means an error has occurred.
  3.  Finally, the “UserName” variable needs tidying up as it will be essentially padded with null chars so we extract the chars that are in the variable before a Chr(0) is found.

See – now that wasn’t that bad was it! And now, at least if you have to deal with mdb database corruption, you’ll know who was using the database last so who you can blame! (I’m joking here of course!)

Author Introduction:

Mitchell Pond is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including repair SQL Server problem and excel recovery software products. For more information visit www.datanumen.com

4 responses to “How to Get the User Name of the Person Currently Using the Database via VBA”

  1. “. You don’t require to research many phrases to maintain the discussion going, however you can stun your Brazilian woman by recognizing some essentials. International dating can be challenging, specifically if you intend to begin a partnership with a woman whose culture you understand nothing around. As a matter of fact, this post will certainly be enough for you to start dating Brazilian females! Several ladies have two jobs simply to make sufficient cash for food and also living. Never ever send your personal details or cash to individuals on mail order brides websites. For foreign men who are fantasizing about a beautiful Brazilian sweetheart, international women dating web sites are a perfect choice. So, ParPerfeito is an excellent way to meet Brazilian love. He wed his Brazilian love in 2002. He claimed that his lady is caring, clever, and beautiful. You ought to likewise think about the price of presents for a woman you such as. You can make use of messaging alternatives to obtain closer to Brazilian girls and also select the one you like the most. This helps to develop count on, as well as you are familiar with each other better. A great deal of single regional ladies obtain into the global dating scene as well as look for a best partner around the globe.

    He additionally compared her to American women and also stated she is just ideal for him. Brazilian bride-to-bes frequently come to be brides to American men as well as build a pleased household life. They are confident concerning what they are doing and also what they desire in life (generally fun!). It is really discourteous to discourage her from doing these activities. Others favor more intimate and also lower-key yet nostalgic displays of love. You should ask her even more questions regarding her life. You’re going to satisfy this individual in the real world someday, as well as it would certainly be a pity if all your lies emerge. Can’t choose what is the best method to locate and also meet Brazilian ladies for marriage? Weding a Brazilian female means discovering the very best buddy. We prepared for you the checklist of the finest sites with their monthly gos to where you can discover your future female from Brazil. As well as you will require to select from them, which can be extremely challenging. Which Brazilian dating solution will be best to discover love? Such an individual will certainly be bordered with love and also passion. Stunning Brazilian females dating brings convenience: When you visit a mail order bride-to-be site, you will certainly note that registration as well as filtering can be provided for totally free.

    In situation females wish to use them, it is definitely cost-free. However if you choose to visit Brazil and satisfy girls personally, it can be a decision you will certainly regret in situation of not successful dating. Despite whether you pick to fulfill Brazilian ladies online or go on a love excursion to Brazil, you can discover something helpful in the listed here. Looking for the very best dating solution to fulfill gorgeous Brazilian brides? When you understand about the guide ideas of your trip in love, it is time to be familiar with regarding the ideal online dating web sites. You can figure out the finest pointers for producing connections with Brazilian ladies and locate the testimonials of one of the most famous mail order bride-to-be sites. We will certainly show you several fascinating realities concerning dating Brazilian females, stats on their connections with immigrants, and also some functional ideas that can aid you to conquer bride-to-bes from Brazil. Maintain reviewing to discover numerous helpful pointers on how to overcome any Brazilian woman, and also do not accidentally ruin every little thing with a wrong praise or another thing. Keep in mind that witnesses need to be residents of Brazil.

    Keep reading as well as enjoy enchanting stories! We know they are glamorous, super manly, and exceedingly charming. Brazilian girls are family-oriented and also constantly care for their better halves. Weding a woman from Brazil, you can anticipate that she will certainly take treatment of the coziness and also consistency at your residence. Prior to enrolling in a dating system, have a look at the testimonials from other customers. Price of attributes on Pay-per-Use websites – Users pay just for features (message chat, video clip telephone calls, etc) they utilize. Wedding event – Consists of the price of a wedding, wedding apparel, rings, marriage license, and wedding celebration function. The expense of on-line dating solutions varies from site to website as well as likewise depends upon the length of time you will certainly utilize a bride-to-bes site. Extra services – Gift shipment, equating solutions, a search boost, “view all images” function, and other services are normally paid individually. However, there is an ordinary price for a month of usage of dating solutions. Nevertheless, plastic surgical procedures are popular in Brazil, yet women do them just to improve their natural appeal, not to suit Western beauty standards

  2. It is much easier to use this command to retrieve the same information:

    Environ(“USERNAME”)

    BTW, this gives you the NT login name, which could be an employee id depending on your company policy. It is more informative if you can retrieve the display name from Active Directory. I can show the code if there is interest in it.

Leave a Reply

Your email address will not be published. Required fields are marked *