Getting NT User Login Names from Access 2000 or 2003

M

Michael Miller

I have a function called fOSUserName, that I found, to get the login name of
the person, when they open my main form.

code:
Private Declare Function apiGetUserName Lib "C:\WINNT\system32\advapi32.dll"
Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function fOSUserName() As String
'returns the network login name

Dim lngLen As Long
Dim lngX As Long
Dim strUserName As String

strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen - 1)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen)
Else
fOSUserName = vbNullString
End If

End Function

Problem is that it's returning mmiller followed by a bunch of square boxes.
The Left$ (in the code) and a try of Trim and a LTrim(RTrim()) is not fixing
it. When I try to assign it to a field in a log table, it always says that
my data is too big for the field.
When I assign it to a label, it shows up fine, as the boxes are not visible.

Any ideas? Thanks.
 
M

Michael Miller

Nothing is missing and it does find MMILLER, but has over 200 spaces after it.
The additional spaces will not allow it to be assigned to a field.

This code does match the user name with a string value tho, even given the
spaces:
Select Case LCase(fOSUserName())

Case "mmiller"
cmdDeveloperClose.Visible = True
cmdStarsUpdate.Visible = True
lblHello.Visible = True
lblHello.Caption = lblHello.Caption & " Michael"

--
MichaelM


Jeff Conrad said:
Sounds like maybe a References problem. See:

http://home.bendbroadband.com/conradsystems/accessjunkie/references.html

Post back with results.
--
Jeff Conrad
Access Junkie
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
I have a function called fOSUserName, that I found, to get the login name of
the person, when they open my main form.

[code snipped]
Problem is that it's returning mmiller followed by a bunch of square boxes.
The Left$ (in the code) and a try of Trim and a LTrim(RTrim()) is not fixing
it. When I try to assign it to a field in a log table, it always says that
my data is too big for the field.
When I assign it to a label, it shows up fine, as the boxes are not visible.
 
J

Jeff Conrad

Humor me here.

Create a new blank form and put this code in the Open event.

Private Sub Form_Open(Cancel As Integer)
MsgBox fOSUserName()
End Sub

What does the message box say?
Any extra spaces?

--
Jeff Conrad
Access Junkie
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
Nothing is missing and it does find MMILLER, but has over 200 spaces after it.
The additional spaces will not allow it to be assigned to a field.

This code does match the user name with a string value tho, even given the
spaces:
Select Case LCase(fOSUserName())

Case "mmiller"
cmdDeveloperClose.Visible = True
cmdStarsUpdate.Visible = True
lblHello.Visible = True
lblHello.Caption = lblHello.Caption & " Michael"

--
MichaelM


Jeff Conrad said:
Sounds like maybe a References problem. See:

http://home.bendbroadband.com/conradsystems/accessjunkie/references.html

Post back with results.
--
Jeff Conrad
Access Junkie
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
I have a function called fOSUserName, that I found, to get the login name of
the person, when they open my main form.

[code snipped]
Problem is that it's returning mmiller followed by a bunch of square boxes.
The Left$ (in the code) and a try of Trim and a LTrim(RTrim()) is not fixing
it. When I try to assign it to a field in a log table, it always says that
my data is too big for the field.
When I assign it to a label, it shows up fine, as the boxes are not visible.
 
M

Michael Miller

Ok, made the form and you cannot see the spaces in my username in a msgbox,
so I made it with a strUserName variable, and when I step thru the code -I
can- see the square boxes after my username, just as in my real form.
--
MichaelM


Jeff Conrad said:
Humor me here.

Create a new blank form and put this code in the Open event.

Private Sub Form_Open(Cancel As Integer)
MsgBox fOSUserName()
End Sub

What does the message box say?
Any extra spaces?

--
Jeff Conrad
Access Junkie
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
Nothing is missing and it does find MMILLER, but has over 200 spaces after it.
The additional spaces will not allow it to be assigned to a field.

This code does match the user name with a string value tho, even given the
spaces:
Select Case LCase(fOSUserName())

Case "mmiller"
cmdDeveloperClose.Visible = True
cmdStarsUpdate.Visible = True
lblHello.Visible = True
lblHello.Caption = lblHello.Caption & " Michael"

