Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application.currentuser and environ("Username") 1

Status
Not open for further replies.

Grieve1

Programmer
Jun 11, 2008
33
GB
Hi
Can anyone show me how to call either application.currentuser or environ("Username")the exact code. Please I have been playing around with it but no joy. I have Access security set and trying to allocate different forms to different users on opening the database.

Many thanks
 
I have a form with Currentuser text box on the event before update of the form my code is [currentuser]= environ.("username")
 
There should not be a stop in the line:

environ("username")
 
Removed the stop but code doing nothing no error no update
 
Has the update event occurred? Does Envion("UserName") return anything in the immediate window? It is best to rename textboxes to ensure that the name does not conflict with fieldnames [currentuser] is likely to be the field name, try:

Me.CurrentUser

Or rename:

Me.txtCurrentUser

 
I am obviously not doing something right nothing is being returned. this is my code
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.currentuser = Environ("username")
End Sub

Thanks for your time and effort
 
How about my question about the immediate window? Have you set a breakpoint in the code to ensure that the event runs?
 
sorry I have set a breakpoint and nothing running
 
If you want the field to update regardless of whether the form is updated, you need to choose a different event. The Current event of the form runs for every record, that might suit, it depends what you want to do.
 
Do you have an autoexec module that runs when the DB is opened? If so you can store the user name in a public variable and use the api to get the user name. Sometimes the Enviroment variables aren't reliable as they can be changed.

If you have an autoexec module put this at the top

Code:
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public sUserName as string

Then try something like this:

Code:
Dim lpBuff          As String * 1024

'get the user name
GetUserName lpBuff, Len(lpBuff)
sUserName = Left$(lpBuff, (InStr(1, lpBuff, vbNullChar)) - 1)
lpBuff = vbNullString
debug.print sUserName

This should print the current user in the immediate window.
 
jadams0173

However, this will not help if the form is not updated and the update event is used to write the data, yesno?
 
I was thinking that if the OP already had the current user from the autoexec, they could populate the text box using the Form_Load event since all that was in his Before_Update Event was populating the user name text box.

Grieve1 said:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.currentuser = Environ("username")
End Sub
 
When using jadams0173 code on event load form i get my name returned in immediate window but not on form. Thanks to both of you
 
Grieve1,

In the Form_load event of the form try:

me![yourtextboxname]=sUserName
 
I have previously suggested the Current event of the form, and I still suggest that event as it will allow you to check if the box has already been filled in. However, if you only want the box to be updated when the data is changed, stick with the update event and update the record to check that the code is working properly.
 
Many thanks to each of you for your time and expertise it all working now
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top