t_tip1.gif (7229 bytes)

 
 
I have been presented with this problem may times when training or consulting at a company. The solution is simple but the explanation is slightly long because I believe understanding serves students better in the long run than a quick fix.

First, the customer form is based directly on (bound to) a table or on a query that alphabetizes the customers in the table. No problem here.

Second, The combo box was created with the Access combo box wizard, and the customer table was selected as the source of the customer ID's and names. Again, no problem.

Third, the developer assumes that, when he or she uses the form to add a new customer to the table, both the form and the combo box should be updated as both are ultimately based on the same customer table. Issue (as Microft euphemistically puts it), wrong assumption.

Access automatically updates the bound customer table and the form when you leave or save a new record. However, it does not automatically update the combo box that is based on the same table.

The trick to know is that the combo box wizard creates a query behind the scenes and assigns the query's SQL statement to the Row Source property of the combo box. You can check it out by looking at the Data properties for the combo box and noting that the Row Source property contains text beginning "SELECT ..." That is the start of the SQL statement. (To view the properties of a form's combo box control, open the form in design view, right-click on the combo box control, and select "Properties" from the little pop-up menu that appears. Then click on the "Data" tab.)

When you open the form, both the form and the combo box retrieve the current data from the customer table. They are in synch. However, after that, you can regard the combo box with its Row Source as existing on the form but as having a somewhat independent life of its own.

As you add new customers and Access automatically updates both the bound table, the independent combo box's list becomes increasingly outdated. You have to cause it to be updated to remain in synch with the updated table. One solution - not recommended - close and reopen the form.

The better solution is simple if you know the name of the combo box, know what form event to use, and know the one line of VBA code to write.

To get the name of the combo box, view the properties for the combo box and click on the "Other" tab. Its "Name" is at the top of the list. Write down the name or copy it to the clipboard. If you know what you are doing, you may give the combo box a more meaningful name like "cboPickCustomer." However, if your combo box is already being referred to elsewhere in your database by other database objects, leave the name as it is.

Next, the correct form event is its After Update event. This form event occurs after your new customer has been saved in the table and the form has been updated. That means the new customer data is in the table so your combo box can fetch it and add it to its Row Source list when you command it to do so.

Open the form in design view, the properties for the form and click the "Event" tab.

Note: a quick way to get to the form's properties after opening the form in design view is to double click the little square in the upper left corner of the form's border. Works on reports, too.

Once you access the form's properties, click in the After Update property. Drop down the list and select "[Event Procedure]." Then click the little button with the three dots on it. This button puts you in the code module that is attached to the form. You are now ready to write your one line of code on its own line between the two lines reading "Sub" and "End Sub".

We will assume the name of the combo box is something like "cboPickCustomer."

The one line of code is:

Me!cboPickCustomer.Requery

or

Me("cboPickCustomer").Requery

Do not create a typo. We have not included any error trapping in this mini-procedure. If you write code, you should always error trap your procedures, but error trapping is a different and lengthy topic.

I suggest you select "Debug/Compile" from the menu. The compiler will report most errors in your code, but not all. Then save your module.

Close the code module by clicking on the "x," save the form, and open it. Add a new customer, and save the data by moving to a new record. You should then see the customer in your combo box's drop-down list..

What did we do? "Me" refers to the form itself. "!" is a separator and says that the text following it is the name of a control on "Me." "cboPickCustomer" is the name of the control. "." is a separator that says the text that follows identifies a property or method. "Requery" is a method which instructs the combo box to rerun its query, fetching a refreshed set of data from the customer table.

That's it. If your situation resembles this hypothetical one, this advice may be of help. Always check your work and make sure the results of your coding are what you want.

   
Tales  Tips Menu
Tales Company
Home Home
Contact Us Contact Us