--
MichaelM


Jeff Conrad said:
Sounds like maybe a References problem. See:

http://home.bendbroadband.com/conradsystems/accessjunkie/references.html

Post back with results.
--
Jeff Conrad
Access Junkie
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:

I have a function called fOSUserName, that I found, to get the login name of
the person, when they open my main form.

[code snipped]

Problem is that it's returning mmiller followed by a bunch of square boxes.
The Left$ (in the code) and a try of Trim and a LTrim(RTrim()) is not fixing
it. When I try to assign it to a field in a log table, it always says that
my data is too big for the field.
When I assign it to a label, it shows up fine, as the boxes are not visible.
 
M

Michael Miller

I tried to post the code:
Option Compare Database
Private Declare Function apiGetUserName Lib "C:\WINNT\system32\advapi32.dll"
Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Private Sub Form_Open(Cancel As Integer)
Dim strUserName As String
strUserName = fOSUserName()
MsgBox strUserName

End Sub
and got an error in my reply to you and the reply has no code.
Tryng the code, above, again.
--
MichaelM


Jeff Conrad said:
Humor me here.

Create a new blank form and put this code in the Open event.

Private Sub Form_Open(Cancel As Integer)
MsgBox fOSUserName()
End Sub

What does the message box say?
Any extra spaces?

--
Jeff Conrad
Access Junkie
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
Nothing is missing and it does find MMILLER, but has over 200 spaces after it.
The additional spaces will not allow it to be assigned to a field.

This code does match the user name with a string value tho, even given the
spaces:
Select Case LCase(fOSUserName())

Case "mmiller"
cmdDeveloperClose.Visible = True
cmdStarsUpdate.Visible = True
lblHello.Visible = True
lblHello.Caption = lblHello.Caption & " Michael"

--
MichaelM


Jeff Conrad said:
Sounds like maybe a References problem. See:

http://home.bendbroadband.com/conradsystems/accessjunkie/references.html

Post back with results.
--
Jeff Conrad
Access Junkie
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:

I have a function called fOSUserName, that I found, to get the login name of
the person, when they open my main form.

[code snipped]

Problem is that it's returning mmiller followed by a bunch of square boxes.
The Left$ (in the code) and a try of Trim and a LTrim(RTrim()) is not fixing
it. When I try to assign it to a field in a log table, it always says that
my data is too big for the field.
When I assign it to a label, it shows up fine, as the boxes are not visible.
 
J

Jeff Conrad

Most peculiar.

Humor me some more.

1. What happens on a different machine with a different user name?
Same problem or no problems?

2. Create a completely new blank database.
Copy and paste the code from here (not the code you have)
in to a new standard module (not a form's class module):

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

Create a new test form with text box that has a Default Value
of
=fOSUserName()

Does it display properly?

Create a new table called Table1 with these fields:
IDField Autonumber (PrimaryKey)
MyUserName Text
SomeOtherField Text

Save the table.
Open the form in Design View and set the Record Source of the
form to Table1. Set the Control Source of your text box to
MyUserName so it is bound. Bring down the SomeOtherField
from the Field List so you have two text boxes now.

Save the form.

Now open the form in regular mode.
Does the User Name display correctly?

Enter something into the other text box to dirty the record.
Does the User Name get saved correctly to the table now
or does it still have all the extra stuff?

--
Jeff Conrad
Access Junkie
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
 
M

Michael Miller

Hi Jeff,

First, THANK YOU for sticking with me. It means a lot (as I need this func
to work) and is educational at the same time. BTW, after these tests you're
giving me, do I get MS Certified? :eek:)

Ok, I got everything set up on my orig machine and on my VS.NET machine.

1. regarding getting fresh code from the web, I did that (that's where I
got my orig code) and defaulted the field and it does come up with my Name,
and I was not able to 'arrow' past my name, inferring that it is trimmed ok
(but not sure it could be 'assigned' to a field).

