; Access Database
Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

Access Database

VIEWS: 21 PAGES: 2

  • pg 1
									                                          Access Database Programming

Changing the default control for our Data

   1. When you want to use a table and not the DataGridView Control, click on the arrow for the table (in data sources
      view) and choose Details. Then VS will put our data into a table.




   2. Then drag the customers table onto the form and VB creates all the labels and textboxes for you. It also adds the
      navigation bar at the top of the form so you can add/edit/delete records.




   3. Now you just need to edit your labels if you want and add error handling.
   4. Make sure the MaxLength property of each texbox matches the field length of the database. I give you a list of
      my customers-03.mdb below. In other words, if you design the middleinit field to only allow 1 character, you have
      to make sure the user can only enter 1 letter. Otherwise, you get an error when you update the records.

                                 DB Field Name                      Max Size
                                 LastName                           20
                                 FirstName                          15
                                 MiddleInit                         1
                                 StreetAddress                      25
                                 City                               15
                                 State                              2
                                 Zip                                10
                                 Tele                               13
                                 Fax                                13
5. Now add error handling:

   Private Sub CustomersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles CustomersBindingNavigatorSaveItem.Click
        ' When they save, three errors can occur: Server, ADO, & concurrency. After trapping and giving a message,
        ' I reload/refresh the DB so they can try again. I use my own sub to do this.
    Try
       Me.CustomersBindingSource.EndEdit()
       Me.CustomersTableAdapter.Update(Me.CustomerDataSet.Customers)
    Catch ex As DBConcurrencyException 'concurrency error
       MessageBox.Show("Someone else changed this account so I cannot save your changes." _
       , "Mulitple Users Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
       LoadDB()
    Catch ex As DataException               'ADO error edit/update/delete
       MessageBox.Show(ex.Message, ex.GetType.ToString)
       LoadDB()
    Catch ex As OleDbException               'server error
       MessageBox.Show("I cannot access the database." & ex.Message, ex.GetType.ToString)
       LoadDB()
    End Try

  End Sub

  Private Sub Step3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'I use this in the error handler so might as well direct them there now.
        LoadDB()
  End Sub

  Private Sub LoadDB()
     Me.CustomersTableAdapter.Fill(Me.CustomersDataSet.Customers)
  End Sub

6. Next we add data validation. We are only checking to make sure there is something typed into each textbox for the
    Last Name & First Name. But you can do the same for any texbox you want.

   The property called “CausesValidation” is set to true when you add a textbox to a form. What happens is that as soon
   as the textbox loses the focus, the Validating event fires. The code below executes when the focus is on the control
   and the save button is clicked or when the focus leaves the control itself.

   This is not the only way to make sure they typed something but is a cool feature.
   ' PLEASE NOTE: IF THEY TRY TO DELETE DATA IN THESE BOXES THEY WON'T BE ALLOWED TO
   ' MOVE TO ANOTHER TEXTBOX AND WON'T BE ABLE TO EXIT THE APPLICATION!!!!

  Private Sub CustIDTextBox_Validating(ByVal sender As System.Object,_
        ByVal e As System.ComponentModel.CancelEventArgs) Handles CustIDTextBox.Validating
            If Trim(CustIDTextBox.Text) = "" Then
               e.Cancel = True
               MsgBox("Please enter the customer ID", MsgBoxStyle.Critical, "System Message")
            End If
  End Sub

  Private Sub LastNameTextBox_Validating(ByVal sender As System.Object,_
        ByVal e As System.ComponentModel.CancelEventArgs) Handles LastNameTextBox.Validating
            If Trim(LastNameTextBox.Text) = "" Then
               e.Cancel = True
               MsgBox("Please enter the last name", MsgBoxStyle.Critical, "System Message")
            End If
  End Sub

  Private Sub FirstNameTextBox_Validating(ByVal sender As System.Object,_
        ByVal e As System.ComponentModel.CancelEventArgs) Handles FirstNameTextBox.Validating
              If Trim(FirstNameTextBox.Text) = "" Then
                  e.Cancel = True
                  MsgBox("Please enter the first name", MsgBoxStyle.Critical, "System Message")
              End If
  End Sub

								
To top