Environ("USERNAME")

zezo2021

Member
Local time
Today, 09:44
Joined
Mar 25, 2021
Messages
412
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")
 
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?
 
Hi. Try doing it on the form instead of the table.
 
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.
 
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.
 
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.
 
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

Back
Top Bottom