Adding a record using a currency manager

  • Thread starter Thread starter gwboolean
  • Start date Start date
G

gwboolean

Guest
The btnAdd event in this app adds a new row to a table using a currencymanager. Additionally, some data is auto input onto the some of the form controls.et information then displays an editable new row.

At least in theory and I think in practice.


Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
MyBookmark = FileMasterManager.Count + 1
FileMasterManager.AddNew()
Me.Text = "FileMaster - Record " + CStr(FileMasterManager.Count) + " of " + CStr(FileMasterManager.Count)
#Region "Default Record information"
lblFileID.Text = "FM10000007" 'Value to come from datafile
txtName.Text = ""
txtDescription.Text = ""
lblDirectory.Text = "F:\SiTech" 'Value to come from datafile
chkUser1.Checked = False
chkUser2.Checked = False
chkUser3.Checked = False
chkUser4.Checked = False
chkUser5.Checked = False
chkUser6.Checked = False
chkUser7.Checked = False
chkUser8.Checked = False
chkUser9.Checked = False
chkUser10.Checked = False
rdoObsolete.Checked = False
rdoActive.Checked = True
rdoInactive.Checked = False
rdoUser1.Checked = False
rdoUser1.Checked = False
#End Region
SetState("Add")
Catch ex As Exception
MessageBox.Show("Error adding record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub


When the code is executed the form displays as expected. Data can be input and the saved.

1616274.jpg


Where I run into a problem is when I click btnSave and/or btnClose


Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim SavedName As String = txtName.Text
Dim SavedRow As Integer
Try
FileMasterManager.EndCurrentEdit()
FileMasterTable.DefaultView.Sort = "FileMasterName"
SavedRow = FileMasterTable.DefaultView.Find(SavedName)
FileMasterManager.Position = SavedRow
MyBookmark = FileMasterManager.Count
SetText()
MessageBox.Show("Record saved.", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
SetState("View")
Catch ex As Exception
MessageBox.Show("Error saving record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub frmFileMaster_Closing(sender As Object, e As EventArgs) Handles Me.FormClosing
Try
'Save changes to database
Dim FileMasterAdapterCommands As New OleDbCommandBuilder(FileMasterAdapter)
FileMasterAdapter.Update(FileMasterTable)
Catch ex As Exception
MessageBox.Show("Error saving database to file:" + ControlChars.CrLf + ex.Message, "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
#Region "Close Connection"
FileMasterConnection.Close()
'dispose of the objects
FileMasterConnection.Dispose()
FileMasterCommand.Dispose()
FileMasterAdapter.Dispose()
FileMasterTable.Dispose()
#End Region
End Sub


What I end up with is no new record and the last record in the table being overwritten by the data meant for the new record.

I am at a loss as to where I went wrong.






gwboolean

Continue reading...
 
Back
Top