Share via


Visual Basic Concepts

Modifying the Form to Let You Add New Records

In addition to viewing and updating existing records, you may want to add new records to your data source. You can modify your form so that it can serve as a data entry form by creating command buttons that use ActiveX Data Objects (ADO) and the BindingsCollection object to clear the display of data, manage the data binding of controls, and add a new record to the underlying recordset.

This topic shows how to modify the form you created in the previous topic so that it also serves as a data entry form for adding new customer records.

To modify the form to let you add new records

  1. Create command buttons for allowing data entry, adding new customer records, and returning to viewing data.

  2. Add code to enable data entry.

  3. Add code to enable saving the data you enter as a new record.

  4. Add code to enable returning the form to viewing data.

****Note   ****This topic is part of a series that walks you through creating a simple database application that interacts with data in a tab-delimited text file. It begins with the topic Interacting with Data in an ASCII Text File.

Create Command Buttons for Allowing Data Entry, Adding New Customer Records, and Returning to Viewing Data

The first step in modifying the form is to create the interface for the tasks that you want to accomplish. For example, to allow data entry on the customer address form you created in the previous topic, you could add the following:

  • A DataEntry command button to clear the existing data displayed on the form and disable data binding.

  • An AddCustomer command button to add new data entered on the form as a new record in the underlying recordset.

  • A ViewData command button to re-enable data binding, returning the form to its original state.

First, add a command button to the form and change its Caption and Name properties to DataEntry. Add a second command button to the form and change its Caption and Name properties to AddCustomer. Then add a third command button to the form and change its Caption and Name properties to ViewData.

Because the AddCustomer and ViewData command buttons should only be displayed when the form is being used for data entry, set the Visible property for these controls to False.

Add Code to Enable Data Entry

You can make a data-bound form also serve as a data entry form by disabling data binding and clearing the existing data displayed on the form. You may also want to show hidden command button controls that apply only to adding new records, and hide command button controls that apply only to viewing existing records.

For example, to enable the DataEntry command button on the customer address form, add the following code to the DataEntry_Click event procedure:

Private Sub DataEntry_Click()
   ' Disable data binding.
   Set colBind = Nothing

   ' Clear the text box controls.
   Me.txtCustomerID = ""
   Me.txtCompanyName = ""
   Me.txtAddress = ""
   Me.txtCity = ""
   Me.txtRegion = ""
   Me.txtPostalCode = ""
   Me.txtCountry = ""

   ' Hide the command buttons used for viewing
   ' existing data.
   Me.Next.Visible = False
   Me.Previous.Visible = False
   Me.First.Visible = False
   Me.Last.Visible = False
   Me.DataEntry.Visible = False

   ' Show the command buttons used for entering new data.
   Me.AddCustomer.Visible = True
   Me.ViewData.Visible = True

End Sub

Add Code to Enable Saving the Data You Enter as a New Record

After you've entered record data in a data entry form, you can use ADO to add the record to a recordset. For example, to enable the AddCustomer command button on the customer address form, add the following code to the AddCustomer_Click event procedure:

Private Sub AddCustomer_Click()

   ' Add the record to the rsCustomers recordset
   ' in your data-aware class.
   With objDataSource.rsCustomers
      .AddNew
      !CustomerID = Me.txtCustomerID.Text
      !CompanyName = Me.txtCompanyName.Text
      !Address = Me.txtAddress.Text
      !City = Me.txtCity.Text
      !Region = Me.txtRegion.Text
      !PostalCode = Me.txtPostalCode.Text
      !Country = Me.txtCountry.Text
      .Update

   End With

   ' Clear the controls for additional data entry,
   ' if desired.
   Me.txtCustomerID = ""
   Me.txtCompanyName = ""
   Me.txtAddress = ""
   Me.txtCity = ""
   Me.txtRegion = ""
   Me.txtPostalCode = ""
   Me.txtCountry = ""

End Sub

Add Code to Enable Returning the Form to Viewing Data

When you've finished using your form as a data entry form, you can return it to its original use for viewing and editing existing records by re-enabling data binding. Any new records you've entered will now be displayed when you move through records on the form. You can also hide command button controls that apply only to adding new records, and show hidden command button controls that apply to viewing existing records.

For example, to enable the ViewData command button on the customer address form, add the following code to the ViewData_Click event procedure:

Private Sub ViewData_Click()

   ' Bind text box controls to the data source
   ' of your data-aware class.
   Set colBind = New BindingCollection

   Set colBind.DataSource = objDataSource
   colBind.Add txtCustomerID, "Text", "CustomerID"
   colBind.Add txtCompanyName, "Text", "CompanyName"
   colBind.Add txtAddress, "Text", "Address"
   colBind.Add txtCity, "Text", "City"
   colBind.Add txtRegion, "Text", "Region"
   colBind.Add txtPostalCode, "Text", "PostalCode"
   colBind.Add txtCountry, "Text", "Country"

   ' Show the command buttons used for viewing
   ' existing data.
   Me.Next.Visible = True
   Me.Previous.Visible = True
   Me.First.Visible = True
   Me.Last.Visible = True
   Me.DataEntry.Visible = True

   ' Hide the command buttons used for entering new data.
   Me.AddCustomer.Visible = False
   Me.ViewData.Visible = False

End Sub

For More Information   To read about the BindingCollection object, see Creating Data-Aware Classes in "Programming with Objects" in the Programmer's Guide.

Step by Step

This topic is part of a series that walks you through using a data-aware class and ADO to create a simple database application that interacts with data in a tab-delimited text file.

To See
Go to the next step Modifying the Class and Form to Write Records Back to the Delimited Text File
Start from the beginning Interacting with Data in an ASCII Text File