Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
Using the NotInList Event
Part 2: The VBA Code for Barb's App
 More of this Feature
• Part 1: Barb Describes Her Problem
• Part 3: Turning Barb's App Into a Lookup Table
 
 Join the Discussion
Is this the kind of article that helps you?
Let us know!
 
 Related Resources
• About Databases
The About.COM site for Database Programming
• The About Visual Basic VBA Tutorial Series
 

In brief, NotInList allows you to quickly and easily (if you know exactly how to do it) update an Access database table if you enter information into a ComboBox that isn't already present in the table. You can select information from the dropdown or enter new information and it will be quietly added to the lookup table.

Here's the 'official' description from Access Help:

The NotInList event occurs when the user enters a value in the text box portion of a combo box that isn't in the combo box list.

Of course, if you DON'T know how to do it, it can be a real pain and it's not at all quick and easy.

To detail how NotInList works, we're going to start with one simple form with one ComboBox and update just one table. But all of the VBA code necessary will be introduced in this simple example. Once we see the basic coding in the program, we'll build a slightly more useful application - one that fits Barb's requirements - that updates a two table database where the tables are linked with a relationship.

single table example

Here's the 'single table - single form - single component' application we're starting with. First, we create a very simple form with a single ComboBox component. The Combobox is linked to the Activities table using the RowSource property as follows:

Now … here the VBA code required for the job!

Open a VBA code window (Tools > Macro > Visual Basic Editor is one way to do it) and enter this code (Note: VB program statements are continued to keep them short for the format required on the page. Feel free to expand the statements out to their full length again.)
Private Sub ActivityNames_NotInList _
    (NewData As String, _
    Response As Integer)
Dim Db As DAO.Database
Dim Rs As DAO.Recordset
Dim Msg As String
On Error GoTo Err_ActivitiesKey_NotInList
        ' Open a recordset using the
        ' Activities table.
        Set Db = CurrentDb
        Set Rs = Db.OpenRecordset( _
            "Activities", dbOpenDynaset)
        ' Create a new record.
        Rs.AddNew
        ' Assign the NewData argument to
        ' the ActivityName field.
        Rs![ActivityName] = NewData
        ' Save the record.
        Rs.Update
        ' Set Response argument to indicate
        ' that new data is being added.
        Response = acDataErrAdded
Exit_ActivitiesKey_NotInList:
       Exit Sub
Err_ActivitiesKey_NotInList:
       ' An unexpected error occurred, _
       ' display the normal error message.
       MsgBox Err.Description
       ' Set the Response argument to suppress
       ' an error message and undo changes.
       Response = acDataErrContinue
End Sub

The DAO (Data Access Objects) library is required when Database and Recordset are declared at the top of the program. Add a reference to DAO using Tools > References. The Microsoft DAO 3.6 Object Library was used when this example was tested.

single table example

When the form is opened, you can add new items to the Activities table simply by typing a new value into the textbox part of the ComboBox control. This triggers the NotInList event which actually updates the table. Notice that, since the form isn't bound to the database, the automatic navigation buttons at the bottom of the form are not updated and only show one record even though several records have been added to the table. Another trick is to end the NotInList event subroutine with

Response = acDataErrContinue

This forces Access to ignore the error condition which originated the trigger for the NotInList subroutine.

Next page > Turning Barb's App Into a Lookup Table > Page 1, 2, 3

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.