Getting User Name form Login (System enviroment variable?)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave N

    Getting User Name form Login (System enviroment variable?)

    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 "GetUserNam eA" _
    (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 SHGetPathFromID List Lib "shell32.dl l" Alias _
    "SHGetPathFromI DListA" (ByVal pidl As Long, _
    ByVal pszPath As String) As Long

    Private Declare Function SHBrowseForFold er Lib "shell32.dl l" Alias _
    "SHBrowseForFol derA" (lpBrowseInfo As BROWSEINFO) _
    As Long

    Private Const BIF_RETURNONLYF SDIRS = &H1

    Function NetworkUserName () As String

    Dim lngStringLength As Long
    Dim sString As String

    lngStringLength = Len(sString)
    sString = String$(iString Length, 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.


  • Tom Wickerath

    #2
    Re: Getting User Name form Login (System enviroment variable?)

    Hi Dave,

    Try this method instead:
    ACC2000: How to Retrieve Workgroup Information Under Win32
    Microsoft Support is here to help you with Microsoft products. Find how-to articles, videos, and training for Microsoft Copilot, Microsoft 365, Windows 11, Surface, and more.


    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" <nelsondavea@jo hndeere.com> wrote in message
    news:3fdf5356$1 @news1.dpn.deer e.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 "GetUserNam eA" _
    (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 SHGetPathFromID List Lib "shell32.dl l" Alias _
    "SHGetPathFromI DListA" (ByVal pidl As Long, _
    ByVal pszPath As String) As Long

    Private Declare Function SHBrowseForFold er Lib "shell32.dl l" Alias _
    "SHBrowseForFol derA" (lpBrowseInfo As BROWSEINFO) _
    As Long

    Private Const BIF_RETURNONLYF SDIRS = &H1

    Function NetworkUserName () As String

    Dim lngStringLength As Long
    Dim sString As String

    lngStringLength = Len(sString)
    sString = String$(iString Length, 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.



    Comment

    • Roger

      #3
      Re: Getting User Name form Login (System enviroment variable?)

      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$(lngStri ngLength, 0)

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

      End Function

      "Dave N" <nelsondavea@jo hndeere.com> wrote in message news:<3fdf5356$ 1@news1.dpn.dee re.com>...[color=blue]
      > 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 "GetUserNam eA" _
      > (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 SHGetPathFromID List Lib "shell32.dl l" Alias _
      > "SHGetPathFromI DListA" (ByVal pidl As Long, _
      > ByVal pszPath As String) As Long
      >
      > Private Declare Function SHBrowseForFold er Lib "shell32.dl l" Alias _
      > "SHBrowseForFol derA" (lpBrowseInfo As BROWSEINFO) _
      > As Long
      >
      > Private Const BIF_RETURNONLYF SDIRS = &H1
      >
      > Function NetworkUserName () As String
      >
      > Dim lngStringLength As Long
      > Dim sString As String
      >
      > lngStringLength = Len(sString)
      > sString = String$(iString Length, 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.[/color]

      Comment

      • Terry Kreft

        #4
        Re: Getting User Name form Login (System enviroment variable?)


        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




        Terry



        "Dave N" <nelsondavea@jo hndeere.com> wrote in message
        news:3fdf5356$1 @news1.dpn.deer e.com...[color=blue]
        > 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[/color]
        in[color=blue]
        > 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[/color]
        part[color=blue]
        > but can not get the User Login ID.
        >
        > I have triedthe following but always come with no one loged in and of[/color]
        course[color=blue]
        > I am logged into my computer.
        >
        > Option Compare Database
        > Declare Function wu_GetUserName Lib "advapi32" Alias "GetUserNam eA" _
        > (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 SHGetPathFromID List Lib "shell32.dl l" Alias _
        > "SHGetPathFromI DListA" (ByVal pidl As Long, _
        > ByVal pszPath As String) As Long
        >
        > Private Declare Function SHBrowseForFold er Lib "shell32.dl l" Alias _
        > "SHBrowseForFol derA" (lpBrowseInfo As BROWSEINFO) _
        > As Long
        >
        > Private Const BIF_RETURNONLYF SDIRS = &H1
        >
        > Function NetworkUserName () As String
        >
        > Dim lngStringLength As Long
        > Dim sString As String
        >
        > lngStringLength = Len(sString)
        > sString = String$(iString Length, 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.
        >
        >[/color]


        Comment

        • Wayman Bell

          #5
          Re: Getting User Name form Login (System enviroment variable?)

          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("Usern ame"). My problem was solved.

          Wayman

          "Dave N" <nelsondavea@jo hndeere.com> wrote in message
          news:3fdf5356$1 @news1.dpn.deer e.com...[color=blue]
          > 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[/color]
          in[color=blue]
          > 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[/color]
          part[color=blue]
          > but can not get the User Login ID.
          >
          > I have triedthe following but always come with no one loged in and of[/color]
          course[color=blue]
          > I am logged into my computer.
          >
          > Option Compare Database
          > Declare Function wu_GetUserName Lib "advapi32" Alias "GetUserNam eA" _
          > (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 SHGetPathFromID List Lib "shell32.dl l" Alias _
          > "SHGetPathFromI DListA" (ByVal pidl As Long, _
          > ByVal pszPath As String) As Long
          >
          > Private Declare Function SHBrowseForFold er Lib "shell32.dl l" Alias _
          > "SHBrowseForFol derA" (lpBrowseInfo As BROWSEINFO) _
          > As Long
          >
          > Private Const BIF_RETURNONLYF SDIRS = &H1
          >
          > Function NetworkUserName () As String
          >
          > Dim lngStringLength As Long
          > Dim sString As String
          >
          > lngStringLength = Len(sString)
          > sString = String$(iString Length, 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.
          >
          >[/color]


          Comment

          • Tony Toews

            #6
            Re: Getting User Name form Login (System enviroment variable?)

            "Wayman Bell" <waymanbell@com cast.net> wrote:
            [color=blue]
            >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("User name"). My problem was solved.[/color]

            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

            Comment

            Working...