Insert username into text box

P

Paul

I am using the fOSUserName functionality in my DB. I would like to use it to
dynamically preface comments a user types into a text box.

Scenario...user opens a ticket form, starts typing a comment in the text
box, and Access automatically inserts their username in front of their
comments (in bold typeface if possible) followed by a couple spaces, or a
space dash space.

Any ideas? Thanks!
 
D

Dale Fye

Paul,

I would recommend against this, since it violates the 1st Normal form (more
than one piece of information in a field). Instead, I recommend you add a
UserID field to the table, and update it before a record is written.

But it sounds like you want to use this textbox as a running summation of
comments made by multiple people about a particular ticket. If that is so,
then what you really need is a separate table that contains the fields for a
foreign key (TicketID), UserID, Comment, and dtEntered (date/time field).

Then, you can create a subform that displays all of these fields. You can
set the txt_UserID controls default value to "=fOSUserName()" so that it
automatically gets entered when you create a new record.

HTH
Dale

rather than using a single textbox, you create a
 
M

meyerryang

These are both good suggestions, but might I add mine. If you don't have any
user setting setup or login, etc.

On the After Update of any field or record:

me.[textbox, field, note, whatever you decide to update] = environ("USERNAME")

....this will update the field with the users environ settings, basically the
same way that it would update a comment in a note in excel.
 
D

Douglas J. Steele

I always cringe when I see someone recommending using the Environ function
to get user name. It's trivial to reset the value of the environment
variable for the duration of the Access session.

Paul's mentioned fOSUserName, which is presumably the code from
http://www.mvps.org/access/api/api0008.htm at "The Access Web" that
demonstrates the GetUserName API that's far, far more reliable than Environ.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


meyerryang said:
These are both good suggestions, but might I add mine. If you don't have
any
user setting setup or login, etc.

On the After Update of any field or record:

me.[textbox, field, note, whatever you decide to update] =
environ("USERNAME")

...this will update the field with the users environ settings, basically
the
same way that it would update a comment in a note in excel.

Paul said:
I am using the fOSUserName functionality in my DB. I would like to use it
to
dynamically preface comments a user types into a text box.

Scenario...user opens a ticket form, starts typing a comment in the text
box, and Access automatically inserts their username in front of their
comments (in bold typeface if possible) followed by a couple spaces, or a
space dash space.

Any ideas? Thanks!
 
P

Paul

Since I'm just looking to add a simple identifier of the person who added the
specific update, I decided to use the option Al provided in the After Update,
but I substituted 'CurrentUser' with fOSUserName():

Me.Comment = fOSUserName() & " - " & [Comment]

This works great for what I'm wanting to do.

Two more items...

1. I would really like to be able to bold the username and not the rest of
the text in the text box but I don't believe this can be done with Access '03
in it's maiden form.

2. I wouldn't mind being able to tie the fOSUserName value back to my
'Contacts' table which has a field for the network logon id...and then return
back the 'Name' field from that table (Contacts) and show it instead of their
network logon id, aka the fOSUserName.

Suggestions?
 
D

Douglas J. Steele

Paul said:
Since I'm just looking to add a simple identifier of the person who added
the
specific update, I decided to use the option Al provided in the After
Update,
but I substituted 'CurrentUser' with fOSUserName():

Me.Comment = fOSUserName() & " - " & [Comment]

This works great for what I'm wanting to do.

Two more items...

1. I would really like to be able to bold the username and not the rest of
the text in the text box but I don't believe this can be done with Access
'03
in it's maiden form.

You're correct that there's no way to bold only part of the text in
"vanilla" Access 2003: you'd have to use an RTF control, and I think you'd
find that far more effort than it's worth.
2. I wouldn't mind being able to tie the fOSUserName value back to my
'Contacts' table which has a field for the network logon id...and then
return
back the 'Name' field from that table (Contacts) and show it instead of
their
network logon id, aka the fOSUserName.

Me.Comment = DLookup("[Name]", "[Contacts]", "LogonId ='" & fOSUserName() &
"'") & " - " & [Comment]

(or whatever the field name is rather than LogonId). Note that's ' " before
the ampersand, and " ' " after it.

Note that Name isn't actually a good choice for a field name: it's a
reserved word, and you shouldn't use reserved words for your own purposes.
For a good discussion of what names to avoid, see what Allen Browne has at
http://www.allenbrowne.com/AppIssueBadWord.html
 
P

Paul

Thanks, that worked very well...one last question...how would your line of
code differ if I wanted to include 2 fields so I had the first and last name?

I tried playing around with it, but was unable to make it work. I take that
back, I could get it to pull both back, but I couldn't get it to put a space
between the First and Last name so I got 'FirstLast -' instead of 'First Last
- '.

Douglas J. Steele said:
Paul said:
Since I'm just looking to add a simple identifier of the person who added
the
specific update, I decided to use the option Al provided in the After
Update,
but I substituted 'CurrentUser' with fOSUserName():

Me.Comment = fOSUserName() & " - " & [Comment]

This works great for what I'm wanting to do.

Two more items...

1. I would really like to be able to bold the username and not the rest of
the text in the text box but I don't believe this can be done with Access
'03
in it's maiden form.

