How to determine the Windows user name from within Access?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do you determine the Windows user name from within Access? Please note,
not the secure workgroup name used to login to Access.

Thank you for your help.
Mark
 
I have setup the module with the below code and I have tried several
different methods to add it to my forms with no luck. I also used the:

=fOSUserName()
this should trigger login to be put in the text field
me.UserNameTextBoxName=fosusername()
this one should save the input in the field
right? am I close. This is not working for me what am I missing

I have the module saved as fOSUserName like it says.

What and where do I put the information to make it work. I use macros a lot
but not modules. I need detailed steps.

Thank You
 
thcode said:
I have setup the module with the below code and I have tried several
different methods to add it to my forms with no luck. I also used the:

=fOSUserName()
this should trigger login to be put in the text field
me.UserNameTextBoxName=fosusername()
this one should save the input in the field
right? am I close. This is not working for me what am I missing

I have the module saved as fOSUserName like it says.


That is a misunderstanding. The *function* is named "fOSUserName", but the
module must not be given the same name. It doesn't really matter what the
module is called, except that it shouldn't be the same as any public
procedure or variable. Call the module "modOSUserName", if you like.
 
Ok I have changed the name of the module to modOSUserName

I need an audit trail of who was logged into the machine when the record was
input so I need this added to the table for reports.

So I put the =modOSUserName() in what?
 
thcode said:
Ok I have changed the name of the module to modOSUserName

I need an audit trail of who was logged into the machine when the record
was
input so I need this added to the table for reports.

So I put the =modOSUserName() in what?


Not "=modOSUserName", "=fOSUserName()" -- assuming that's the name of the
function *in* the module. The module is just the container for the
function. It's the function you actually call by name.

Have you crewated a text field in your table to hold the user name?
Assuming you have, then add a text box to the entry form, bound to that
field, and set the Default Value property of the text box to

=fOSUserName()

If you don't want the user to be able to see the text box, you can set the
text box's Visible property to No, and make the text box as small as you
want.

The Default Value property set as above will cause the user name to
aautomatically be inserted into any new record that is added with that form.
It won't track who updated the record later, though, unless you also add
code to the form's BeforreUpdate event, along these lines:

Private Sub Form_BeforeUpdate(Cancel As Integer)

Me!txtUserName = fOSUserName()

End Sub

Note: if you have code like that, you don't need to set the Default Value
property.

--

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Ok now I understand. I got this to run and it gave a compile error.
Sub or Function not defined
it highlighted this area in the module
apiGetUserName

I get #Error in my text field in the form. Help.
 
thcode said:
Ok now I understand. I got this to run and it gave a compile error.
Sub or Function not defined
it highlighted this area in the module
apiGetUserName


Did you paste the declaration of the API function at the top of the module,
as instructed on the web page? This code:

'------ start of code ------

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

'------ end of code ------

.... has to be pasted into the Declarations section of the module into which
you pasted the fOSUserName function, after the Option Compare Database and
Option Explicit statements.
 
It says:
Option Compare Database

****code start
exactly how it appears on the website except it has the line right after
the "GetUserNameA" part that is added automatically
****code end

Sorry I am rusty at this. I probably missed something obvious.
Tina
 
Ok,

It worked. I created a new database without all my failed attempts and it
worked. I also had it pull the machine name and that worked as well.

Very Cool.
Thanks,
Tina
 
Back
Top