-1

I am running Windows 7 Professional. I have an MS Access frontend to an MS Access backend. The form that opens at the start of opening the frontend causes the app to crash.

Here is the code:

Private Sub Form_Open(Cancel As Integer)
Dim strMyDir As String
Dim intPos As Integer
Dim rst As dao.Recordset
Dim strSQL As String
Dim rstWhatsNew As dao.Recordset

DoCmd.ShowToolbar "Database", acToolbarNo
DoCmd.ShowToolbar "Toolbox", acToolbarNo
DoCmd.ShowToolbar "Form View", acToolbarNo

If Application.GetOption("ShowWindowsInTaskbar") = -1 Then
    Application.SetOption "ShowWindowsInTaskbar", 0
End If


If DLookup("Locked", "luLockOut") <> 0 Then
    MsgBox "Database is being worked on.  Please try back in a couple minutes.", vbInformation, " "
    DoCmd.Quit
Else
    strSQL = "Select * From tblLastLogins Where UserName = '" & GetCurrentUserName() & "'"

This is where I have traced the error to: GetCurrentUserName()

    Set rst = CurrentDb.OpenRecordset(strSQL)
        With rst
            If Not .EOF Then
                .Edit
                strSQL = "Select WhatsNewID From tblWhatsNew Where DateAdded >= #" & !LastLoginDate & "#"
                Set rstWhatsNew = CurrentDb.OpenRecordset(strSQL)
                    While Not rstWhatsNew.EOF
                        DoCmd.OpenForm "frmWhatsNew", , , , , acDialog, rstWhatsNew!WhatsNewID
                        rstWhatsNew.MoveNext
                    Wend
                    rstWhatsNew.Close
                Set rstWhatsNew = Nothing
            Else
                .AddNew
                !UserName = GetCurrentUserName()
            End If
            !LastLoginDate = Now()
            !IsLoggedIn = -1
            Me.txtLastLoginID = !LastLoginID
            .Update
            .Close
        End With
    Set rst = Nothing
    DoCmd.OpenForm "frmPrivacyNote"
    Debug.Print Me.txtLastLoginID
End If

I need to track the username, so if GetCurrentUserName() is outdated, what is the current syntax?

Further follow up. I could not find data on Bing for GetCurrentUserName(), for good reason. It is a function within a MOD, so I need to figure out why the MOD is not getting called, or is malfunctioning.

After further delving, I found a Referenced MDB that has another function created by one of our users that is the cause of this error.

This is currently not an issue of MS Access working incorrectly. It is an issue with user created code.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • Games have mods, maybe you mean an Add-On? – Andre Oct 27 '16 at 22:29
  • No. MOD is short for MODULE, in MS Access. These are codeholders that are not directly associated with the VBA code of a form. – Sensii Miller Oct 27 '16 at 22:32
  • Ah, that's not a very common abbreviation (I think). If you want to analyze that, please add the code to your question (in the comment it's hard to read). There must be a declaration of the API function `GetUserName()` together with it. But actually you can remove all that and use the function posted in the answer - it is no longer necessary to use the API function. – Andre Oct 27 '16 at 22:37
  • I tracked further. I found a DLL that needs to be registered. ADVAPI32.DLL. Unfortunately when I try to register it, I get the following error: "Can't add reference to the specific file". Googling that error, I found many have encountered it, but nothing directly related to my problem (the solutions suggested did not work). – Sensii Miller Oct 28 '16 at 17:13
  • Have you tried `CreateObject("WScript.Network").UserName` ? I have the feeling you are trying to solve a problem that doesn't really exist anymore, since there is a better alternative. – Andre Oct 28 '16 at 17:15
  • I have the same inkling. – Sensii Miller Oct 28 '16 at 17:19
  • CreateObject("WScript.Network").UserName - - Worked! I googled it and found code to get the user name...well, when I ran it on my Windows 10 machine, it crapped out, but that might be an install issue. – Sensii Miller Oct 28 '16 at 17:57
  • Well, it worked on my desktop PC running Windows 7 Pro. However, it failed on my Windows 10 tablet. My current solution is to default the user to "Admin" until I get the forms changed that I need changed, then reset the user code back to the original and let whomever is in charge change that code when it is an issue. – Sensii Miller Nov 01 '16 at 16:42

3 Answers3

2

GetCurrentUserName() is not defined by Access, so you should have looked at (and posted) its code.

If you are looking for the Windows user name, use this function:

Public Function GetUserName() As String
    ' GetUserName = Environ("USERNAME")
    ' Environ("USERNAME") is easily spoofed, see comment by HansUp
    GetUserName = CreateObject("WScript.Network").UserName
End Function

Source

Community
  • 1
  • 1
Andre
  • 26,751
  • 7
  • 36
  • 80
  • Function GetCurrentUserName() As String Dim strName As String Dim lngCharacters As Long Dim lngReturn As Long ' strName = Space(255) lngCharacters = 255 lngReturn = GetUserName(strName, lngCharacters - 1) If lngReturn = 0 Then GetCurrentUserName = "Unable to retrieve name." Else GetCurrentUserName = OnlyLetters(strName) End If End Function – Sensii Miller Oct 27 '16 at 22:30
0

The link below would suggest that

CurrentUser()

is the function

CurrentUser()

  • Curiously, CurrentUser() returned the value, "Admin". That might be my role, but it is not my user name. – Sensii Miller Oct 27 '16 at 22:16
  • 1
    You always have an username in Access, which is returned by `CurrentUser()`. If it isn't specified on the command-line with `/User xyz`, it is always `Admin`. This is (was) used for user-level security in Access. – Andre Oct 27 '16 at 22:32
0

Andre, thank you very much for the insight! I found this link: http://www.codeproject.com/Articles/1422/Getting-User-Information-Using-WSH-and-VBScript

Dim objNet
On Error Resume Next

'In case we fail to create object then display our custom error

Set objNet = CreateObject("WScript.NetWork")
If Err.Number <> 0 Then                 'If error occured then display notice
    MsgBox "Don't be Shy." & vbCRLF &_
               "Do not press ""No"" If your browser warns you."
    Document.Location = "UserInfo.html"
                                        'Place the Name of the document.
                                    'It will display again
End If

Dim strInfo
strInfo = "User Name is     " & objNet.UserName & vbCrLf & _
          "Computer Name is " & objNet.ComputerName & vbCrLf & _
          "Domain Name is   " & objNet.UserDomain
MsgBox strInfo

Set objNet = Nothing                    'Destroy the Object to free the Memory