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

Getting User Name form Login (System enviroment variable?)

P: n/a
I posted this request earlier and was directed to some code that would not
work. I am using win2000 Professional. I want to find out who is logged in
to the computer and take that ID look it up in a table and put the persons
name into a combo box on the form. I think I can handle the combo box part
but can not get the User Login ID.

I have triedthe following but always come with no one loged in and of course
I am logged into my computer.

Option Compare Database
Declare Function wu_GetUserName Lib "advapi32" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long

Private Const BIF_RETURNONLYFSDIRS = &H1

Function NetworkUserName() As String

Dim lngStringLength As Long
Dim sString As String

lngStringLength = Len(sString)
sString = String$(iStringLength, 0)

If wu_GetUserName(sString, lngStringLength) Then
networkname = Left$(sString, lngStringLength)
Else
networkname = "Unknown"
End If

End Function

I step through the code at the If above and always go to the else stmt and
ge "Unknown" as the answer

Any Ideas of what is wrong.
Nov 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Dave,

Try this method instead:
ACC2000: How to Retrieve Workgroup Information Under Win32
http://support.microsoft.com/?ID=210088

Also, I recommend that you always use Option Explicit. Your code was using an
undeclared variable. You can configure the VBA editor to always include this in
new code modules, by clicking on Tools > Options (when viewing code) and placing
a check in Require Variable Declaration.

Tom
_______________________________

"Dave N" <ne*********@johndeere.com> wrote in message
news:3f********@news1.dpn.deere.com...
I posted this request earlier and was directed to some code that would not
work. I am using win2000 Professional. I want to find out who is logged in
to the computer and take that ID look it up in a table and put the persons
name into a combo box on the form. I think I can handle the combo box part
but can not get the User Login ID.

I have triedthe following but always come with no one loged in and of course
I am logged into my computer.

Option Compare Database
Declare Function wu_GetUserName Lib "advapi32" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long

Private Const BIF_RETURNONLYFSDIRS = &H1

Function NetworkUserName() As String

Dim lngStringLength As Long
Dim sString As String

lngStringLength = Len(sString)
sString = String$(iStringLength, 0)

If wu_GetUserName(sString, lngStringLength) Then
networkname = Left$(sString, lngStringLength)
Else
networkname = "Unknown"
End If

End Function

I step through the code at the If above and always go to the else stmt and
ge "Unknown" as the answer

Any Ideas of what is wrong.

Nov 12 '05 #2

P: n/a
your string length is too short (ie. 24)
try this
Function NetworkUserName() As String

Dim lngStringLength As Long
Dim sString As String

lngStringLength = 255 'Len(sString)
sString = String$(lngStringLength, 0)

If wu_GetUserName(sString, lngStringLength) Then
NetworkUserName = Left$(sString, lngStringLength)
Else
NetworkUserName = "Unknown"
End If

End Function

"Dave N" <ne*********@johndeere.com> wrote in message news:<3f********@news1.dpn.deere.com>...
I posted this request earlier and was directed to some code that would not
work. I am using win2000 Professional. I want to find out who is logged in
to the computer and take that ID look it up in a table and put the persons
name into a combo box on the form. I think I can handle the combo box part
but can not get the User Login ID.

I have triedthe following but always come with no one loged in and of course
I am logged into my computer.

Option Compare Database
Declare Function wu_GetUserName Lib "advapi32" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long

Private Const BIF_RETURNONLYFSDIRS = &H1

Function NetworkUserName() As String

Dim lngStringLength As Long
Dim sString As String

lngStringLength = Len(sString)
sString = String$(iStringLength, 0)

If wu_GetUserName(sString, lngStringLength) Then
networkname = Left$(sString, lngStringLength)
Else
networkname = "Unknown"
End If

End Function

I step through the code at the If above and always go to the else stmt and
ge "Unknown" as the answer

Any Ideas of what is wrong.

Nov 12 '05 #3

P: n/a

Your string is zero length going into the function call it has to be padded
to a length at least equal to what you are expecting to be returned.
Look at

http://www.mvps.org/access/api/api0008.htm
Terry

"Dave N" <ne*********@johndeere.com> wrote in message
news:3f********@news1.dpn.deere.com...
I posted this request earlier and was directed to some code that would not
work. I am using win2000 Professional. I want to find out who is logged in to the computer and take that ID look it up in a table and put the persons
name into a combo box on the form. I think I can handle the combo box part but can not get the User Login ID.

I have triedthe following but always come with no one loged in and of course I am logged into my computer.

Option Compare Database
Declare Function wu_GetUserName Lib "advapi32" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long

Private Const BIF_RETURNONLYFSDIRS = &H1

Function NetworkUserName() As String

Dim lngStringLength As Long
Dim sString As String

lngStringLength = Len(sString)
sString = String$(iStringLength, 0)

If wu_GetUserName(sString, lngStringLength) Then
networkname = Left$(sString, lngStringLength)
Else
networkname = "Unknown"
End If

End Function

I step through the code at the If above and always go to the else stmt and
ge "Unknown" as the answer

Any Ideas of what is wrong.

Nov 12 '05 #4

P: n/a
My situation may have been a little different. I wanted the username for
anyone creating a new record in my Employee and/or Evaluations Tables. I
could not get the VBA code to work so I solved the problem by adding another
field to my tables called Evaluator then set the default value
=Environ("Username"). My problem was solved.

Wayman

"Dave N" <ne*********@johndeere.com> wrote in message
news:3f********@news1.dpn.deere.com...
I posted this request earlier and was directed to some code that would not
work. I am using win2000 Professional. I want to find out who is logged in to the computer and take that ID look it up in a table and put the persons
name into a combo box on the form. I think I can handle the combo box part but can not get the User Login ID.

I have triedthe following but always come with no one loged in and of course I am logged into my computer.

Option Compare Database
Declare Function wu_GetUserName Lib "advapi32" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long

Private Const BIF_RETURNONLYFSDIRS = &H1

Function NetworkUserName() As String

Dim lngStringLength As Long
Dim sString As String

lngStringLength = Len(sString)
sString = String$(iStringLength, 0)

If wu_GetUserName(sString, lngStringLength) Then
networkname = Left$(sString, lngStringLength)
Else
networkname = "Unknown"
End If

End Function

I step through the code at the If above and always go to the else stmt and
ge "Unknown" as the answer

Any Ideas of what is wrong.

Nov 12 '05 #5

P: n/a
"Wayman Bell" <wa********@comcast.net> wrote:
My situation may have been a little different. I wanted the username for
anyone creating a new record in my Employee and/or Evaluations Tables. I
could not get the VBA code to work so I solved the problem by adding another
field to my tables called Evaluator then set the default value
=Environ("Username"). My problem was solved.


Trouble is anyone can go to a command prompt and change that variable.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.