Windows user name

J

justinc

I was wondering if there is a way to automatically get the windows user name
to pop in a field. i have a field called reps and i need it to pull in the
currently logged on windows user name into that field. im using Access 2003.
Any help would be great
 
C

Chris O'C via AccessMonster.com

The code here will get the user's windows name:

http://www.mvps.org/access/api/api0008.htm

In the unbound text box's control source property you can call a public
function in a standard module like this:

=functionname()

If you need to save this info in a table, you can't do the above, because the
text box's control source is bound to the field in the table, Reps. In the
form's open event, set the value of the bound text box to the return value of
the function.

Private Sub Form_Open(Cancel As Integer)
txtReps = functionname()
End Sub

Chris
Microsoft MVP
 
J

justinc

thanks for the help, but im kinda new to this so can you explain how to do
this. thanks
 
C

Chris O'C via AccessMonster.com

No prob. Here are the steps:

1 - Create a new standard code module and paste the code on this page
http://www.mvps.org/access/api/api0008.htm into it. (Menu: Insert > Module)
Name the module basApiFunctions. (Whatever you do, don't name the module the
same as any of the procedures in your app.) Compile the code.

2 - Create a new query based on the table. (Menu: Insert > Query)

3 - Create a new form based on the query. (Menu: Insert > Form) You base
the form on the query instead of the underlying table so you have flexibility
to add other tables, sort and filter the data in the query.

4 - Access will name the text box bound to the reps field the same name.
While the form is in design view, change that text box's name to txtReps in
the form's properties.

5 - Go to the events tab in the form's properties. Scroll down till you see
OnOpen. Click on the combo box next to it and choose [Event Procedure].
Click on the three dots to the right of the combo box to open the code module
in the open event. Add the code I suggested in my earlier post and error
handling. Your Form_Open procedure should look a lot like this:

'start of code:

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Proc_Err

Me.txtReps = fOSUserName()

Proc_Exit:

Exit Sub

Proc_Err:
MsgBox Err.Number & vbcrlf & Err.Description
Err.Clear
Resume Proc_Exit

End Sub

'end of code

Compile the code and save the form. When the form opens, your windows user
name will show in the text box and be saved in the table.

Chris
Microsoft MVP
 
J

justinc

i did that now its giving me an error. plus i forgot to mention i have
multiple people using this app so can i still use it to pull in and save
everyone's user name? i need the user names for a report that my boss wants
run.
thanks for the help im very new to this

Chris O'C via AccessMonster.com said:
No prob. Here are the steps:

1 - Create a new standard code module and paste the code on this page
http://www.mvps.org/access/api/api0008.htm into it. (Menu: Insert > Module)
Name the module basApiFunctions. (Whatever you do, don't name the module the
same as any of the procedures in your app.) Compile the code.

2 - Create a new query based on the table. (Menu: Insert > Query)

3 - Create a new form based on the query. (Menu: Insert > Form) You base
the form on the query instead of the underlying table so you have flexibility
to add other tables, sort and filter the data in the query.

4 - Access will name the text box bound to the reps field the same name.
While the form is in design view, change that text box's name to txtReps in
the form's properties.

5 - Go to the events tab in the form's properties. Scroll down till you see
OnOpen. Click on the combo box next to it and choose [Event Procedure].
Click on the three dots to the right of the combo box to open the code module
in the open event. Add the code I suggested in my earlier post and error
handling. Your Form_Open procedure should look a lot like this:

'start of code:

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Proc_Err

Me.txtReps = fOSUserName()

Proc_Exit:

Exit Sub

Proc_Err:
MsgBox Err.Number & vbcrlf & Err.Description
Err.Clear
Resume Proc_Exit

End Sub

'end of code

Compile the code and save the form. When the form opens, your windows user
name will show in the text box and be saved in the table.

Chris
Microsoft MVP

thanks for the help, but im kinda new to this so can you explain how to do
this. thanks
 
C

Chris O'C via AccessMonster.com

What's the error message?

The steps I gave you will save the current user's windows name in the txtReps
text box (which is bound to the reps field in the table) every time the form
is opened. It will overwrite any previous name if the first record displayed
isn't a new record. Is that what you want? You'll have to modify the steps
to suit your needs if that's not what you want it to do. I don't have your
app in front of me, so I can't see what you have to tell you what you need to
do about it.

If you're new and don't have any training, ask your boss when that training
is coming. You won't get very far pointing and clicking and guessing how to
do things in Access. Read Access help while you're waiting for the training
and read some tutorial books, too.

Chris
Microsoft MVP

i did that now its giving me an error. plus i forgot to mention i have
multiple people using this app so can i still use it to pull in and save
everyone's user name? i need the user names for a report that my boss wants
run.
thanks for the help im very new to this
No prob. Here are the steps:
[quoted text clipped - 47 lines]
 
C

Chris O'C via AccessMonster.com

After rereading my earlier reply it sounds like I'm not willing to help, but
that's not true. You can always post questions in these groups and we'll try
to help you. But getting answers from these groups is no substitute for good
training.

You'll need to describe your form in a little more detail and how your users
use it so I can give you better instructions so the user name stored in the
table doesn't get overwritten at the wrong times. I gave you a very simple
example of the steps, but I don't think that's quite what you need. I think
you need some more help getting where you need to go.

Chris
Microsoft MVP
 
J

justinc

the error is:
-2147352567
You can't assign a value to this object

im using the the from to track customer calls by the Rep Name, so when a new
record is created the user doesnt have to type there name in every time it
just pulls in there currently logged on name in to the database so when we
run the report it will show the user name with all of info that the user did
for that call.
thanks again for all your help! it is very much appreciated!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top