Locking field using UserName

yeppy12

Registered User.
Local time
Today, 12:52
Joined
Sep 11, 2007
Messages
38
I want to add another user name to the following code, but I am not sure how. For example, I want to add "joesmith22" so that either joe or mwilson can edit the field. Can someone help with the syntax? Thanks.

Code:
Private Sub txtDate_Received_GotFocus()
If Environ("UserName") = "mwilso1" Then
  Me.txtDate_Received.Locked = False
Else
  If Len(Nz(Me.txtDate_Received)) = 0 Then
     Me.txtDate_Received.Locked = False
  Else
     Me.txtDate_Received.Locked = True
  End If
End If
End Sub
 
Code:
If Environ("UserName") = "mwilso1" Or Environ("UserName") = "joesmith22" Then
 
I would highly suggest not hard coding the names in there. Use a table so that if things change you (or someone else) can change the names and ID's. If you don't then if Joe Smith leaves and someone else gets his job then you will need to go fix code.

It's much better to always try to think "reusable" than to hard code something.
 
Cheers to that Bob. I am always telling people at work to use tables instead of hardcoding values. It makes your code so much more dynamic and easy to maintain.
 
Ok. Thanks for the help. How would I switch my code from hard-coded user names to a table with user names?
 
For example lets say you create the below table. Its not formatted that nice but its late.

Username(PK)
FirstName
LastName
Title


Then your code would look similar to below



Private Sub txtDate_Received_GotFocus()



dim strUsername as string

strUsername=environ("username")

If IsNull(Dlookup("UserName',"tblUsers","UserName='" & strUserName & "'"))=false Then
Me.txtDate_Received.Locked = False
Else
If Len(Nz(Me.txtDate_Received)) = 0 Then
Me.txtDate_Received.Locked = False
Else
Me.txtDate_Received.Locked = True
End If
End If
End Sub
 
Ok. Thanks. I have to play around with your code to get it to work...it keeps giving me errors for some reason.
 
Well, it looks like Keith's code had a single quote at the end of UserName in the first part of the Dlookup. So, corrected it would look like this:

If IsNull(Dlookup("UserName","tblUsers","UserName='" & strUserName & "'"))=false Then
 

Users who are viewing this thread

Back
Top Bottom