2. In the table setup and adding the bound SomeOtherField field to the
form, plus binding the orig MyUserName field, defaulted to fOSUsername(), to
the table.
The orig field, now bound to the MyUserName field, does not show my network
name anymore. Is that what you intended, using the username field as
defaulted -and- bound to the table field? Oops! Just got the default to
show up. Had to move to record 2 and made a manual entry to someotherfield.
Now my username does show up, cannot arrow to the right, changed the fld size
to 10 chars and tried again (cuz the square boxes/extra spaces would not fit
in 10 chars) and it still worked.

I will do a new test on my orig machine and get back to you againe.
What will happen in your scenario, if I tried to assign the func to a strvar
and then assign it to the field, in code? That's the problem with my logging
of users.

Thanks.
 
M

Michael Miller

Well, I went back to the main machine, and it will not assign the field as is.
Also, a Watch, of the variable, does show the boxes after that value.

However, I think I have a workaround. I went into my real target table, and
changed the fld size to 255. It assigned the value, and I cannot 'arrow'
past the end of my name. I will just have to verify that with a query
linking my name, to a static list with my name in it, to see if it matches up.

Also, as I may have mentioned, my routine of getting the username into a
Case stmt, and displaying 'Michael' if the username is mmiller, does work
under the old scheme. So, in that case the extra box chars are ignored. Go
figure.

I think we can close this, as long as the workaround works, but if you can
find out why (if you view the value of strusername, do you see the boxes as I
do?) this happens and how to fix it, I would still like to know, if you
remember me.
 
J

Jeff Conrad

in message:
First, THANK YOU for sticking with me. It means a lot (as I need this func
to work) and is educational at the same time. BTW, after these tests you're
giving me, do I get MS Certified? :eek:)

You're welcome, it is no trouble.
MD Certified? Probably not.
But you will be an Access Junkie.
;-)
1. regarding getting fresh code from the web, I did that (that's where I
got my orig code) and defaulted the field and it does come up with my Name,
and I was not able to 'arrow' past my name, inferring that it is trimmed ok
(but not sure it could be 'assigned' to a field).
Good.

2. In the table setup and adding the bound SomeOtherField field to the
form, plus binding the orig MyUserName field, defaulted to fOSUsername(), to
the table.
The orig field, now bound to the MyUserName field, does not show my network
name anymore. Is that what you intended, using the username field as
defaulted -and- bound to the table field? Oops! Just got the default to
show up. Had to move to record 2 and made a manual entry to someotherfield.
Now my username does show up, cannot arrow to the right, changed the fld size
to 10 chars and tried again (cuz the square boxes/extra spaces would not fit
in 10 chars) and it still worked.

I'm still a little confused here.
You are performing this test on a completely *different* blank database, correct?
When I say blank, I mean just our one test table and form with the specifics I
provided, correct?

My test scenario previously provided should show the User name *text box*
on the form will automatically show the Network User Name. As you dirty
the record the user name will get saved back to the table since we have bound
it to the table field.

From your description it almost sounds like it did not work and then it did.
Which is it?
I will do a new test on my orig machine and get back to you again.
What will happen in your scenario, if I tried to assign the func to a strvar
and then assign it to the field, in code? That's the problem with my logging
of users.

If you assign it to a string variable and then push the value in, that should
work as well.
Well, I went back to the main machine, and it will not assign the field as is.
Also, a Watch, of the variable, does show the boxes after that value.

You are really going to have to provide more specifics on exactly
the setup you have. Table field properties, form code, module code, etc.

If you import just that table, form and module code into a new database
container does it still not work? You may need to bring in additional elements
if necessary, but I'm seeing if you can reproduce this problem without all
the other database elements around.
However, I think I have a workaround. I went into my real target table, and
changed the fld size to 255. It assigned the value, and I cannot 'arrow'
past the end of my name. I will just have to verify that with a query
linking my name, to a static list with my name in it, to see if it matches up.

I don't like this idea and I do believe it should be necessary.
Also, as I may have mentioned, my routine of getting the username into a
Case stmt, and displaying 'Michael' if the username is mmiller, does work
under the old scheme. So, in that case the extra box chars are ignored. Go
figure.

I don't like this idea either.
I think we can close this, as long as the workaround works, but if you can
find out why (if you view the value of strusername, do you see the boxes as I
do?) this happens and how to fix it, I would still like to know, if you
remember me.

This code *should* work just fine. It has been time tested for a long time.
The two possibilities I see are:
1. There is something in your current setup that is interfering.
2. There is possibly some corruption in your database.

I'm really starting to think option two may be a distinct possibility.
What happens if you import everything into a new database container.
Still the same problem?
 
M

Michael Miller

sorry for the confuzion.
From your description it almost sounds like it did not work and then it did.
Which is it?

the tests were done on a different machine, and a blank form and new table.
not sure about dirtying record 1, i thought i did and the username was not
presented nor in the table. but when i created record two, then it worked.
and now, i -just- went back in and deleted both records and loaded the form
and it -showed- my name without having to dirty the other field, and stored
it when I typed in text in someotherfield. So. all that works
If you assign it to a string variable and then push the value in, that should
work as well.

this was done in my main machine, since i had all the code set up, and that
does not work and a Watch shows the var has the boxes in it. When I get
time, i can recreate it in the blank database, per all your other thoughts.

I agree, it shouldn't be happening. I will have to try to follow up with
you at some future point
 
J

Jeff Conrad

in message:
sorry for the confuzion.


the tests were done on a different machine, and a blank form and new table.
not sure about dirtying record 1, i thought i did and the username was not
presented nor in the table. but when i created record two, then it worked.
and now, i -just- went back in and deleted both records and loaded the form
and it -showed- my name without having to dirty the other field, and stored
it when I typed in text in someotherfield. So. all that works


this was done in my main machine, since i had all the code set up, and that
does not work and a Watch shows the var has the boxes in it. When I get
time, i can recreate it in the blank database, per all your other thoughts.

I agree, it shouldn't be happening. I will have to try to follow up with
you at some future point

Ok let me know what you find out.
This is most peculiar and should not be happening.
 
J

John Griffiths

Michael Miller said:
I have a function called fOSUserName, that I found, to get the login name of
the person, when they open my main form.

code:
Private Declare Function apiGetUserName Lib "C:\WINNT\system32\advapi32.dll"
Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function fOSUserName() As String
'returns the network login name

Dim lngLen As Long
Dim lngX As Long
Dim strUserName As String

strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen - 1)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen)

=========================================
I was expecting ->
fOSUserName = Left$(strUserName, lngX)
does this work ?

All flames to ->
Regards John
=========================================
 
D

Douglas J. Steele

Check http://msdn.microsoft.com/library/en-us/sysinfo/base/getusername.asp

Return Values
If the function succeeds, the return value is a nonzero value, and the
variable pointed to by nSize contains the number of TCHARs copied to the
buffer specified by lpBuffer, including the terminating null character.

If the function fails, the return value is zero. To get extended error
information, call GetLastError.

I have no idea, though, why it's not working for Michael: I use that API
call all the time.
 
J

John Griffiths

Douglas J. Steele said:
Check http://msdn.microsoft.com/library/en-us/sysinfo/base/getusername.asp

Return Values
If the function succeeds, the return value is a nonzero value, and the
variable pointed to by nSize contains the number of TCHARs copied to the
buffer specified by lpBuffer, including the terminating null character.

If the function fails, the return value is zero. To get extended error
information, call GetLastError.

I have no idea, though, why it's not working for Michael: I use that API
call all the time.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)




=========================================

Hi Douglas

Seeing on the above line "lngLen - 1"
I assumed the parameter was IN
as it was calculated and any return value would be lost.

How about

Public Function fOSUserName() As String
'returns the network login name

Dim lngLen As Long
Dim lngX As Long
Dim strUserName As String

lngLen = 255
strUserName = Space(lngLen)
lngX = apiGetUserName(strUserName, lngLen)

If (lngX > 0) Then
fOSUserName = Left(strUserName, lngLen-1)
Else
fOSUserName = vbNullString
End If

End Function
=========================================
 
D

Douglas J. Steele

The original code looks as though it's the same code as at
http://www.mvps.org/access/api/api0008.htm at "The Access Web", which has
always worked, without any changes, for me.

