2 of the 3 works

Copper Contributor

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 suggestion, Please?

Forms![frmLogbookEntry].[LogData] = [UserName] & " On site,and Logged In ."
Forms![frmLogbookEntry].[LogType] = "logIn"
Forms![frmLogbookEntry].[OfficerName] = Forms![frmLogin].[UserName]

Francisco

9 Replies

@Francisco_Cuartas 

 

Where are these lines of code running? How are they triggered?


Is the form called "frmLogin" actually open when this line runs? (It must be open to work.)
Does it have a valid value in it when the line of code runs? 

Is Forms![frmLogin].[UserName] a bound control? 

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

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.

Private 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.
I'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?
Hello George, The code runs from the Private Sub Command1_Click(), and it is on the frmLogin

@Francisco_Cuartas 

 

This line closes the form: 

 

DoCmd.Close acForm, "frmLogin", acSaveNo 

 

Why try to do that at that point, given that you are still running additional code, including an attempt to read a value from a control on that same form?

 

One of the most effective trouble-shooting methods is to put a break point in the code on the first executable line. Then step through the procedure, one line at a time, checking the logic and values to be sure they are what you think they are.

At this point, the only way I could do more analysis would be to have a copy of the actual accdb (with sample data, not real data). Can you provide that?

Hello George,
I got it working, thank you for your help.
Congratulations on solving the problem.
Continued success with the project.