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!
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!