I'm pretty close to finishing this payroll program, but i've run in to yet some more problems and i can't figure out how to fix them.
So, i have various forms representing different tables of my database (Access - the database is connected to my vb application as a data source)
Take for instance the employee table. You can add and delete records from the table as well as make changes to the fields. The add and delete buttons on my form work properly, and if i go to the 2nd record upwards, the save button works, but when i try update the first record, the changes don't stick.
Here is a code snippet. This one is from the Save button for a table called Allowances pay.
' save current record
' variables to store records
Dim row, datar2 As payrollDatabaseDataSet.Allowances_Pay_TableRow
row = PayrollDatabaseDataSet.Allowances_Pay_Table.Rows(current_row)
datar2 = PayrollDatabaseDataSet.Allowances_Pay_Table.Rows(0)
' user must confirm the edit
Dim confirm AsInteger
confirm = MsgBox("Are you sure you want to alter this record", 1 + 48, "Please Confirm")
If confirm = 1 Then
' get new values for variables
row.employee_ID = CStr(Employee_IDTextBox.Text)
row.allowances_ID = Allowances_IDTextBox.Text
row.allowances_amount = Allowances_amountTextBox.Text
Else : MsgBox("Action cancelled!")
EndIf
' update record
Allowances_Pay_TableTableAdapter.Update(row)
Allowances_Pay_TableTableAdapter.Update(PayrollDatabaseDataSet.Allowances_Pay_Table)
I put in a breakpoint just before the update section of the code and checked the values for row.employee_id, etc. The values stored in them are all correct, so i presume it must be the way i'm calling the update procedure.
There is no error message. the code runs, but the record just reverts to what it was before the changes were made. i.e. if i change employee name from Benny to Ben and click save, when i go back to that record (after the record is saved, the program displays the first record in the table) record, it is still Benny.
I noticed that this doesn't happen on all my forms (but most) so i'm going to analyze the code and see what the differences are between the code that works 100% properly and the code that doesn't save the first record
Since row is actually representing a record, shouldn't it be
set row = PayrollDatabaseDataSet.Allowances_Pay_Table.Rows(current_row)
?
Since you have just declared row as a variant, there might be a typecasting problem here.
confirm = MsgBox("Are you sure you want to alter this record", 1 + 48, "Please Confirm")
If confirm = 1 Then
' get new values for variables
row.employee_ID = CStr(Employee_IDTextBox.Text)
row.allowances_ID = Allowances_IDTextBox.Text
row.allowances_amount = Allowances_amountTextBox.Text
Else : MsgBox("Action cancelled!")
EndIf
In above code, after confirm, you are not altering emp name, may that could be the reason..
may be this sort of like should be included.. :
those are the only three fields in that table, so i don't think thats it. why its so weird is that its only the first record that doesn't save. if i go to the next record in the table, and save new amounts, or whatever for it, it saves fine. Just the first one doesn't