Getting the logged in users name and appending to a path

Posted on 2003-03-19
Medium Priority
Last Modified: 2012-05-04
Hello!  I am trying to write a progeam, and have a roadblock that I can't seem to figure out.  Please be kind, this may be really basic, but this is actually my first "real" program that does something useful, and I've been getting code snippets off of the net and cobbling them together, along with my own stuff.

My Problem:  I need PathToSource to equal "M:\Documents and Settings\" + GetUser + "\ntuser.dat".  Getuser is the currently logged in user.  I am getting only the first string and GetUser in my msgbox, leaving out the "\ntuser.dat".  Can someone figure out why?  BTW, the getUser routine below is by Matthew Grove, I found it at:


Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long
Private Sub Form_Load()
Dim sBuffer As String
Dim lSize As Long
Dim result As Integer
Dim PathToSource As String

' Parameters for the dll declaration are _
sBuffer = Space$(255)
lSize = Len(sBuffer)
Call GetUserName(sBuffer, lSize) ' Call the declared dll function
If lSize > 0 Then
Getuser = Left$(sBuffer, lSize) ' Remove empty spaces
Getuser = vbNullString

End If
PathToSource = "M:\Documents and Settings\" + Getuser + "\ntuser.dat"
result = MsgBox(PathToSource, vbOKOnly, "Test")
End Sub
Question by:BobbyH
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 43

Accepted Solution

TimCottee earned 1000 total points
ID: 8167359
Hi BobbyH,

> If lSize > 0 Then
> Getuser = Left$(sBuffer, lSize) ' Remove empty spaces
> Else

Change this to:

If lSize > 0 Then
  GetUser = Left(sBuffer,Instr(sBuffer,Chr(0)) - 1)

There are NULL characters at the end of the returned sBuffer, these are not removed by the original statement and will cause your final result to appear tuncated.


Brainbench MVP for Visual Basic

Experts-Exchange Advisory Board Member

Author Comment

ID: 8167417
Beautiful!  Thanks so Much!  Worked like a charm, and I would never have figured that out.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question