G
Guest
I need to add the windows login user id to my table. Have not done this one
before. Anyone have any suggestions?
before. Anyone have any suggestions?
Ralph said:If the field on your form was named UserID then you could use the AfterUpdate
property on the field to update it, something like:
Private Sub UserID_AfterUpdate()
Me.UserID = fOSUserName
End Sub
has several syntax errors in it that are so fundamental that the line wouldPublic Fuction GetCurrent UserName() As String
RobertTaylor said:Ok I'm sorry but I'm still haveing problems. I have new code in place and
can't get it. I have manged to get the user name to come up and save it
self
to the table with the other information, but anytime I file that record
and
then go to enter another one, the user name disappears and then is not
saved
with any of the rest of the records that I save. Now if I close the
database
and then reopen it, it will display the user ID again but then I get the
same
problem when I file and try for another one. I have a module set up with
the
following information:
Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lPBuffer
As
String, nSize As Long) As Long
Private Declare Fuction GetUserName Lib "ADVAPI32.dll" Alias
"GetUserNameA"
(ByVal lpBuffer As STring, nSize As Long) As Long
Public Fuction GetComputerName() As String
On Error GoTo Err_GetComputerName
Dim Username As String * 255
Call Get ComputerNameA(Username, 255)
GetComputerName - Left$(Username, InStr(Username, Chr$(0)) - 1)
Exit_GetComputerName:
Exit Fuction
Err_GetComputerName:
MsgBox Err.Description
Resure Exit_GetComputerName
End Fuction
-----------------------------------------
Public Fuction GetCurrent UserName() 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)
GetCurrentUserNme = Username & ""
Exit_GetCurrentUserName:
Exit Fuction
Err_getCurrentUserName:
MsgBox Err.Description
Resume Exit_GetCurrentUserName
End Function
---------------------------------------------
Ok thats all the module, now I have an unbound text box on my form and the
name of it is LoginName with the control source as LoginName for the
table.
Now for the actual FORM, I have this code under the On Load:
------------------
Private Sub Form_Load()
Me.LoginName = GetCurrentUserName()
End Function
------------------
That's everything I have for code wise, but I still don't know what needs
to
be done. Any thoughts, and thanks!
Your statement:
Me.UserID = fOSUserName
appears to be in the wrong place.
If you want to record the UserID for new records only, use the form's
BeforeInsert event. If you want to record it for new AND changed
records,
use the form's BeforeUpdate event. Note that this is the event for the
*form*, not for any control.
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
Ok thats all the module, now I have an unbound text box on my form and the
name of it is LoginName with the control source as LoginName for the
table.
Now for the actual FORM, I have this code under the On Load:
------------------
Private Sub Form_Load()
Me.LoginName = GetCurrentUserName()
End Function
------------------
Graham Mandeno said:Hi Robert
Well, it looks like your GetCurrentUserName function will return the
username OK.
So the problem is that the username is not being written to the updated
record?
Quoting from your previous post:
Ok thats all the module, now I have an unbound text box on my form and the
name of it is LoginName with the control source as LoginName for the
table.
Now for the actual FORM, I have this code under the On Load:
------------------
Private Sub Form_Load()
Me.LoginName = GetCurrentUserName()
End Function
------------------
If the textbox has a ControlSource witch is the name of a field in your
table, then it is NOT unbound. It is, by definition, bound to that field.
You are setting its value in Form_Load, which is the wrong place. That will
set the value once only, for the first record in the form. That record may
be an old one, so you would overwrite the username that was in that record
with the current user, no matter whether or not the record was being
updated.
If you want to record the username for any new records AND any changed
records, then use Form_BeforeUpdate:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me!LoginName= GetCurrentUserName
End Sub
If you want to record the username only for new records, then set the
DefaultValue property of your LoginName textbox to:
=GetCurrentUserName()
or, if you don't like the username showing in the box before you start
typing a new record, you could use Form_BeforeInsert:
Private Sub Form_BeforeInsert(Cancel As Integer)
Me!LoginName= GetCurrentUserName
End Sub
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
RobertTaylor said: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
RobertTaylor said:Correct
Graham Mandeno said:Hi Robert
Well, it looks like your GetCurrentUserName function will return the
username OK.
So the problem is that the username is not being written to the updated
record?
Quoting from your previous post:
Ok thats all the module, now I have an unbound text box on my form and
the
name of it is LoginName with the control source as LoginName for the
table.
Now for the actual FORM, I have this code under the On Load:
------------------
Private Sub Form_Load()
Me.LoginName = GetCurrentUserName()
End Function
------------------
If the textbox has a ControlSource witch is the name of a field in your
table, then it is NOT unbound. It is, by definition, bound to that
field.
You are setting its value in Form_Load, which is the wrong place. That
will
set the value once only, for the first record in the form. That record
may
be an old one, so you would overwrite the username that was in that
record
with the current user, no matter whether or not the record was being
updated.
If you want to record the username for any new records AND any changed
records, then use Form_BeforeUpdate:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me!LoginName= GetCurrentUserName
End Sub
If you want to record the username only for new records, then set the
DefaultValue property of your LoginName textbox to:
=GetCurrentUserName()
or, if you don't like the username showing in the box before you start
typing a new record, you could use Form_BeforeInsert:
Private Sub Form_BeforeInsert(Cancel As Integer)
Me!LoginName= GetCurrentUserName
End Sub
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
RobertTaylor said: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
Graham Mandeno said:No hard feelings, Robert. Apology accepted
Did the solution work for you?
--
Graham Mandeno [Access MVP]
Auckland, New Zealand
RobertTaylor said:Please also forgive me for I misread the reply from you. I have just
noticed
that it was longer than what I had originally read. I thought you had
just
asked one question and it was the end of the reply. I noticed the ">" and
thought it was previous messages so I ignored them. Again I am VERY
SORRY!!
RobertTaylor said:Yes, it worked PERFECT! Again, I'm sorry, I feel horrible. It was my
mear
stupidity that caused this. Thank you for working with me and helping me
with everything. I'll definatly look you up next time I have a problem!
Graham Mandeno said:No hard feelings, Robert. Apology accepted
Did the solution work for you?
--
Graham Mandeno [Access MVP]
Auckland, New Zealand
RobertTaylor said:Please also forgive me for I misread the reply from you. I have just
noticed
that it was longer than what I had originally read. I thought you had
just
asked one question and it was the end of the reply. I noticed the ">"
and
thought it was previous messages so I ignored them. Again I am VERY
SORRY!!
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.