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

Help With Functions 1

Status
Not open for further replies.

3239

Technical User
May 14, 2003
64
I'm am just getting my feet wet in programming functions. There is one function that I have found off of The Access Web website written by Dev Ashish, that allows you to retrieve a user's network User Name. What I am trying to do is use this function to restrict access to forms in my database based on the user's Windows login information. My question is how do I reference this function in my forms. Here is the function:

Code:
'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' 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 **************************

Thanks.
 
to get username from listed function:

Sub test()
Dim strUserName As String
strUserName = fOSUserName()
End Sub


In place of the code you found, you might use

Sub test()
Dim strUserName As String
strUserName = Environ("USERNAME")
End Sub

NO further code neded ;0)

 
I would always use the API function rather than the Environ() which is not as reliable.

You could use the function in the On Open event of a form like:

Code:
Private Sub Form_Open(Cancel As Integer)
    If fOSUserName() <> "User3239" Then
        MsgBox "Opening Readonly"
        Me.AllowEdits = False
    End If
End Sub

Duane
Hook'D on Access
MS Access MVP
 
Thanks KimFreeze!

That worked! Your post was helpful!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top