Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Update Field to current logged in user ID with new record command button Expand / Collapse
Author
Message
Posted Wednesday, July 17, 2013 12:03 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, October 13, 2014 8:14 AM
Points: 101, Visits: 322

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.

Post #1474753
Posted Wednesday, July 17, 2013 12:06 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 25, 2014 8:35 PM
Points: 187, Visits: 434
The correct column is picked therefore I don't believe that is the issue.

What else could I be missing?
Post #1474755
Posted Wednesday, July 17, 2013 12:33 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, October 13, 2014 8:14 AM
Points: 101, Visits: 322
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

Post #1474762
Posted Wednesday, July 17, 2013 1:12 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 25, 2014 8:35 PM
Points: 187, Visits: 434
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?
Post #1474779
Posted Wednesday, July 17, 2013 1:41 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, October 13, 2014 8:14 AM
Points: 101, Visits: 322
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.
Post #1474786
Posted Wednesday, July 17, 2013 2:07 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 25, 2014 8:35 PM
Points: 187, Visits: 434
Yes!!!

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

It Works!

Public Function GetWindowUser() as String
GetWindowUser = Environ("USERNAME")
End Function
Post #1474791
Posted Wednesday, July 17, 2013 2:51 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 9:23 AM
Points: 154, Visits: 562
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!
Post #1474806
Posted Friday, July 19, 2013 6:55 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 25, 2014 8:35 PM
Points: 187, Visits: 434
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!
Post #1475444
Posted Friday, July 19, 2013 8:54 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 9:23 AM
Points: 154, Visits: 562
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!
Post #1475535
Posted Wednesday, August 7, 2013 2:54 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 25, 2014 8:35 PM
Points: 187, Visits: 434
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.
Post #1482071
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse