Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function Not Getting UserName? 1

Status
Not open for further replies.

quest4

Technical User
Aug 27, 2001
735
US
Good afternoon. I have a form with a command buton and three textboxes. I have a function, which is supposed to get the user's logon name from the system and set the date to today. My OnClick procedure is:
Me.CCDApprovedBy = fOSUserName()
Me.CCDDate = Date
Me.CCDComments.SetFocus
My function is:
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function fOSUserName() As String ' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If lngX <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = &quot;&quot;
End If
End Function
When I click the command button, I get an error message the the textbox is to small for the amount of data, which is supposed to be 20. I did try changing lngLen to 20, but nothing changed. I hope that someone can point out the error of my ways. Thank you in advnce to anyone helping out.
 
I would check a couple of things. You might want to put a message box at the end of the function to see just what is being returned by the fOSUserName function. Secondly, if CCDApprovedBy is bound to a database field, I would make sure that the length of the database field is large enough to hand the User Name returned value.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thank you for the response, CajunCenturion. I have double checked the field length of the CCDApprovedBy, which is bound, and it is set for 20, the same as the NT logon length. I am not quite sure how to put the message box in. I have check the function in a small dbase and it works fine there. I got the function from Bill Powers, a while back. If you could give me a hint about the message, where and what, I would appreciate it. Thank you again for the response.
 
Give this a shot:

Function fOSUserName() As String
Dim lngLen As Long, lngX As Long
Dim strUserName As String

strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If lngX <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = &quot;&quot;
End If
MsgBox fOSUserName & &quot;-&quot; & Len(fOSUserName)
End Function


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thank you for the assistance, CajunCenturion. I made the change and it printed out a message, my logon-6, which was correct. Then came the message that the field is to small for that amount of data, and to reduce the amount of data. I also tried a different approach, =Environ(&quot;Username') and I got the same results. I recheck the field size and it is still set to 20. Dumb question, why can't 6 characteders go into 20 spaces? I am confussed now. Thanks you again for the assistnace.
 
Are you sure you're dealing with the right textbox? Single Step through the code to be sure you're chasing the right problem.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thank you again CajunCenturion. That did it, I discovered the error of my ways, I sent it to the wrong textbox. It now works. Thank your for the assistance.
 
Glad you could find the problem.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top