Forum Discussion
Francisco_Cuartas
Oct 25, 2021Copper Contributor
2 of the 3 works
Hello all, I have these 3 lines of code, and the first 2 works, but I cannot make the 3 third ones work. I need the user name on the login form to be pasted on the frmlogbookEntry. Can anyone give a ...
Francisco_Cuartas
Copper Contributor
Hello George, this is the code that opens the logbook entry
stDocName = "frmLogbookEntry"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.GoToRecord , , acNewRec
Forms![frmLogbookEntry].[LogData] = [UserName] & " On site,and Logged In ." ==the UserName does not work, the other part does
Forms![frmLogbookEntry].[LogType] = "logIn" ==This line works
'Forms![frmLogbookEntry].[OfficerName] = Me.[UserName]== Does not work
DoCmd.Save
DoCmd.Close
stDocName = "frmLogbookEntry"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.GoToRecord , , acNewRec
Forms![frmLogbookEntry].[LogData] = [UserName] & " On site,and Logged In ." ==the UserName does not work, the other part does
Forms![frmLogbookEntry].[LogType] = "logIn" ==This line works
'Forms![frmLogbookEntry].[OfficerName] = Me.[UserName]== Does not work
DoCmd.Save
DoCmd.Close
George_Hepworth
Oct 27, 2021Silver Contributor
Please post the COMPLETE procedure, starting with the declaration as Private or Public and ending with the close, End Sub or End Function, not snippets from it.
Also, please confirm that this code runs on some event in the form named frmLogin. Which event? A command button click?
Again, it's also useful to have answers to my specific questions, including the actual value in the control called UserName on the form called [frmLogin]. Is UserName a bound control? What kind of control is it? Thank you.
PS: Maybe in this case actual screenshots of the two forms would also be helpful. Thanks again.
- Francisco_CuartasOct 27, 2021Copper ContributorPrivate Sub Command1_Click()
Dim rs As Recordset
Dim Security, UserName As String
Dim Auth As Variant
On Error GoTo Err:
Set rs = CurrentDb.OpenRecordset("tblUser", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.UserName & "'"
If IsNull(Me.TxtSiteName) Then
Me.TxtSiteName.SetFocus
MsgBox "Insert Site Name.", vbCritical, "Login"
Exit Sub
End If
If rs.NoMatch = True Then
Me.UserName.SetFocus
MsgBox "Wrong User Name", vbCritical, "Login"
Exit Sub
End If
If IsNull(Me.txtPassword) Then
Me.txtPassword.SetFocus
MsgBox "Insert Password.", vbCritical, "Login"
Exit Sub
End If
If rs!UserSecurity <> Me.txtPassword Then
MsgBox "Wrong Password, Please try again.", vbCritical, "Login"
Else
Auth = DLookup("UserManage", "tblUser", "UserName='" & Me.UserName & "'")
DoCmd.OpenForm "frmMain", acNormal, , "UserName='" & Me.UserName & "'"
DoCmd.OpenForm "frmLoggedIn", acNormal, , "UserName='" & Me.UserName & "'"
Forms!frmMain.cmdLogOut.SetFocus
If Auth = 0 Then
Forms!frmMain.cmdAddNewUser.Visible = False
Forms!frmMain.cmdUsers.Visible = False
Else
Forms!frmMain.cmdAddNewUser.Visible = True
Forms!frmMain.cmdUsers.Visible = True
End If
DoCmd.Close acForm, "frmLogin", acSaveNo
DoCmd.Close acForm, "frmBackGround", acSaveNo
stDocName = "frmLogbookEntry"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.GoToRecord , , acNewRec
Forms![frmLogbookEntry].[LogData] = [UserName] & " On site,and Logged In ."
Forms![frmLogbookEntry].[LogType] = "logIn"
'Forms![frmLogbookEntry].[OfficerName] = Me.[UserName]
DoCmd.Save
DoCmd.Close
End If
Exit Sub
The UserName it is Unbound.- George_HepworthOct 27, 2021Silver ContributorI'll have a chance to look at this more carefully later today, but it looks like you are CLOSING the form called frmLogin and opening a second form called frmLogBookEntry.
Then when you get to the commented out line (which I believe is the one you say doesn't work, you are trying to put a value into the control called "OfficerName" on the frmLogBookEntry by referencing a control called UserName on that same form. In other words, Me refers to the form on which the code runs, and that appears to be the form called frmLogBookEntry.
Is that what you expect to happen?- Francisco_CuartasOct 27, 2021Copper ContributorHello George, The code runs from the Private Sub Command1_Click(), and it is on the frmLogin