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!

Return User Name

Status
Not open for further replies.

Moxy1

Programmer
Aug 29, 2002
75
US
I'm calling code when a form is opened to return the user name and pass it to a field on the form. Everytime I go to open the form I get an error, "Object doesn't support this property or method (Error 438)". I've used this code before to write to a table without any problems. Any ideas?

Code:
Private Sub Form_Open(Cancel As Integer)
DoCmd.Maximize
    Me.txtUser = username
End Sub

Code:
Option Compare Database

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


Function username()

On Error GoTo Problem

    Dim nSize As Long
    Dim lpBuffer As String
    Dim computername As String
        
    Dim rec As Recordset
    Dim inpname As String
    
    nSize = 255
    lpBuffer = Space$(nSize)

    If GetUserName(lpBuffer, nSize) Then
        username = Left$(lpBuffer, nSize)
    Else
        username = ""
    End If


rec.Close


Exit Function

Problem:

Exit Function

MsgBox Err.Description


End Function

Thanks
 
Could it be that "username" has not been dimmed as a global variable in the new app?

I wondered why the baseball was getting bigger.
Then it hit me.
 
I have updated the code as follows and still get the same error.

Code:
Option Compare Database

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


Function username() As String

On Error GoTo Problem

    Dim nSize As Long
    Dim lpBuffer As String
    Dim computername As String
        
    Dim rec As Recordset
    Dim inpname As String
    
    nSize = 255
    lpBuffer = Space$(nSize)

    If GetUserName(lpBuffer, nSize) Then
        username = Left$(lpBuffer, nSize)
    Else
        username = ""
    End If


rec.Close


Exit Function

Problem:

Exit Function

MsgBox Err.Description


End Function
 
My code for this is a little more simple than yours, but it might do the trick for you. I just set the field to the Environ("username").

Example:
Forms!FormName!txtChangedBy = Environ("UserName")
 
I'd use the Load event procedure instead of the Open ...
 
Naming advapi32.dll GetUserNameA is really cool, but it is a bit unusual. I bet 95% of everyone using vba uses environ("USERNAME"). Try using that and see what happens. I'm pretty sure that environ("USERNAME") can return null, so keep that in mind.
 
But the simple answer to the original question is to add the parentheses to the function call:
Code:
Me.txtUser = username[COLOR=red]()[/color]

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top