1. Home
  2. Computing & Technology
  3. Visual Basic
Using the NotInList Event
Part 3: Turning Barb's App Into a Lookup Table
 More of this Feature
• Part 1: Barb Describes Her Problem
• Part 2: The VBA Code for Barb's App
 
 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
 

What we need to do now is apply this program to a more useful database. Barb's original requirement was to have Topics automatically added when something new was typed into the ComboBox but otherwise behave as a standard "lookup table". So let's create a lookup table for topics and put the rest of the data into a more normal database table.

two table database

We're going to skip over most of the creation of the Access database. If you need to know more about databases, I recommend that you try About.Com Databases. The completed database, however, is shown in the relationship diagram on the left.

TopicTable is the "lookup table" and the main ActivityTable simply refers to the values in TopicTable using the key of TopicTable as a foreign key. The easiest way to build this relationship in Access is to use the Lookup Wizard. This is the last data type in the Design dialog in Access.

two table database

This wizard fills in the Row Source property:

SELECT [TopicTable].[TopicKey], [TopicTable].[TopicName] FROM TopicTable;

The key has to be selected along with the TopicName field to make the database update automatically. To keep the information from displaying on the form, the Lookup Wizard uses a sneaky trick. The Column Width property of the first column is set to 0".

Column Width

Essentially the same VBA coding is used with our new database design, but this time, TopicTable is updated instead.

Private Sub TopicKey_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_Topic_NotInList
        ' Open a recordset using the
        ' TopicTable table.
        Set Db = CurrentDb
        Set Rs = Db.OpenRecordset( _
            "TopicTable", _
            dbOpenDynaset)
        ' Create a new record.
        Rs.AddNew
        ' Assign the NewData argument
        ' to the TopicName field.
        Rs![TopicName] = NewData
        ' Save the record.
        Rs.Update
        ' Set Response argument to indicate
        ' that new data is being added.
        Response = acDataErrAdded
Exit_Topic_NotInList:
       Exit Sub
Err_Topic_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

Since the rest of the form works quite normally, this technique expands the usefulness of the lookup table a lot and works quite transparently! The completed form looks like this:

completed application

First page > Barb Describes Her Problem > Page 1, 2, 3

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.