save button code

venu_resoju

Registered User.
Local time
Tomorrow, 01:04
Joined
Dec 29, 2010
Messages
129
I have a form named "frmAttendance" which contain "EmpID", "EmpName", "EPFNo" and "AttendanceDate" and am using the below code to save it to "tblAttendance". It is working fine, but I want to add "Attendance Status" Option group(like"Present" & "Absent') and "OT Hrs" text box to the form. How can I include them in the below code to save to the 'tblAttendance" table.
and also DoCmd.GoToRecord , "", acNewRec is not working in the below code where is the problem, I didn;t understand...

please anybody help me to solve this...


Private Sub cmdSave_Click()
On Error GoTo cmdSave_Click_Err

On Error Resume Next

Dim db As Database
Dim rst As Recordset

Set db = CurrentDb

Set rst = db.OpenRecordset("Customers")
rst.AddNew

rst!AttendanceDate = Me!txtAtDate
rst!EmpID = Me!Combo10
rst!EmpName = Me!txtEmpName
rst!EPFNo = Me!txtEPFNo

rst.Update
rst.Close

DoCmd.GoToRecord , "", acNewRec

cmdSave_Click_Exit:
Exit Sub

cmdSave_Click_Err:
MsgBox Error$
Resume cmdSave_Click_Exit

End Sub
 
Why are you using a DAO recordset for this? Why not use a bound form (if you don't already have one) and then in the form's Before Update event you validate your values and cancel the update if it fails validation or otherwise just let it go through. And the code for a save button there would be:

Code:
If Me.Dirty Then Me.Dirty = False
 
Yup, the recordset says unbound but the GoToRecord says bound. It seems to be a mixture of code.

I would guess the form is unbound and the GoToNewRecord was added to try to make it save all fields.

Either make it a bound form or add extra lines of code for the extra fields & controls. The format used above is:

rst!FieldName = me!ControlName

Therefore if we assume that Attendance Status field is AttendanceStatus and the control is cboAttendanceStatus (combobox as it's multiple choice) then you would add:

rst!AttendanceStatus = me!cboAttendanceStatus

And repeat the same format for the other fields / controls.
 
I still would bind the form and just let Access do the work. Why eliminate what it does for free and you end up having to code a bunch to do the same work that it already does? There are RARE instances where using an unbound form is necessary but that is the EXCEPTION to the rule.
 
Thank you "boblarson" for your quick reply and I will try as you told......

@ @ @ @ @ @


And also Thank you a lot "CBrighton". and which command have to use instead of DoCmd.GoToRecord , "", acNewRec

"cboAttendanceStatus" type is already prepared and it's working. but I have to create like option group buttons...please give any idea....
 
Assuming you follow Bob's advice and make it a bound form something like "If Me.Dirty Then Me.Dirty = False" will save the record.

:edit:

There are various ways to save a record, this is just one way.
 
Thank you "CBrighton". I have bounded the form and I made it. but I have another problem.
"Attendance" table contain these fields:
1) AttendanceID
2) EmpID
3) EmpName
4) AttendanceDate [Current Date]
5) AttendanceStatus [storage like "Present" or "Ansent"]

"Salary" Form which contain:
1) EmpName
2) Month [Combo box]
3) Year [Combo box]
4) No.of Working Days
5) No of Days worked

I am trying to code this. I want to auto fill the fields which are "No of working days" and "No of Days worked" when the completion of the selection of "Month" and "Year" combo boxes.
how can I write the code for this...
please give me some idea...and help me to write the code..

thanks in advance...
 
Autofill from where?

I assume the working days control will just be a sum of weekdays in the month, but where would no of days worked be populated from? It sounds like something to be manually input to me.
 
it is a calculation sum of selected period[month and year] of "Attendance Status" field from "Attendance" table
 
A DCount with suitable criteria (employee ID, date, status) should do what you need for that one.

Something like:

Code:
DCount("AttendanceStatus","Attendance","EMPID = " & Control1 & " AND AttendanceStatus = 'Present' AND AttendanceDate between DateSerial(Control2,Control3,1) AND dateserial(Control2,Control3 + 1, 1)-1")

Control1 = control on the form bound to EmpID, Control2 = combobox for year, Control3 = combobox for month.

It's rough code and will likely need tweaking before use, but you get the idea.
 
Thank you "CBrighton". I have bounded the form and I made it. but I have another problem.
"Attendance" table contain these fields:
1) AttendanceID
2) EmpID
3) EmpName
4) AttendanceDate [Current Date]
5) AttendanceStatus [storage like "Present" or "Ansent"]

"Salary" Form which contain:
1) EmpName
2) Month [Combo box]
3) Year [Combo box]
4) No.of Working Days
5) No of Days worked

I am trying to code this. I want to auto fill the fields which are "No of working days" and "No of Days worked" when the completion of the selection of "Month" and "Year" combo boxes.
how can I write the code for this...
please give me some idea...and help me to write the code..

thanks in advance...

FYI: The EmpID should be stored in the Salary table and NOT the employee name!
 
It says salary is a form rather than a table.

:edit:

I would have EmpID on the form, even if it's hidden and just used to refer to for subforms, etc.

But EmpName would be what the end user wants to see.
 
I am here with attaching my db please check and give me some guide lines...
 

Attachments

Users who are viewing this thread

Back
Top Bottom