hi experts, i have problem saving my logout entry could not be saved on database.

Password & username textbox is in the login form on form 1 and lbldate & lbltime is on form 2. Log-out click is a Menu editor on form 2. Pls help me check my code. Thanks a lot.

Private Sub Logout_Click()

Set rs = New ADODB.Recordset
rs.Open "select*from Logout_Records", cn, adOpenKeyset, adLockOptimistic
rs.AddNew

rs!Password = Form01.txtPassword.Text
rs!Username1 = Form01.txtUsername.Text
rs!Logout_Date = Form02.Label1.Caption
rs!Logout_Time = Form02.Label2.Caption

rs.Update
rs.Close
Set rs = Nothing
Unload Me
Exit Sub
End Sub

Recommended Answers

All 7 Replies

if you did not change name property of the form then
use form1 not form01
use form2 not form02

if still having problems then what error you are getting now

sir, the forms name are already correct. there is no error when I click the log-out on the menu editor and no data has save on database.

This is normally where I will start using public variables...

In a module add the following -

Public strUsername1 As String, strPassword As String, strLogout_Date As String, strLogout_Time As String ''Note, string values used, you have already formatted all dates and times etc...

In your logout form -

Private Sub Logout_Click()
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM Logout_Records", cn, adOpenKeyset, adLockOptimistic

strPassword = Form01.txtPassword.Text
strUsername1 = Form01.txtUsername.Text

''The values from form2 can be called either here IF FORM2 IS STILL OPEN... If form 2 was closed earlier on, add the values BEFORE form2 was closed as per our discussion yesterday...
strLogout_Date = Form02.Label1.Caption
strLogout_Time = Form02.Label2.Caption

rs.AddNew
rs!Password = strPassword
rs!Username1 = strUsername1
rs!Logout_Date = strLogout_Date
rs!Logout_Time = strLogout_Time
rs.Update
rs.Close
Set rs = Nothing

strUsername1 = vbNullString
strPassword = vbNullString
strLogout_Date = vbNullString
strLogout_Time = vbNullString

Unload Me
Exit Sub
End Sub

Hi Sir, I still can't connect with the codes below; Pls help me check whats wrong.

Private Sub Timer2_Timer()
Label1.Caption = Format(Date, "mm/dd/yy")
Label2.Caption = Format(Now, "hh:mm:ss")
End Sub

*in module1

Public cn As ADODB.Connection
Public rs As ADODB.Recordset
Public strUsername1 As String, strPassword As String, strLogout_Date As String, strLogout_Time As String

Sub main()

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.ConnectionString = "provider=Microsoft.jet.oledb.4.0; data source=" & (App.Path & "\automatedpayrollsystem.mdb") & "; persist security info  = false"
cn.Open
Form01.Show

End Sub


Logout click is in menu bar of form02 as menu editor.

Private Sub Logout_Click()

Set rs = New ADODB.Recordset
rs.Open "select * from Logout_Records", cn, adOpenKeyset, adLockOptimistic

strPassword = Form01.txtPassword.Text
strUsername1 = Form01.txtUsername.Text
strLogout_Date = Form02.Label1.Caption
strLogout_Time = Form02.Label2.Caption

rs.AddNew

rs!Password = strPassword
rs!Username1 = strUsername1
rs!Logout_Date = strLogout_Date
rs!Logout_Time = strLogout_Time

rs.Update
rs.Close
Set rs = Nothing

strPassword = vbNullString
strUsername1 = vbNullString
strLogout_Date = vbNullString
strLogout_Time = vbNullString

Unload Me
Exit Sub
End Sub

First, Sub Main is incorrect...

Sub main()
Set cn = New ADODB.Connection
''Set rs = New ADODB.Recordset - DO NOT USE THIS HERE, use it in your form, when done with rs, close it...
cn.ConnectionString = "provider=Microsoft.jet.oledb.4.0; data source=" & App.Path & "\automatedpayrollsystem.mdb", persist security info  = false"
cn.Open
Form01.Show
End Sub

Your Logout_click will NOT work if form 1 was closed UNLESS you gave values to Username1 and Password BEFORE FORM 1 WAS CLOSED!!!

Private Sub Logout_Click()
Set rs = New ADODB.Recordset
rs.Open "select * from Logout_Records", cn, adOpenKeyset, adLockOptimistic
strPassword = Form01.txtPassword.Text ''YOU DO NOT NEED TO DO THIS HERE. BEFORE form 1 is closed, add the values to the public )global) variables...
''In Form01 you will have before its unload event -
''strUsername1 = Form01.txtUsername.Text
''strPassword = Form01.txtPassword.Text
strLogout_Date = Form02.Label1.Caption
strLogout_Time = Form02.Label2.Caption
rs.AddNew
rs!Password = strPassword
rs!Username1 = strUsername1
rs!Logout_Date = strLogout_Date
rs!Logout_Time = strLogout_Time
rs.Update
rs.Close
Set rs = Nothing
strPassword = vbNullString
strUsername1 = vbNullString
strLogout_Date = vbNullString
strLogout_Time = vbNullString
Unload Me
Exit Sub
End Sub

And voila, all sorted...

Note that I have changed your connection entirely (cn)...

Hi Sir, thank you for this. Saving the fields above is now okay. I just deleted the Logout_Records and created a new Logout table and its working, few changes also were made;

I used these for date and time codes without using the labels.

Private Sub Logout_Click()

Set rs = New ADODB.Recordset
rs.Open "select*from LogOut", cn, adOpenKeyset, adLockOptimistic

rs.AddNew

rs!Password = Form01.txtPassword.Text
rs!Username1 = Form01.txtUsername.Text
rs!Logout_Time = Format(Now, "hh:mm:ss")
rs!Logout_Date = Format(Date, "mm/dd/yy")

rs.Update
rs.Close
MsgBox "Your Time out has been recorded!", vbInformation, "Logout"
Set rs = Nothing
Unload Me

End Sub

for now its fine but as soon as error occurs I will try your code aboe sir.

thanks a lot.

No problem. If it solved your problem, you know what to do then, thanx. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.