Free Lessons
Fast Tips
Topic Index
Home   Courses   Seminars   Templates   TechHelp   Fast Tips   Forums   Help   Contact   Join   Order   Logon  
Home > Forums > Access
Access Forum

Back to Access Forum

TempVars Upload Images   Link  
Tom Kelly 
2 months ago
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!
Alex Hedley
2 months ago
Tom Kelly
2 months ago
Thank you for the resources!  I'll be doing some digging into those :-)
Kevin Yip
2 months 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
Option Explicit

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

Function GetUserNameT()
    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)
End Function

' P.S. String(200,0) above creates a string of two hundred null characters, as required by the next line.
Tom Kelly
2 months 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
2 months 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.
Richard Rost
2 months ago
Just be careful because environment variables can be overwritten by someone who has mad hacker skillz. LOL

This thread is now closed. If you wish to comment, start a NEW discussion, below.

Back to Access Forum Comments

Start a NEW Conversation
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
Subscribe to Access Forum
Get notifications when this page is updated

Check out these other pages that may be of interest to you:

11/28/2022Top X
11/27/2022System Settings
11/25/2022Application Title
11/24/2022Full HTML Editor in Access
11/24/2022Change Database Title
11/23/2022Other Names
11/21/2022Check Icon File
11/21/2022Check Icon File
11/18/2022Archive Records
11/17/2022InStr Function

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.

Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
My Account
My Courses
Lost Password
Student Databases
Change Email
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Customer Support
Web Site Tour
Consulting Services
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Video Tutorials
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Terms of Sale
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
Blog RSS Feed    Twitter

YouTube Channel    LinkedIn