Results 1 to 7 of 7
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038

    Module errors when *not* on network

    Experts:

    I am using a module (see below) to "get the username". This module/function works fine when I'm on my work computer (on a network).



    However, if I work from home (off network), I have to comment out the two lines starting with "Private Declare Function" and "lngX = apiGetUserName" (as shown below).

    I use Office365 on my home computer and Office2010 on my work computer. How should the module be modified so that I don't constantly have to comment/un-comment the two lines?

    Thank you,
    EEH

    Code:
    Option Compare Database
    'Private Declare Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long     '*** Errors on home computer; works fine when on company network
    
    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)           '*** Errors on home computer; works fine when on company network
            
        If (lngX > 0) Then
           fOSUserName = Left$(strUserName, lngLen - 1)
        Else
           fOSUserName = vbNullString
        End If
            
    End Function

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,463
    Maybe trying using the Environ function to get the username? strUserName = Environ("Username") or maybe its strUserName = Environ(3)

    Or use the Environ to get the PC name, if it is home computer skip those lines.

    Or you could maybe trap the error and if it is the one you describe, resume next or skip to the line you need to continue.

  3. #3
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Bulzie -- thanks for the feedback. Working from home right now so I can't fully test whether or not the function works. What does the full module look like given your recommendation?

  4. #4
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,463
    It is a built in function, just use strUserName = Environ("Username") or I think you can also use strUserName = Environ(3) , look it up online for other options.

  5. #5
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Thanks... did some additional research. Ultimately, I changed the lines as follows and I don't see any errors (on home PC) any longer.

    Code:
    Private Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As LongPtr) As Long
    lngX = apiGetUserName(strUserName, CInt(lngLen))

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    sounds like your home machine is 64bit access, but your offfice machine is 32 bit access

    your revised code will work in both 64 and 32bit access - so recommend your review all your api calls and modify accordingly

  7. #7
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Ajax - thank you... will do.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Running a Module - Keep getting Errors
    By griztkojr in forum Modules
    Replies: 20
    Last Post: 04-20-2018, 01:35 PM
  2. Replies: 4
    Last Post: 11-28-2014, 01:43 AM
  3. Replies: 4
    Last Post: 06-21-2014, 05:43 AM
  4. class module vs regular module
    By Madmax in forum Modules
    Replies: 1
    Last Post: 05-01-2012, 03:44 PM
  5. Replies: 4
    Last Post: 05-16-2011, 04:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums