Get User Name Insert into (1 Viewer)

ECEK

Registered User.
Local time
Today, 05:09
Joined
Dec 19, 2012
Messages
717
Im trying to insert a user name into a table on opening a Welcome Form.
The error is within the DoCmd.runsql. and on opening the form it is asking for GUN?


Could anybody please advise?

Code:
Me.GetUserNametxt = Environ("username")

Dim GUN As String
GUN = Me.GetUserNametxt

DoCmd.RunSQL "INSERT INTO tblUserName (UserName) VALUES (GUN);"

Many thanks
 

isladogs

MVP / VIP
Local time
Today, 05:09
Joined
Jan 14, 2017
Messages
18,216
You forgot text delimiters.
Variable GUN isn't really needed
I prefer CurrentDB.Execute so warning messages only appear if an error occurs

Code:
Me.GetUserNametxt = Environ("username")

CurrentDB.Execute "INSERT INTO tblUserName (UserName) VALUES ('" & Me.GetUserNametxt & "');"
 

CJ_London

Super Moderator
Staff member
Local time
Today, 05:09
Joined
Feb 19, 2013
Messages
16,607
username is a string, so you need to include the quotes which you have not done, also your sql string is not pulling through the GUN variable, just the word 'GUN'

Always better to assign your built up sql to a string and debug.print it so you can copy to a new query and run it to check it works as expected.

And personally I'm not sure of the benefit of assigning the value to multiple objects/variables.

After 700+ posts you should be aware of these basics by now. try

Code:
dim sqlstr as string
Me.GetUserNametxt = Environ("username")
sqlstr="INSERT INTO tblUserName (UserName) VALUES ('" & Me.GetUserNametxt & "');"
debug.print sqlstr

DoCmd.RunSQL sqlstr
 

ECEK

Registered User.
Local time
Today, 05:09
Joined
Dec 19, 2012
Messages
717
Thank you for your solutions. I used CJ's solution.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:09
Joined
Oct 29, 2018
Messages
21,467
Hi. I know this is already solved, but I was just wondering if you could have done this as well. Cheers!
Code:
DoCmd.RunSQL "INSERT INTO tblUserName (UserName) VALUES (Environ('username'));"
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:09
Joined
Sep 21, 2011
Messages
14,265
Hi. I know this is already solved, but I was just wondering if you could have done this as well. Cheers!
Code:
DoCmd.RunSQL "INSERT INTO tblUserName (UserName) VALUES (Environ('username'));"

I cannot get Environ('username') to produce anything except an error message in the immediate window?

Code:
sqlstr="INSERT INTO tblUserName (UserName) VALUES ('" & Environ("Username") & "');"

certainly works though
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:09
Joined
Oct 29, 2018
Messages
21,467
I cannot get Environ('username') to produce anything except an error message in the immediate window?

Code:
sqlstr="INSERT INTO tblUserName (UserName) VALUES ('" & Environ("Username") & "');"
certainly works though
Hi. Thanks for confirming it. I guess Environ() is only a VBA function. Cheers!
 

Users who are viewing this thread

Top Bottom