Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update Field to current logged in user ID with new record command button


Update Field to current logged in user ID with new record command button

Author
Message
R. Brush
R. Brush
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 346
If your form is "bound" to a recordsource (table or view), the textbox's control source would be the name of the column in the recordsource that will receive the value. You can open the textbox properties and click on the dropdown for the control source to see a list of columns. Then just pick the correct one.
kwoznica
kwoznica
SSC Veteran
SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)

Group: General Forum Members
Points: 221 Visits: 475
The correct column is picked therefore I don't believe that is the issue.

What else could I be missing?
R. Brush
R. Brush
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 346
You haven't indicated that the line of code:
Me.txtUserID = Environ("UserName")
is executing. You will have to figure that out.

I would take out the line:
On Error Resume Next
Also, set VB Editor to "Break on all Errors"

Try this code instead of what you have:

Private Sub cmdNewRecord_Click()
On Error GoTo cmdNewRecord_Click_Err

If Not .NewRecord Then DoCmd.RunCommand acCmdRecordsGoToNew

Me.txtUserID = Environ("UserID")


cmdNewRecord_Click_Exit:
Exit Sub

cmdNewRecord_Click_Err:
MsgBox Err.Number & " - " & Err.Description
Resume cmdNewRecord_Click_Exit

End Sub
kwoznica
kwoznica
SSC Veteran
SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)

Group: General Forum Members
Points: 221 Visits: 475
Mr Brush,

The line of code is definitely not executing.

I added break on all errors and the vb editor doesn't pop up.


Also if I set the default value of the property for the txtUserID box to Functions/Built In Functions/Database =CurrentUser() then create a new record it writes it as Admin in the text box. I am not logged into the database nor my computer as Admin so I'm not sure where it is pulling this data.

Please let me know if you have any other thoughts?
R. Brush
R. Brush
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 346
I can't help you get the line of code to execute, since I cannot debug it myself.

The reason CurrentUser() returns Admin is that CurrentUser() is returning the Access security user, which is by default, "Admin."

Here is another option...
Forget the Me.txtUserID = code in the cmdNew_click sub.

Add the following function to a VB Module (not a form class module):
Public Function GetWindowUser() as String
GetWindowUser = Environ("USERNAME")
End Function

Then in the txtUserID Default property (instead of CurrentUser()), put:
=GetWindowUser()

That should work as an alternative.
kwoznica
kwoznica
SSC Veteran
SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)

Group: General Forum Members
Points: 221 Visits: 475
Yes!!!

That will suffice. Thank you so much!!!!!!

It Works!


Public Function GetWindowUser() as String
GetWindowUser = Environ("USERNAME")
End Function

WendellB
WendellB
SSC Veteran
SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)

Group: General Forum Members
Points: 247 Visits: 1634
kwoznica (7/17/2013)
Thanks for the suggestions but I have not been successful.

I used Environ("UserName"), suser_sname(), and select system_user to populate the value in the text box on the form.

I am setting this in the Control Source of the Textbox Data Properties. After doing so it returns a value of #Name? or Admin. I am no logged in as either of these ID's.

....

If you are going to let SQL Server do the work for you, then you need to set the Default Value in the table definition in SQL Server, not in an Access text box. The T-SQL for doing that is:

ALTER TABLE [dbo].[YourTable] ADD CONSTRAINT [DF_YourTable_strWhoEdited] DEFAULT (suser_sname()) FOR [strWhoEdited]


However we usually use the table designer SSMS to set such things as default values, Null or Not Null, etc.

Wendell
Colorado, USA

Wendell
Colorful Colorado
You can't see the view if you don't climb the mountain!
kwoznica
kwoznica
SSC Veteran
SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)

Group: General Forum Members
Points: 221 Visits: 475
Thanks Wendal.

I like that approach to make the backend do the work. I didn't realize suser_sname() was meant to be put in the table definition which is why I was confused. I'll give it a try.

I really appreciate everyone's suggestions!
WendellB
WendellB
SSC Veteran
SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)

Group: General Forum Members
Points: 247 Visits: 1634
There are several advantages to that approach - it works whether the record is created using a form or added at the table level. It also uses the system level authentication ID, so if you have users roaming across several workstations, it still captures the correct name. And it doesn't complicate your form design by using VBA to track things. The one thing it doesn't do is track who last changed the record. That can be done on an Access form, but you can't deal with changes made at the table level. SQL Server triggers are the best way of doing that IMHO.

Wendell
Colorado, USA

Wendell
Colorful Colorado
You can't see the view if you don't climb the mountain!
kwoznica
kwoznica
SSC Veteran
SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)

Group: General Forum Members
Points: 221 Visits: 475
Wendell,

When I made the change to add suser_sname() as the default value for the field it works well only from my system.
If I launch the access file from another user's system the field does not populate. I have tried setting the user's having the issue in the db_owner role for the database but the same thing still occurs.

Is there a specific permission that needs to be set in order to enable the user updating or adding a record on the form?

Also my system is running access 2013 with sql server 2012 native client odbc driver. The user systems have access 2010 running sql server 2008 R2 native client odbc driver.

Please let me know what you think.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search