I'd like some help understanding why some of this works. Using the MS Access version of Northwind.mdb and VB2008 in VS2008.

I am pulling the customer records and displaying them on a form in text boxes and one combobox for the country.

I am populating the form with the following code
VB2008 Code:
  1. Public Class frmMain
  2.  
  3.    Private strFilename As String
  4.    Private AccConn As New OleDbConnection
  5.    Private mybindingsource As New BindingSource
  6.    Private AccCmd As OleDbCommand = New OleDbCommand
  7.    Private AccAdapter As OleDbDataAdapter = New OleDbDataAdapter
  8.    Private AccDS As DataSet = New DataSet
  9.  
  10.    Private Sub mnuCustomerView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCustomerView.Click
  11.       pnlCustRecords.Visible = True
  12.       pnlCustRecords.Enabled = True
  13.  
  14.       'setup toolbar
  15.       tsNew.ToolTipText = "Add a New Customer"
  16.       tsFirst.ToolTipText = "First Customer"
  17.       tsNext.ToolTipText = "Next Customer"
  18.       tsPrevious.ToolTipText = "Previous Customer"
  19.       tsLast.ToolTipText = "Last Customer"
  20.       tsNew.Enabled = True
  21.       tsFirst.Enabled = True
  22.       tsNext.Enabled = True
  23.       tsPrevious.Enabled = True
  24.       tsLast.Enabled = True
  25.  
  26.       'setup buttons
  27.       btnSave.Enabled = False
  28.  
  29.       'get data from database
  30.       AccCmd.CommandText = "SELECT CustomerID, CompanyName, ContactName, " _
  31.                               & "ContactTitle, Address, City, Region, " _
  32.                               & "PostalCode, Country, Phone, Fax " _
  33.                               & "FROM Customers " _
  34.                               & "ORDER BY CompanyName"
  35.  
  36.       AccCmd.Connection = AccConn
  37.  
  38.       AccAdapter.SelectCommand = AccCmd
  39.       AccDS.Clear()
  40.       AccAdapter.Fill(AccDS, "Customers")
  41.  
  42.       mybindingsource.DataSource = AccDS.Tables(0)
  43.  
  44.       'assign values to textboxes
  45.       txtCustID.DataBindings.Add("Text", mybindingsource, "CustomerID")
  46.       txtCompany.DataBindings.Add("Text", mybindingsource, "CompanyName")
  47.       txtContact.DataBindings.Add("Text", mybindingsource, "ContactName")
  48.       txtContTitle.DataBindings.Add("Text", mybindingsource, "ContactTitle")
  49.       txtAddress.DataBindings.Add("Text", mybindingsource, "Address")
  50.       txtCity.DataBindings.Add("Text", mybindingsource, "City")
  51.       txtProv.DataBindings.Add("Text", mybindingsource, "Region")
  52.       txtPostalCode.DataBindings.Add("Text", mybindingsource, "PostalCode")
  53.       cboCountry.DataBindings.Add("Text", mybindingsource, "Country")
  54.       txtTel.DataBindings.Add("Text", mybindingsource, "Phone")
  55.       txtFax.DataBindings.Add("Text", mybindingsource, "Fax")
  56.  
  57.       tsPosition.Text = (1 + mybindingsource.Position).ToString
  58.  
  59.  
  60.    End Sub

So the above code is working fine and pulls the data from the database and allows me to use toolbar buttons to move between records. This was part of what I wanted.

I want to be able to show a record and then delete it. So I put a record into the customer table that shows in my form. I can then delete it with the code below (I needed to add a record that didn't have orders as there is referential integrity set up in this file at the database level).

VB2008 Code:
  1. Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click, mnuCustomerDelete.Click
  2.       'deletes current record
  3.       'TODO: manage referential integrity and handle orders and order items
  4.  
  5.       Dim strSQLText As String
  6.       Dim intTotalRecords As Long
  7.  
  8.       strSQLText = "DELETE FROM Customers " _
  9.                               & "WHERE CustomerID = '" & txtCustID.Text & "'"
  10.  
  11.       Dim strSQL As New OleDbCommand(strSQLText, AccConn)
  12.  
  13.       intTotalRecords = strSQL.ExecuteNonQuery
  14.  
  15.       mybindingsource.RemoveCurrent()
  16.       mybindingsource.ResetCurrentItem()
  17.  
  18.  
  19.       mybindingsource.ResetBindings(False)
  20.  
  21.    End Sub

What I found was that I had to remove it from both the database and the bindingsource separately using the code above. This does seem to work but I am missing the concepts of why I need to do both if a bindingsource is used. I thought a binding source binds the data from the database to the fields.

So my main questions are:

1) Is the above delete code correct?
2) If not, what would you suggest as a better alternative?
3) Is there anything else I am missing in this attempt to look at some data and then delete a record?

Thanks for taking the time to read through this rather long post. I appreciate it. rasinc