You're correct that there's no way to bold only part of the text in
"vanilla" Access 2003: you'd have to use an RTF control, and I think you'd
find that far more effort than it's worth.
2. I wouldn't mind being able to tie the fOSUserName value back to my
'Contacts' table which has a field for the network logon id...and then
return
back the 'Name' field from that table (Contacts) and show it instead of
their
network logon id, aka the fOSUserName.

Me.Comment = DLookup("[Name]", "[Contacts]", "LogonId ='" & fOSUserName() &
"'") & " - " & [Comment]

(or whatever the field name is rather than LogonId). Note that's ' " before
the ampersand, and " ' " after it.

Note that Name isn't actually a good choice for a field name: it's a
reserved word, and you shouldn't use reserved words for your own purposes.
For a good discussion of what names to avoid, see what Allen Browne has at
http://www.allenbrowne.com/AppIssueBadWord.html
 
D

Douglas J. Steele

You can actually cheat, and have DLookup return two fields at once:

Me.Comment = DLookup("[FirstName] & ' ' & [LastName]", "[Contacts]",
"LogonId ='" & fOSUserName() & "'") & " - " & [Comment]

Note that's a pair of single quotes, separated by a space, between the
names.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Paul said:
Thanks, that worked very well...one last question...how would your line of
code differ if I wanted to include 2 fields so I had the first and last
name?

I tried playing around with it, but was unable to make it work. I take
that
back, I could get it to pull both back, but I couldn't get it to put a
space
between the First and Last name so I got 'FirstLast -' instead of 'First
Last
- '.

Douglas J. Steele said:
Paul said:
Since I'm just looking to add a simple identifier of the person who
added
the
specific update, I decided to use the option Al provided in the After
Update,
but I substituted 'CurrentUser' with fOSUserName():

Me.Comment = fOSUserName() & " - " & [Comment]

This works great for what I'm wanting to do.

Two more items...

1. I would really like to be able to bold the username and not the rest
of
the text in the text box but I don't believe this can be done with
Access
'03
in it's maiden form.

You're correct that there's no way to bold only part of the text in
"vanilla" Access 2003: you'd have to use an RTF control, and I think
you'd
find that far more effort than it's worth.
2. I wouldn't mind being able to tie the fOSUserName value back to my
'Contacts' table which has a field for the network logon id...and then
return
back the 'Name' field from that table (Contacts) and show it instead of
their
network logon id, aka the fOSUserName.

Me.Comment = DLookup("[Name]", "[Contacts]", "LogonId ='" & fOSUserName()
&
"'") & " - " & [Comment]

(or whatever the field name is rather than LogonId). Note that's ' "
before
the ampersand, and " ' " after it.

Note that Name isn't actually a good choice for a field name: it's a
reserved word, and you shouldn't use reserved words for your own
purposes.
For a good discussion of what names to avoid, see what Allen Browne has
at
http://www.allenbrowne.com/AppIssueBadWord.html
 
P

Paul

Works like a charm, thanks Doug! I was making the mistake of using double
quotes instead of single quotes!

Douglas J. Steele said:
You can actually cheat, and have DLookup return two fields at once:

Me.Comment = DLookup("[FirstName] & ' ' & [LastName]", "[Contacts]",
"LogonId ='" & fOSUserName() & "'") & " - " & [Comment]

Note that's a pair of single quotes, separated by a space, between the
names.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Paul said:
Thanks, that worked very well...one last question...how would your line of
code differ if I wanted to include 2 fields so I had the first and last
name?

I tried playing around with it, but was unable to make it work. I take
that
back, I could get it to pull both back, but I couldn't get it to put a
space
between the First and Last name so I got 'FirstLast -' instead of 'First
Last
- '.

Douglas J. Steele said:
Since I'm just looking to add a simple identifier of the person who
added
the
specific update, I decided to use the option Al provided in the After
Update,
but I substituted 'CurrentUser' with fOSUserName():

Me.Comment = fOSUserName() & " - " & [Comment]

This works great for what I'm wanting to do.

Two more items...

1. I would really like to be able to bold the username and not the rest
of
the text in the text box but I don't believe this can be done with
Access
'03
in it's maiden form.

You're correct that there's no way to bold only part of the text in
"vanilla" Access 2003: you'd have to use an RTF control, and I think
you'd
find that far more effort than it's worth.

2. I wouldn't mind being able to tie the fOSUserName value back to my
'Contacts' table which has a field for the network logon id...and then
return
back the 'Name' field from that table (Contacts) and show it instead of
their
network logon id, aka the fOSUserName.

Me.Comment = DLookup("[Name]", "[Contacts]", "LogonId ='" & fOSUserName()
&
"'") & " - " & [Comment]

(or whatever the field name is rather than LogonId). Note that's ' "
before
the ampersand, and " ' " after it.

Note that Name isn't actually a good choice for a field name: it's a
reserved word, and you shouldn't use reserved words for your own
purposes.
For a good discussion of what names to avoid, see what Allen Browne has
at
http://www.allenbrowne.com/AppIssueBadWord.html
 

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