retreving the network login name in a text box

J

Jason Lim

Dear all,

I am referring to this article:
http://www.mvps.org/access/api/api0008.htm

I thought of having a text box which displays the user's login name,
and save it to a table. So I create a text box, and bound it a field.

And the following is the code for retreving the login name. Very sorry
for my silly question, but I really know nothing about VB. errrm what
should I do with the code? Save it as a module? Let's say I name it
fOSUserName. Then, how do I call the function or the module? How to
relate it to the text box?

Thanks a lot in advance... =)

'******************** Code Start **************************
' This code was originally written by Dev Ashish.
'
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)
Else
fOSUserName = vbNullString
End If
End Function
'******************** Code End **************************
 
L

Lou

Dear all,

I am referring to this article:http://www.mvps.org/access/api/api0008.htm

I thought of having a text box which displays the user's login name,
and save it to a table. So I create a text box, and bound it a field.

And the following is the code for retreving the login name. Very sorry
for my silly question, but I really know nothing about VB. errrm what
should I do with the code? Save it as a module? Let's say I name it
fOSUserName. Then, how do I call the function or the module? How to
relate it to the text box?

Thanks a lot in advance... =)

'******************** Code Start **************************
' This code was originally written by Dev Ashish.
'
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)
    Else
        fOSUserName = vbNullString
    End If
End Function
'******************** Code End **************************

I like to gather all my Windows api calls into a single code module
called ModWinApi. Often Windows functions repeatedly use the same
symbolic constant values. If they are all in a single code module,
these constants can have module level scope rather than global scope
or redeclaration in multple forms modules.

This means that the functions must be declared with Public scope.

In form modules, I refer to these functions with their full name,
i.e.:
sUserName = ModWinApi.fOSUserName().
or
Me.lblOsUserName.caption = ModWinApi.fOSUserName()

That reminds me where the function resides and that I should reuse
"known good" code.

A side benefit of the single code module is that it can be imported
into other Access forms mdb files.
 
P

pietlinden

I like to gather all my Windows api calls into a single code module
called ModWinApi.  Often Windows functions repeatedly use the same
symbolic constant values. If they are all in a single code module,
these constants can have module level scope rather than global scope
or redeclaration in multple forms modules.

This means that the functions must be declared with Public scope.

In form modules, I refer to these functions with their full name,
i.e.:
           sUserName = ModWinApi.fOSUserName().
           or
           Me.lblOsUserName.caption = ModWinApi.fOSUserName()

That reminds me where the function resides and that I should reuse
"known good" code.

A side benefit of the single code module is that it can be imported
into other Access forms mdb files.

actually, if you're saving the value, you need

Me.txtOSUserName = ModWinAPI.fOSUserName()

otherwise it will only show on the form, but never be recorded on the
table.
There's not really a point in putting it in the DefaultValue property
of the control, unless you want the user to be able to override it..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top