Adding new record to database - overwriting first record

Loony064

Registered User.
Local time
Today, 21:46
Joined
Jun 4, 2008
Messages
10
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:

Code:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CmdAdd_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] CmdAdd.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dataR [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] payrollDatabaseDataSet.Allowances_Pay_TableRow[/SIZE]
[SIZE=2]dataR = PayrollDatabaseDataSet.Allowances_Pay_Table.NewRow[/SIZE]
[SIZE=2][COLOR=#008000]' fill variables[/COLOR][/SIZE]
[SIZE=2]dataR.employee_ID = Employee_IDTextBox.Text[/SIZE]
[SIZE=2]dataR.allowances_ID = Allowances_IDTextBox.Text[/SIZE]
[SIZE=2]dataR.allowances_amount = Allowances_amountTextBox.Text[/SIZE]
[SIZE=2][COLOR=#008000]' update records[/COLOR][/SIZE]
[SIZE=2]PayrollDatabaseDataSet.Allowances_Pay_Table.Rows.Add(dataR)[/SIZE]
[SIZE=2]Allowances_Pay_TableTableAdapter.Update(PayrollDatabaseDataSet.Allowances_Pay_Table)[/SIZE]
[SIZE=2][COLOR=#008000]' display first record in(database)[/COLOR][/SIZE]
[SIZE=2]current_row = 0[/SIZE]
[SIZE=2]ShowCurrentRecord()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

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
 
Solved

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:

Code:
[SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'clear the text boxes for new input and set visibility
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CmdAdd2_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] CmdAdd2.Click
txtid.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]txtdesc.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]Project_IDTextBox.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]Project_DescriptionTextBox.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]
txtid.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2]txtdesc.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2]CmdAdd.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]MsgBox([/SIZE][SIZE=2][COLOR=#800000]"You may now input values for new record"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' add new record to database
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CmdAdd_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] CmdAdd.Click
[/SIZE][SIZE=2][COLOR=#008000]' add a new record to the database
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dataR [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] payrollDatabaseDataSet.Project_Description_TableRow
dataR = PayrollDatabaseDataSet.Project_Description_Table.NewRow
[/SIZE][SIZE=2][COLOR=#008000]' fill variables
[/COLOR][/SIZE][SIZE=2]dataR.Project_ID = [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](txtid.Text)
dataR.Project_Description = txtdesc.Text
[/SIZE][SIZE=2][COLOR=#008000]' update records
[/COLOR][/SIZE][SIZE=2]PayrollDatabaseDataSet.Project_Description_Table.AddProject_Description_TableRow(dataR)
Project_Description_TableTableAdapter.Update(PayrollDatabaseDataSet.Project_Description_Table)
[/SIZE][SIZE=2][COLOR=#008000]' display first record in database
[/COLOR][/SIZE][SIZE=2]current_row = 0
ShowCurrentRecord()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
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
 

Users who are viewing this thread

Back
Top Bottom