Access VBA - Find User name

RCBricker

Well-known Member
Joined
Feb 4, 2003
Messages
1,560
Hi all.

I would like to add a test for the user of the database. I am looking to add this to my error handling, if I am not the user then I want the error message (user-Defined) to fire, a screen shot taken, and an email sent to me with the screen shot, variables from the user form and a copy of all the files that have been selected for processing.

Once I get all that data I should be able to recreate the issue and determine the fix.

If I am the user, I simply want the error handler to fire the message (user-defined).

I need to be able to capture both Access and Excel user names if the process is different. I like to maintain my error handling for all project as much as possible.

Here is my current error handler

Code:
''=====================================================================================
'   Program     :   errHAND
'   Desc.       :   This Sub is used for error handling for the other subs.
'   Called By   :   All other subs in this module
'   Calls       :   None
'   Arguments   :   strENUM As String, strELINE As String, strEDESC As String
'   Comments    :   See in-line comments
'
'   Changes----------------------------------------------------------------------------
'
'   Date        Programmer      Change
'   ----        ----------      -------------------------------------------------------
'   UNK         RBricker        Written
'   10/15/15    RBricker        Added Comments for clarity and error handling
'
''=====================================================================================
Private Sub errHAND(strENUM As String, strELINE As String, strEDESC As String)

Const strERRORMSG As String = "The Program has generated the following error", _
        strERRORnbr As String = "Error Number: ", _
        strERRORline As String = "error occured on line: ", _
        strERRORdesc As String = "Error Description: ", _
        strERRORocc As String = "An Error has Occured!"
Dim strFNAME As String, strFPATH As String, strKILLFILE As String

'**************************************************************************************
'*****  Displays message box which describes the error which has occured and the line
'           number.
'**************************************************************************************
    MsgBox strERRORMSG & vbCrLf & vbCrLf & _
        strERRORnbr & strENUM & vbCrLf & _
        strERRORline & strELINE & vbCrLf & _
        strERRORdesc & strEDESC, _
        vbCritical, strERRORocc

'**************************************************************************************
'*****  On error direction set to goto the next line of code
'**************************************************************************************
On Error Resume Next

'**************************************************************************************
'*****  set Excel Application parameters
'**************************************************************************************
    With oEXCEL
        .ScreenUpdating = False
        .DisplayAlerts = False
        .Visible = True
    End With
    
'**************************************************************************************
'*****  sets strFNAME to oxWBRECON file name
'*****  sets strFPATH to oxWBRECON file path and include character "\"
'*****  sets strKILLFILE to strFPATH & strFNAME
'**************************************************************************************
    strFNAME = oxWBRECON.Name
    strFPATH = oxWBRECON.Path & "\"
    strKILLFILE = strFPATH & strFNAME
    
'**************************************************************************************
'*****  Closes oxWBRECON files without saving
'*****  Set oxWS variable to nothing
'*****  Set oxWBRECON variable to nothing
'*****  Quit the Excel Application
'*****  Set oEXCEL variable to nothing
'*****  Delete file using strKILLFILE to identify the appropriate file.
'*****  Set qdf variable to nothing
'*****  Set db variable to nothing
'**************************************************************************************
    oxWBRECON.Close False
    Set oxWS = Nothing
    Set oxWBRECON = Nothing
    oEXCEL.Quit
    Set oEXCEL = Nothing
        Kill strKILLFILE
    Set qdf = Nothing
    Set db = Nothing

    End
End Sub

Not really looking for a re-write as I should be able to handle writing most of this with some research. However, I am not even sure where to look for the username.

Thanks,

Rich
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You can use this to get the MS Access UserName:
Code:
Application.UserName
or this to get the windows UserName:
Code:
Environ("UserName")

Please forgive me if I read this wrong. This may not be what your looking for....
 
Upvote 0
You can use this to get the MS Access UserName:
Code:
Application.UserName
or this to get the windows UserName:
Code:
Environ("UserName")

Please forgive me if I read this wrong. This may not be what your looking for....

is that different for each MS product or can I use that line no matter what?

rich
 
Upvote 0
In case you're using Mac, Google fosUserName. I know of a very knowledgable Access person who had to stop using Environ after a network mod - it stopped working but I don't know why. I have been using fosUserName for years with no issues, but understand that you need a special declaration if you're in 64 bit environment. If so and you need that, post back.
P.S. imitation is a high form of flattery. Outright plagarism/theft is better still! :)
Things like that make me think I actually have something to contribute here.
 
Upvote 0
In case you're using Mac, Google fosUserName. I know of a very knowledgable Access person who had to stop using Environ after a network mod - it stopped working but I don't know why. I have been using fosUserName for years with no issues, but understand that you need a special declaration if you're in 64 bit environment. If so and you need that, post back.
P.S. imitation is a high form of flattery. Outright plagarism/theft is better still! :)
Things like that make me think I actually have something to contribute here.

I am not using a MAC.
 
Upvote 0
Pretty sure that Application.UserName ----> Will give you the name that is in: File > Options > General > User Name for whatever application the code module resides.

The Environ("UserName") will give you the windows login.

Or you can use this:
Code:
'http://www.mrexcel.com/forum/excel-questions/31845-function-shows-your-username.html
Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
(ByVal lpName As String, _
ByVal lpUserName As String, _
lpnLength As Long) As Long


Private Const NO_ERROR = 0
Private Const ERROR_NOT_CONNECTED = 2250&
Private Const ERROR_MORE_DATA = 234
Private Const ERROR_NO_NETWORK = 1222&
Private Const ERROR_EXTENDED_ERROR = 1208&
Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&


Sub WinUsername()
Dim strBuf As String, lngUser As Long, strUn As String
strBuf = Space$(255) '//Clear buffer
lngUser = WNetGetUser("", strBuf, 255)
If lngUser = NO_ERROR Then
strUn = Left(strBuf, InStr(strBuf, vbNullChar) - 1)
MsgBox "User Name is " & strUn
Else
MsgBox "Error :" & lngUser
End If
End Sub
 
Upvote 0
After reviewing my post, I might have given the impression that fosUserName is only for Mac machines. I meant to infer that Environ does not work with Macs.
And the application.Username is not exposed if you're in sandbox mode (probably not an issue here).
 
Upvote 0

Forum statistics

Threads
1,221,573
Messages
6,160,593
Members
451,657
Latest member
Ang24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top