View Full Version : Adding new record to database - overwriting first record


Loony064
06-22-2008, 10:49 PM
Hey all!

On my form, there aretextboxes for all the various fields. the user must enter data into the boxes and then click on the "Add Record" button. This will then create a new record with the data the user has entered.

It works mostly ok, as it adds the new record onto the end of the table, but then it also replaces the first record with the fields the user has entered as well.

Here is my code:


PrivateSub CmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAdd.Click
Dim dataR As payrollDatabaseDataSet.Allowances_Pay_TableRow
dataR = PayrollDatabaseDataSet.Allowances_Pay_Table.NewRow
' fill variables
dataR.employee_ID = Employee_IDTextBox.Text
dataR.allowances_ID = Allowances_IDTextBox.Text
dataR.allowances_amount = Allowances_amountTextBox.Text
' update records
PayrollDatabaseDataSet.Allowances_Pay_Table.Rows.A dd(dataR)
Allowances_Pay_TableTableAdapter.Update(PayrollDat abaseDataSet.Allowances_Pay_Table)
' display first record in(database)
current_row = 0
ShowCurrentRecord()
EndSub


Any idea's as to how i can solve this issue?

Also, for some reason, the changes i make to the database via my application program don't affect my database, i.e. after i close the program, the database reverts to what it was before i ran the program. My database datasource has a build action of "Compile" and Copy to output directory of "Copy Always"

Thanks
Laura

Loony064
06-24-2008, 02:02 AM
After a much time spent messing around, i managed to figure out a way to get it to work. I placed text boxes on my form over the text boxes used to display the record, and set them to not be visible. then, when users clear the record to input new values for a new record, i make the new textboxes visible and make the other ones not visible, take the users input and then switch the visibility back around.

Maybe not the best way, but it works. Here's the code i used, in case someone else encounters a similar problem:


'clear the text boxes for new input and set visibility
Private Sub CmdAdd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAdd2.Click
txtid.Visible = True
txtdesc.Visible = True
Project_IDTextBox.Visible = False
Project_DescriptionTextBox.Visible = False

txtid.Text = ""
txtdesc.Text = ""
CmdAdd.Enabled = True
MsgBox("You may now input values for new record")
End Sub

' add new record to database
Private Sub CmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAdd.Click
' add a new record to the database
Dim dataR As payrollDatabaseDataSet.Project_Description_TableRo w
dataR = PayrollDatabaseDataSet.Project_Description_Table.N ewRow
' fill variables
dataR.Project_ID = CStr(txtid.Text)
dataR.Project_Description = txtdesc.Text
' update records
PayrollDatabaseDataSet.Project_Description_Table.A ddProject_Description_TableRow(dataR)
Project_Description_TableTableAdapter.Update(Payro llDatabaseDataSet.Project_Description_Table)
' display first record in database
current_row = 0
ShowCurrentRecord()
End Sub

TylerTand
07-25-2008, 04:24 PM
Maybe this is too simple but why don't you have your form set to Data Entry to Yes.
This will automatically add the new data to the linked table. If you want to add a new set of data you can have a button with the code me!Requery

This will requery the form and allow you to enter the next set of data.

Tyler