Automatically determine user ID (1 Viewer)

Jupie23

Registered User.
Local time
Today, 13:06
Joined
Nov 9, 2017
Messages
86
I am working on a database that has about 15 users. Someone else added to it so that it will automatically detect the user ID so they don't have to select their name.

Their name displays on the top of the main form where there are additional buttons to open other forms. The code is working to identify which user it is, but when they click the button to open the other forms, it opens the form and then switches right back to the main form. This seems to only happen the first time they open a form, but if they close out of the database and go back in, it does it again. Clicking the button a second time will result in it staying on the 2nd form.

There is a frmLogin that is based on a query with the employee name and user ID. There is a autoexec macro that opens frmLogin as hidden. And there is a module.

The frmLogin has an OnOpen event with the following:

Private Sub Form_Open(Cancel As Integer)
'Populate the LoginName textbox with the CurrentUserName
Me.LoginName = fOSUserName
Me.Requery

Dim WAIT As Double
WAIT = Timer
While Timer < WAIT + 3
DoEvents 'do nothing
Wend
DoCmd.OpenForm "frmMain", acNormal, , , acFormAdd, acWindowNormal
End Sub


The module has this:

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
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)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function

Can you help me find what is causing it to switch back to the main form? I am assuming it's this line in the OnOpen event, but not sure how to correct it: DoCmd.OpenForm "frmMain", acNormal, , , acFormAdd, acWindowNormal

This is my first time posting code, so I hope I have done it correctly. Thank you!
 

Ranman256

Well-known member
Local time
Today, 14:06
Joined
Apr 9, 2015
Messages
4,339
i have a global variable, or a textbox on the main menu that loads when the app opens:

gvUserID = Environ("Username")

user name get get from:
Code:
Public Function GetUserName()
Dim vName, vUserName, vUserDomain
Dim i As Integer

    Set WSHnet = CreateObject("WScript.Network")
    vUserName = WSHnet.UserName
    vUserDomain = WSHnet.UserDomain
    Set objUser = GetObject("WinNT://" & vUserDomain & "/" & vUserName & ",user")
    
    i = InStr(objUser.FullName, "(")
    If i = 0 Then
       vName = objUser.FullName
    Else
       vName = Left(objUser.FullName, i - 1)
    End If
    
    GetUserName= vName
End Function

from there I can look them up in tUsers table to get their admin rights ,if needed.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 11:06
Joined
Aug 30, 2003
Messages
36,124
Is there code in the open/load events of the other forms being opened (the ones that open and then close again)?
 

Jupie23

Registered User.
Local time
Today, 13:06
Joined
Nov 9, 2017
Messages
86
The only thing in Form_Load is:

Private Sub Form_Load()
Me.Processor_SQ_Comment.Enabled = False
Me.Frame115 = Null
Me.Frame131 = Null
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 11:06
Joined
Aug 30, 2003
Messages
36,124
Curious, nothing there would cause the form to close again. Something triggering that login form code would.
 

Jupie23

Registered User.
Local time
Today, 13:06
Joined
Nov 9, 2017
Messages
86
Actually, it's not closing the second form, it is just switching focus back to the first form. The tab for the 2nd form is still open.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 11:06
Joined
Aug 30, 2003
Messages
36,124
Can you attach the db here to experiment on? I can't recreate the problem.
 

Jupie23

Registered User.
Local time
Today, 13:06
Joined
Nov 9, 2017
Messages
86
Here is a very stripped down version, but it still appears to do it, although not every time. I noticed it also does it if you open a table. If it doesn't do it, try closing out and opening again. Seems like it only does it the first time.
 

Attachments

  • Database11.accdb
    564 KB · Views: 112

pbaldy

Wino Moderator
Staff member
Local time
Today, 11:06
Joined
Aug 30, 2003
Messages
36,124
I changed the timer interval on the main form to 0 and deleted this code, which seems to work:

Dim WAIT As Double
WAIT = Timer
While Timer < WAIT + 3
DoEvents 'do nothing
Wend
 

Users who are viewing this thread

Top Bottom