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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Username 1

Status
Not open for further replies.

JonoB

Programmer
May 29, 2003
147
GB
I have used a function from Dev Ashish's website ( in my access project to get the user's network login name. The code is as follows:

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fosUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUsername As String
strUsername = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUsername, lngLen)
If (lngX > 0) Then
fosUserName = Left$(strUsername, lngLen - 1)
fosUserName = LCase(fosUserName)
Else
fosUserName = vbNullString
End If
End Function


Basically, I use this for user tracking, and keep a record of whic user had added records to the database. This all works 100%.

In order to do this, I reference the fosUserName function in various bits of code and in many queries.

My question is this - would I be better off declaring a global variable and assigning fosUserName to this variable right at the start of the application opening. This means that I would not have to keep running the fosUserName function every time I needed to get the username. I would assume that this is a more efficient way of doing things, but not sure if its worth all the trouble.

Your comments/thoughts are appreciated.
 
I use the same function to get the users id and add it as the person who added a record or the person who edited a record.

I put the code in the on click event of both the edit and add record buttons.

HTH

Fred
 
You asked:

would I be better off declaring a [highlight]global variable[/highlight] and assigning fosUserName to this variable right at the start of the application opening. This means that I would not have to keep running the fosUserName function every time I needed to get the username.

Yes, this is the technique that I use all the time. I do also maintain a Function to call up the value if I need to use it in a Query. Because, as you know the Global variable value is not directly available in SQL and must be accessed through a function call.

Good luck.

[COLOR=006633]Bob Scriver[/color]
MIState1.gif
[COLOR=white 006633]MSU Spartan[/color]
 
Ahhh, thanks for pointing that one out to me, Bob.

So, in other words, when I need to get the username anytime in code, I call the global variable.

If I need to call value in a query, then I use the function.

Nice and neat. And more efficient too.

 
Yes, that is correct. You can actually just create another function to call up the Global variable.

Code:
Global gblUserName as String

Public Function UserName() as String
UserName = gblUserName
End Function

Good luck.

[COLOR=006633]Bob Scriver[/color]
MIState1.gif
[COLOR=white 006633]MSU Spartan[/color]
 
Thanks for that Bob.

Just a quick question on defining variables.

My understanding is as follows:
-Defining a variable as Global allows that variable to be used in any part of that project,
-Defining a variable as Public allows it to be used in any projects that reference the project where the public variable is declared.

Is this correct?
 
Yes, your statments above about Public variables is correct. However, it is a little different with Functions as the function I have provided is declared as Public:

[red]Functions can have any of these three optional Declaration:[/red]

Public Optional. Indicates that the Function procedure is accessible to all other procedures in all modules. If used in a module that contains an Option Private, the procedure is not available outside the project.
Private Optional. Indicates that the Function procedure is accessible only to other procedures in the module where it is declared.
Static Optional. Indicates that the Function procedure's local variables are preserved between calls. The Static attribute doesn't affect variables that are declared outside the Function, even if they are used in the procedure.

Post back if you have further questions.

[COLOR=006633]Bob Scriver[/color]
MIState1.gif
[COLOR=white 006633]MSU Spartan[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top