Record UserID in form

T

TinaR

I'm using Access 2007. I've read various posts on capturing the userid. I
have a form where the user is supposed to enter their name so we know who
created a particular record. (I'm interested in who created the record, not
if anyone modified it afterwards). Needless to say, they don't always
complete this field. I'd like to capture it automatically based on their
Outlook login.

I created a new module for the fOSUserName function I found on one of the
postings on this forum. I named the module basUserName.
Then I created a text field called UserID on my table and put the field on
my form.
In the Form's BeforeUpdate event I typed:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.UserID = fOSUserName()
End Sub

But when I create a new record and look at the UserID field on my form,
there's no data in it. If I understood the postings correctly, it should
have my login. What am I doing wrong?

Thanks!
Tina
 
J

John W. Vinson

But when I create a new record and look at the UserID field on my form,
there's no data in it. If I understood the postings correctly, it should
have my login. What am I doing wrong?

Assuming that the user's Outlook username has anything to do with their
Windows logon user name; it doesn't necessarily!

The code you use gets the Windows userID (when you boot up, and select a logon
name from the screen before Windows actually opens).
 
T

TinaR

Thanks for the quick response. I misspoke. I meant the user's Window UserID
when they boot up.

Still not sure where I've gone wrong. At the very least, shouldn't I see at
least my userid populate the UserID field?

Thanks,
Tina
 
K

Keith Wilby

TinaR said:
Thanks for the quick response. I misspoke. I meant the user's Window
UserID
when they boot up.

Still not sure where I've gone wrong. At the very least, shouldn't I see
at
least my userid populate the UserID field?

Yes you should. What do you get when you put "?fOSUserName()" in the
immediate window?

Keith.
www.keithwilby.co.uk
 
T

TinaR

Currently I don't see anything. My userid doesn't show up in the UserID
field on my form. Nor does it show up in the UserID field in the table.

I had to go looking for the immediate window because I didn't know what it
was. I get my Windows userid when I type in ?fOSUserName(). That's a good
sign, right? :)

Tina
 
J

John W. Vinson

Then I created a text field called UserID on my table and put the field on
my form.
In the Form's BeforeUpdate event I typed:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.UserID = fOSUserName()
End Sub

But when I create a new record and look at the UserID field on my form,
there's no data in it. If I understood the postings correctly, it should
have my login. What am I doing wrong?

The BeforeUpdate event fires AFTER you have entered everything on the form,
the instant before it's written to disk. So you won't see it on the form when
you're entering data; you should see it, though, if you bring this record back
onto the form from your table.

Use the form's BeforeInsert event to populate the field when you *start*
entering data.
 
K

Keith Wilby

TinaR said:
Currently I don't see anything. My userid doesn't show up in the UserID
field on my form. Nor does it show up in the UserID field in the table.

I had to go looking for the immediate window because I didn't know what it
was. I get my Windows userid when I type in ?fOSUserName(). That's a
good
sign, right? :)

Tina

It means that the function is working so there must be something else amiss.
Are you sure that the control's name is "UserID" (rather than that being the
field's name)? Does the code compile?

Keith.
www.keithwilby.co.uk
 
T

TinaR

Keith - Well, not sure why, but today it works. My userid shows up after
I've saved the record.

I tried modifying existing records (that I did not create) and I noticed
that my userid populates the UserID field. This tells me that the
fOSUserName() function pulls the userid of the person modifying the record.
What I really need is the userid of the person creating the record. So in
effect, once the record is created, the userid field shouldn't change once
the record has been modified.

John - I used the BeforeInsert event as you suggested and it worked! I then
went back to an existing record that I didn't create (where the UserID field
wasn't populated) and modified the record. Nothing showed up in the UserID
field. Which is exactly what I wanted to happen.

Kieth & John - So I think the fOSUserName() function together with the
BeforeUpdate event is meant is to capture the userid of the person modifying
the record. And the fOSUserName() function along with the BeforeInsert
event is meant to capture the userid of the person creating the record. Is
this correct? THANK YOU both for all your help! I always know I can count
on this knowledge board to help me out.

Tina
 
J

John W. Vinson

Kieth & John - So I think the fOSUserName() function together with the
BeforeUpdate event is meant is to capture the userid of the person modifying
the record. And the fOSUserName() function along with the BeforeInsert
event is meant to capture the userid of the person creating the record. Is
this correct? THANK YOU both for all your help! I always know I can count
on this knowledge board to help me out.

Your analysis is correct, and you're most welcome!
 

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