1

I found some neat code (via here actually) for getting the username of the user currently logged in to Windows.

Say I have a table called AdminEntry. I'd be looking to get the AdminEntry table set up as the recordsource of a form, so the user would do the data entry on that table via the form. Any record they create and complete data entry for would then also have their username stamped on that record so we can track what belongs to who.

Not sure how I would go about pushing that username string in to the table, or the best approach.

Perhaps if the AdminEntry table had the following fields:

| UserName | Field1 | Field2 |
|          |        |        |

The user could just use the form to do data entry on Field1 and Field2:

| UserName | Field1 | Field2 |
|          |  data  |  data  |

And then an event on a Save button could perhaps pull through the username string in to the UserName field and prior to saving the full record in the AdminEntry table:

| UserName | Field1 | Field2 |
|   data   |  data  |  data  |

Haven't got a clue how to pull through that username string though, so would love some help/pointers!

Community
  • 1
  • 1
Matt Hall
  • 2,412
  • 7
  • 38
  • 62

1 Answers1

2

Add a text box, txtUserName, to the form and bind it to the UserName field. You can set the text box's .Visible property to No.

Then assign the value from that fOSUserName() function to txtUserName from the form's before update event. Note before update also occurs in conjunction with form insert, so this approach will store the current user name both when you add a new record and when you modify an existing record.

Private Sub Form_BeforeUpdate(Cancel As Integer)
    ' Me.txtUserName = CreateObject("WScript.Network").UserName
    Me.txtUserName = fOSUserName
End Sub
HansUp
  • 95,961
  • 11
  • 77
  • 135
  • Thanks @HansUp, works great! To anyone else reading: don't do what I did initially and bind the `txtUserName` textbox's control source to `=[UserName]`; make sure it's just `[UserName]` otherwise it won't be able to accept entries from anywhere else (and an error will pop-up). Quick question: the code you commented-out, is that just a simpler/alternative way of getting the username without the need of going through the `fOSUserName` method? – Matt Hall Apr 19 '13 at 13:57
  • 2
    Yes, that `CreateObject("WScript.Network").UserName` was present in my original code. I substituted `fOSUserName` because I thought you wanted that instead. But I left `"WScript.Network"` in there in case other readers are interested. Either way works fine. – HansUp Apr 19 '13 at 14:00
  • 2
    @MattHall FWIW, I would recommend the `WScript.Network` approach because the older code cited in your question uses 32-bit Windows API calls and it almost certainly won't compile in 64-bit Access as written. There might be some way to fiddle with it to get it to run, but it would be easier just to use an approach that avoids direct references to 32-bit Windows API functions. – Gord Thompson Apr 19 '13 at 18:07