Environ("USERNAME") (2 Viewers)

zezo2021

Member
Local time
Today, 14:52
Joined
Mar 25, 2021
Messages
381
Hello

How can I insert user name in a field in a table automatically

I try to use default value - calculated field

but not accept this function

Environ("USERNAME")
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:52
Joined
Feb 28, 2001
Messages
27,172
When you say "not accept" - can you be more specific?

a. Where are you using this?
b. Does it give you an error? If so, which one? Number AND text, please.
c. Does it compile and run but just returns no results?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:52
Joined
Oct 29, 2018
Messages
21,467
Hi. Try doing it on the form instead of the table.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:52
Joined
Feb 28, 2001
Messages
27,172
Good catch, TDBG, I didn't think of it that way.

@zezo2021 - using a computed field in a table is actually not a good idea. Default values and value constraints are usually limited to constants or simple expressions. It can even be difficult to make constraints based on other fields in the same table.

Computations belong in queries or forms or reports or VBA code. Tables should be passively defined.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:52
Joined
Feb 19, 2002
Messages
43,264
Additionally, since the table level calculation happens in ACE (the database engine) rather than in Access, VBA is NOT available so you can only use functions that are known to SQL.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:52
Joined
May 7, 2009
Messages
19,237
How can I insert user name in a field in a table automatically
If you are not going to use Form for your table, there are steps to follow:

1. create a Table for your username:

tblUser (table)
ID (autonumber)
user (short text)

2. add a single record to tblUser (any text).

3. create a public function that will update tblUser with the username

public function fnUpdateUser()
currentdb.execute "update tbluser set user = '" & environ$("username") & "'"
end function

4. create a macro and save it as autoexec (the name).

Runcode
Function name
fnUpdateUser()

5. bring your table in design view.
add Data Macro (Before Change)

Look Up A Record In tblUser
Where Condition [ID] = 1

----SetLocalVar
----Name tvarUser
----Expression [tblUser].[user]

SetField
Name
[theUsernameFieldHere]
Value =[tvarUser]


''''''''''''''''''''''''''''''
save your table and restart your app.
 

Isaac

Lifelong Learner
Local time
Today, 05:52
Joined
Mar 14, 2017
Messages
8,777
That's a native function, try creating your own public function that returns that, then use the public function
 

Users who are viewing this thread

Top Bottom