Greetings Experts and Veterans ... newbie here that knows next to nothing except what I learned from Computer Learning Zone and Richard Rost! Well a few other YouTube videos as well 😉 😉 A little background ... I have a pretty decent and simple data base. My relationships are connected well ... I have primary keys and associated foreign keys in my tables and the data is getting into the fields the way it's supposed to through some forms I designed. I created a Login Form that opens to my Main Menu. My Main Menu has a couple of command buttons to open up different forms. One button opens up "Individuals List" and a button to open up my Individual Person Form which has the different fields and all is great! On that Individual Person Form I have a Command Button to "set an appointment" for that individual person. When I click on "Set Appointment" it opens my AppointmentForm which has the following fields: UserName (the Staff person that currently logged into the database), Individual (first name, last name), StartDateTime (start of appointment), EndDateTime (end of appointment - auto set for an hour after the start of the appointment), Title, and Notes! My question: How can I get UserName field to be automatically populated with the User's "username". Ex: I log into the computer and navigate to set an appointment for Bill Smith. When I get to my AppointmentF I am able to automatically populate the "Bill Smith" from the Individual Person form BUT how can I set "TomK" into the User field. Presently it's a Combo Box and I can drop down to find the User but it would be nice if I could have my name (or the name of whoever is signed in automatically pop into that field)!! Thank you in advance!
Thank you for the resources! I'll be doing some digging into those :-)
Kevin Yip 12 days ago
If it is the Windows login user name you want, you need to have VBA call a function from a Windows DLL. It's an advanced topic and I don't know if any of Richard's videos cover that. You may want to try the following code. This is what I use when I need my Windows login name in Access. The function GetUserNameT() below will return your Windows user name:
Option Compare Database
Declare Function GetUserName& Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long)
Dim s As String, cnt As Long, dl As Long
cnt = 199
s = String(200, 0)
dl = GetUserName(s, cnt)
GetUserNameT = Left(s, cnt - 1)
' P.S. String(200,0) above creates a string of two hundred null characters, as required by the next line.
Tom Kelly 11 days ago
Thank you Kevin!! I will try that. Doesn't "Environ("UserName") do the same thing when it comes to finding the UserName of the person signed on or logged into the computer? Also if that is the case how can I use a Msgbox command to see if the code does in deed catch the right "UserName".
In your code would ... MsgBox GetUserNameT ... before End Function show me who's signed in?
Kevin Yip 11 days ago
You're right, Environ("username") does the same thing. In fact, this and GetUserNameT are one and the same; they look through the environment variables for the user name. GetUserNameT is the old version, and Environ() is built into later versions of Access.
You can assign the return value of Environ("username") or GetUserNameT() to a string variable, say, username. Then you can display it or check its value as you see fit.