Current User

G

Guest

I have the following code on one of my forms to insert the value of current
user into my form (and record in table). However, I can get a msgbox up with
"the current user is x" so i know the current user bit is right however
nothing will happen when I try to use the form. No value is inserted into
the field and i can just type what i want in there. I don't get any errors.

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

What's wrong with it please?

TIA
 
K

Keith W

albycindy said:
I have the following code on one of my forms to insert the value of current
user into my form (and record in table). However, I can get a msgbox up
with
"the current user is x" so i know the current user bit is right however
nothing will happen when I try to use the form. No value is inserted into
the field and i can just type what i want in there. I don't get any
errors.

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

Is Me.CurrentUser a text box? Change it's name to txtCurrentUser and change
your code to Me.txtCurrentUser = CurrentUser.

HTH - Keith.
www.keithwilby.com
 
A

Allen Browne

You have an ambiguity. The 2nd CurrentUser could refer to the text box on
your form, or to the CurrentUser() function from the Access library.

Try being specific about what you are referring to:
Me.CurrentUser = Access.CurrentUser()

In general, it's not a great idea to use one of the built-in names as a
field name.
 
G

Graham Mandeno

The problem is that you have an object in the local scope (your form) which
is named CurrentUser. Because it is closer in scope than the Access library
where the CurrentUser function lives, it gets found first.

So,
Me.CurrentUser = CurrentUser
is just the same as saying
Me.CurrentUser = Me.CurrentUser

In other words, it does nothing!

This illustrates the problem of giving a field or a control the same name as
a built-in constant or function name. It most often arises with:
Me.Date = Date

The best solution is to rename that field in your table - say
"LastUpdatedBy". Then you have:
Me.LastUpdatedBy = CurrentUser

If you really can't change the name for some reason (unlikely!) then a
really nasty workaround is:
Me.CurrentUser = Access.CurrentUser
 
G

Guest

I was wondering if that was the problem...but thought that Access could
distinguish.

So I changed the table, so that the field name is txtCurrentUser. I have
changed the form fields control source and name to txtCurrentUser. I have
changed the coding to

Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.txtCurrentUser = CurrentUser
End Sub

Then entered new record and still.....nothing! No errors - no action.
 
G

Guest

Scrap what I wrote before. It is working in a fashion. The current user
value is in the table, but won't display in the form. However!! It will
display in another form based on the table (obviously)....why?

Graham Mandeno said:
The problem is that you have an object in the local scope (your form) which
is named CurrentUser. Because it is closer in scope than the Access library
where the CurrentUser function lives, it gets found first.

So,
Me.CurrentUser = CurrentUser
is just the same as saying
Me.CurrentUser = Me.CurrentUser

In other words, it does nothing!

This illustrates the problem of giving a field or a control the same name as
a built-in constant or function name. It most often arises with:
Me.Date = Date

The best solution is to rename that field in your table - say
"LastUpdatedBy". Then you have:
Me.LastUpdatedBy = CurrentUser

If you really can't change the name for some reason (unlikely!) then a
really nasty workaround is:
Me.CurrentUser = Access.CurrentUser

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

albycindy said:
I have the following code on one of my forms to insert the value of current
user into my form (and record in table). However, I can get a msgbox up
with
"the current user is x" so i know the current user bit is right however
nothing will happen when I try to use the form. No value is inserted into
the field and i can just type what i want in there. I don't get any
errors.

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

What's wrong with it please?

TIA
 
A

Allen Browne

I'm guessing your text box is named something other than CurrentUser. You
are therefore assigning a value directly to the CurrentUser field in your
table. As a result, it doesn't show up in the text box until the record is
saved/reloaded.
 
K

Keith W

albycindy said:
Scrap what I wrote before. It is working in a fashion. The current user
value is in the table, but won't display in the form. However!! It will
display in another form based on the table (obviously)....why?
I think it must be the nomenclature. Generally you would have a field in a
table "MyFieldName" and your bound text box on your form would be named
"txtMyFieldName". It might be worth naming your field something other than
"txtCurrentUser" since this is currently also the name of your text box, but
not "CurrentUser". How about "UserName"? Then your text box would be
called "txtUserName".

HTH - Keith.
www.keithwilby.com
 
G

Guest

Ok, I understand now. Thanks everyone. This will have to do. It won't show
in my call log form but will show on my follow up form.

Thanks.
 

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