Code Help

CanWest

Registered User.
Local time
Today, 03:08
Joined
Sep 15, 2006
Messages
272
Does anyone know if there is a way to change the value of currentuser()

I have a situation where using access's built in security is not an option. I have six databases that i need to use in this environment. A lot of the code is built around the currentuser() expression. In the new environment i would like to set the value of currentuser() to Environ$("username")

Is this possible??
 
Note that "CurrentUser" is actually "Application.CurrentUser".
Since this is the case you can define your own global variable or property that uses the same name and VBA will consume it first, since it will be the more local of the two references.
For instance...

Code:
[COLOR="Green"]'assign a value to this variable is any module[/COLOR]
public CurrentUser as string

Code:
public property Get CurrentUser as string
  [COLOR="Green"]'Your custom logic goes here, for example...[/COLOR]
  Select Case Environ("UserName")
    Case "Kathy", "Katie", "Karl"
      CurrentUser = "Admin"
    Case Else
      CurrentUser = Environ("UserName")
  End Select
end property
 
Modules

Please forgive me as I am a vba newbie. My database has no modules so I will assume that i must create one. How do I call the module from an open event of a form.
 
you do not normally call modules. You can call functions and procedures. I don't think lagbolt gave you a function. He just created a custom property that assigns a string value to a variable that represents the current user logged into the machine. Use it however it is appropriate...
 
Module

Ok this is what I did

I placed The following code in the declaration section

Code:
Public CurrentUser As String

And this code in the CurrentUser [PropertyGet] section which was created when i pasted the code into a module. (Turns out I did have a module in this db)

Code:
Public Property Get CurrentUser() As String

      CurrentUser = Environ("UserName")

End Property

For my requirements a case was not needed.

It returned an invalid syntax error. Any ideas
 
I may be way out of my league here I guess. I have never used the Environ() function before, but looking it up tells me that it is not really associated with a user. It says something about an environment variable, which I take that as not the same as an actual user of the application.

If all you want to do is get the current user of the application, could you not still use application.currentuser as lagbolt described?

I may be getting confused myself here. If I am way off, just let me know and I'll get out of the way... :D
 
Environ("username") will normally provide the NT login of the person logged in on the computer. However, since it is possible to modify environment variables, the better way to get the current user name is to use this API:
http://www.mvps.org/access/api/api0008.htm
 
Username

I am not having any trouble getting either the currentuser() or the Login username. What i want to do is replace the currentuser() which is normally admin in an unsecured db with the value of the Environ("username") or the value of the api. It really does not matter. I don't have an issue of people knowing how to change environment variables.
 
I don't really think you can do this. CurrentUser has specific meaning within Access. It is the currently logged on user through the MDW security file. If you haven't secured your database, then it uses the only user that is in the System.MDW file - Admin so all current users are Admin. You can't modify what the security model is using otherwise it would be even less secure than it already is.
 
Okay, another Access MVP (Henry Habermacher) provided this:
Henry Habermacher said:
He may use the approach of overtyping the CurrentUser() Funktionality. I
just gave it a try and it worked out correctly. The only thing he needs is a
public function CurrentUser() that contains following code in a standard
module:

Public Function CurrentUser() As String
CurrentUser = Environ("UserName")
End Function

From now on CurrentUser() should return the UserName from the Environment
insteade of the Access Login User. His should work in VBA, in Expressions
and even in Queries. The scope of above function should make it being
executed instead of the CurrentUser() Methode of Access.

If he enters:
? CurrentUser()
He should get his Windows UserName

If he wants to read the user that is logged on to Access then he has to use:
? Access.CurrentUser()

He may give it a try and maybe it helps to do what he wants to do. Jet will
continue to run as "Admin" if he is not signed on explicitly, but this may
be ok. It would be helpful if you could give feedback here if this
workaround of not being able to change the current user in Jet helped and if
he has seen some sideeffects.
 
Ya, as previously mentioned...
Note that "CurrentUser" is actually "Application.CurrentUser".
Since this is the case you can define your own global variable or property that uses the same name and VBA will consume it first, since it will be the more local of the two references.
;)
 

Users who are viewing this thread

Back
Top Bottom