Michael: The only thing I can suggest is determining what those square boxes
are. Try something like:

Dim intLoop As Integer
Dim strUser As String

strUser = fOSUserName()
For intLoop = 1 to Len(strUser)
Debug.Print intLoop & ": " & Mid(strUser, intLoop, 1) & " (" &
Asc(Mid(strUser, intLoop, 1)) & ")"
Next intLoop

Check the debug window: you should see the user id, one character per line,
probably followed by additional lines after the user id is complete. In my
case, I get:

1: D (68)
2: J (74)
3: S (83)
4: t (116)
5: e (101)
6: e (101)
7: l (108)
8: e (101)

I'm hoping you'll see something like:

1: D (68)
2: J (74)
3: S (83)
4: t (116)
5: e (101)
6: e (101)
7: l (108)
8: e (101)
9: (0)
10: (0)

If you do, then try changing the function to:

If lngx <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
If InStr(fOSUserName, Chr(0)) > 0 then
fOSUserName = Left$(strUserName, InStr(fOSUserName, Chr(0)) - 1)
End If
Else
fOSUserName = ""
End If

Replace (0) in the code above with whatever number shows up in your debug
window.
 
M

Michael Miller

Douglas and John,
just got back onto the site.
I will try both of your suggestions.

Funny thing is that I am already using fosusername in a select case
statement and say if it's "mmiller" then say "Hello Michael" in my little
label on the form.
It does see "mmiller" as the result of fosusername and puts up the correct
label text. It's almost like it ignores the alleged spaces at the end of
fosusername.
Looking at the watch window, or hovering over the variable, you can see the
boxes.
 
M

Michael Miller

fOSUserName = Left$(strUserName, lngX)
returns an "m",
incidentally, lngLen is 255 chars,
according to the locals window.
 
M

Michael Miller

Douglas,
note, my login is mmill12.
Yes, with the loop test I got the following zeros, see below.

With your alternative code, I do not understand the use of:
Replace (0) in the code above with whatever number shows up in your debug
window.

I only ran the debug till I got to line 20. It would probably go to 255 ish.
And, wouldn't the proper length depend on the user's name's length, which
are all different?

1: m (109)
2: m (109)
3: i (105)
4: l (108)
5: l (108)
6: 1 (49)
7: 2 (50)
8: (0)
9: (0)
10: (0)
11: (0)
12: (0)
13: (0)
14: (0)
15: (0)
16: (0)
17: (0)
18: (0)
19: (0)
20: (0)
 
J

John Griffiths

I don't know about you but this thread has got very confusing.

Your original code.
=====
Public Function fOSUserName() As String
'returns the network login name

Dim lngLen As Long
Dim lngX As Long
Dim strUserName As String

strUserName = String$(254, 0) ' <-- Error should be 255
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen - 1) ' <-- Error Here
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen)
Else
fOSUserName = vbNullString
End If

End Function
=====

The line
lngX = apiGetUserName(strUserName, lngLen - 1)

should read
lngX = apiGetUserName(strUserName, lngLen)

as using and expression like "lngLen - 1" means that lngLen wasn't getting
updated with
the number of characters being replaced.

so that this line
fOSUserName = Left$(strUserName, lngLen)

is the same as saying
fOSUserName = Left$(strUserName, 255)
as lngLen = 255

Which gives us your username, followed by however many Chr(0) (the square
boxes).


=========================================
' My version of your code after interjection by Douglas
'
Public Function fOSUserName() As String
'---------------------------------
'returns the network login name
'---------------------------------
Dim lngLen As Long
Dim lngX As Long
Dim strUserName As String

lngLen = 255 ' max allowed from api, from max filename length FAT32
strUserName = Space(lngLen)
lngX = apiGetUserName(strUserName, lngLen)
'---------------------------------
' lngX = api result
' lngLen = number of characters replaced including final Chr(0)

If (lngX > 0) Then
fOSUserName = Left(strUserName, lngLen-1) ' or Trim(strUserName)
Else
fOSUserName = vbNullString
End If
'---------------------------------
End Function
=========================================

Regards John
 

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