Storing Windows Name In Table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RTaylor1853
    New Member
    • Nov 2008
    • 15

    Storing Windows Name In Table

    I've got a textbox that when in form view it displays the users name just fine. but when i go to set the control source to the LoginName so it saves it to the table I get this error:

    Run-time error '-2147352567 (80020009)' You can't assign a value to this object.

    I click on Debug and it brings up the code at the very bottom and highlights this:

    Me.LoginName = GetCurrentUserN ame()

    This is the code that I have in the module:
    Code:
    Option Compare Database
    Option Explicit
    Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
    Private Declare Function GetUserName Lib "ADVAPI32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Public Function GetComputerName() As String
    On Error GoTo Err_GetComputerName
    Dim Username As String * 255
    Call GetComputerNameA(Username, 255)
    GetComputerName = Left$(Username, InStr(Username, Chr$(0)) - 1)
    
    Exit_GetComputerName:
        Exit Function
    
    Err_GetComputerName:
            MsgBox Err.Description
            Resume Exit_GetComputerName
    
    End Function
    
    Public Function GetCurrentUserName() As String
    On Error GoTo Err_GetCurrentUserName
     Dim lpBuff As String * 25
     Dim ret As Long, Username As String
       ret = GetUserName(lpBuff, 25)
       Username = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
       GetCurrentUserName = Username & ""
    
    Exit_GetCurrentUserName:
        Exit Function
    
    Err_GetCurrentUserName:
            MsgBox Err.Description
            Resume Exit_GetCurrentUserName
    End Function
    And in my form this is the complimenting code:

    Code:
    Private Sub Form_Open(Cancel As Integer)
       Me.LoginName = GetCurrentUserName()
    End Sub
    and I have a Macro that has the action to OpenForm and then my form name below.
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    I suspect Form_Open fires too early to assign anything. Try moving

    Me.LoginName = GetCurrentUserN ame()

    to the Form_Load event.

    Welcome to Bytes!

    Linq ;0)>

    Comment

    • RTaylor1853
      New Member
      • Nov 2008
      • 15

      #3
      THANKS!!!!! I got that to work, but I still have a little problem. When I open the database it automatically goes to a record. Then it will store that persons name in the table and then when i scroll to another record it erases that name that is displayed and won't store anymore names. Any thoughts? Again thanks for the help and the welcome.

      Comment

      • Krandor
        New Member
        • Aug 2008
        • 50

        #4
        Make Loginname a label that is not tied to the underlying table. That way you can populate it without affecting the data.

        Your command to populate the field will be a little different with a label. You have to use Loginname.Capti on instead of just LoginName. Otherwise, everything else will be the same.

        Comment

        • RTaylor1853
          New Member
          • Nov 2008
          • 15

          #5
          Sorry, I'm not that advanced..where would I add the .Caption? I removed the text box and changed it to a label and then played around it with every way that I could but could not get anything to come up.

          Comment

          • RTaylor1853
            New Member
            • Nov 2008
            • 15

            #6
            ISSUE RESOLVED. Thanks for all the help.

            Comment

            Working...