retrieve windows login id

G

Guest

I need to add the windows login user id to my table. Have not done this one
before. Anyone have any suggestions?
 
G

Guest

Thanks Ralph -

How do I go about getting strUserName to my table? Do i call foSUserName
when form loads or can I add my table to this function to populate UserName
field in my table.
 
G

Guest

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
 
G

Guest

Thanks Ralph - works great.
--
Leslie


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
 
R

RobertTaylor

I'm still having problems. I've got the user name to appear but every time I
file the record and then go to create a new record and file that one, the
user name disappears and does not store on the table. Was wondering if
anyone could help me here? Thanks in advance!
 
G

Graham Mandeno

Hi Robert

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.
 
R

RobertTaylor

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
 
G

Graham Mandeno

Hi Robert

The code you have posted here is full of errors and would not compile, never
mind produce the desired result.

This one line:
Public Fuction GetCurrent UserName() As String
has several syntax errors in it that are so fundamental that the line would
be highlighted in red!

Can you please post the actual code from your module?

Oh, and ensure you have Option Explicit at the top of all your modules.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand


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
 
R

RobertTaylor

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
 
G

Graham Mandeno

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
 
R

RobertTaylor

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
 
G

Graham Mandeno

What is correct?

Is my assumption about the nature of your badly explained problem correct?

Is my assumption about your misunderstanding of the meaning of "unbound"
correct?

Is my assumption about your code being in the wrong place correct?

Does the fact that the solution I have supplied solves your problem make me
"correct"?

Many folks around here don't respond to the answers to their questions and I
just shrug and hope that it's because they didn't see them, and maybe
someone else benefitted from the help.

But I have to say, Robert, that your simple response of "Correct" ranks
among the most rude and hurtful I have seen in more than 13 years of
participation as an unpaid volunteer in these newsgroups.

I hope I have misinterpreted your message. Please tell me this is so.
--

Graham Mandeno [Access MVP]
Auckland, New Zealand


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
 
R

RobertTaylor

Forgive me if it came across that way, for I had no intentions for it to be
that way. Unless I read the reply incorrectly before I stated "correct", I
was repling to the question that you had previously asked which was, "So the
problem is that the username is not being written to the updated record?"
Again, I'm sorry if that came across the wrong way, for it is not my
intentions to upset anyone or be rude/crude or disrespectful in any way.
Please accept my apologies. I am truly and deeply sorry!
 
R

RobertTaylor

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!!
 
R

RobertTaylor

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!!
 
G

Graham Mandeno

Great! I'm glad you got it all working. Thanks for the feedback.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

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!!
 

Ask a Question

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.

Ask a Question

Top