By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,974 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,974 IT Pros & Developers. It's quick & easy.

Storing Windows Name In Table

P: 15
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 = GetCurrentUserName()

This is the code that I have in the module:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
  4. Private Declare Function GetUserName Lib "ADVAPI32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  6. Public Function GetComputerName() As String
  7. On Error GoTo Err_GetComputerName
  8. Dim Username As String * 255
  9. Call GetComputerNameA(Username, 255)
  10. GetComputerName = Left$(Username, InStr(Username, Chr$(0)) - 1)
  12. Exit_GetComputerName:
  13.     Exit Function
  15. Err_GetComputerName:
  16.         MsgBox Err.Description
  17.         Resume Exit_GetComputerName
  19. End Function
  21. Public Function GetCurrentUserName() As String
  22. On Error GoTo Err_GetCurrentUserName
  23.  Dim lpBuff As String * 25
  24.  Dim ret As Long, Username As String
  25.    ret = GetUserName(lpBuff, 25)
  26.    Username = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
  27.    GetCurrentUserName = Username & ""
  29. Exit_GetCurrentUserName:
  30.     Exit Function
  32. Err_GetCurrentUserName:
  33.         MsgBox Err.Description
  34.         Resume Exit_GetCurrentUserName
  35. End Function
And in my form this is the complimenting code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.    Me.LoginName = GetCurrentUserName()
  3. End Sub
and I have a Macro that has the action to OpenForm and then my form name below.
Nov 9 '08 #1
Share this Question
Share on Google+
5 Replies

Expert 2.5K+
P: 3,532
I suspect Form_Open fires too early to assign anything. Try moving

Me.LoginName = GetCurrentUserName()

to the Form_Load event.

Welcome to Bytes!

Linq ;0)>
Nov 9 '08 #2

P: 15
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.
Nov 10 '08 #3

P: 50
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.Caption instead of just LoginName. Otherwise, everything else will be the same.
Nov 10 '08 #4

P: 15
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.
Nov 10 '08 #5

P: 15
ISSUE RESOLVED. Thanks for all the help.
Nov 11 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.