With just a little bit of SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form. If you have never written code or looked at an SQL statement before, no problem! ... give it a chance. Unleash a great new potential.
This technique of basing the Row Source of one control on the value in another is called "cascading", and you will probably hear that term more in reference to combo boxes.
A list box has many similarities to a combo box such as Row Source, Column Count, Column Widths, and Column Heads. The Width of a list in a combo box is List Width; in a list box, it is the same as the control Width.
As the customer changes, VBA runs to add criteria to the SQL statement for the list box. SQL is what a query stores to know what data to get, where it comes from, and how to sort. SQL is Structured Query Language. Don't let the acronym intimidate you.
An SQL statement is simply a standardized way to get information from database tables. It specifies what to show (Select), and where data comes from (From). Optional clauses include criteria (Where), and how to sort (Order By). The basic syntax for an SQL statement is:
SELECT fieldlist
FROM tablename
WHERE criteria
ORDER BY fieldlist;
To get an SQL statement into the Row Source of a combo box or list box, you can: (1) Make a query to show what you want, switch to SQL view, and copy the SQL statement, or (2) click in a control's Row Source property, then on the Builder button (...), specify what you want in the query builder, then save, and close the builder, or (3) write the SQL yourself.
To make the demonstrated code work, copy the resulting SQL statement from the Row Source to the control's Tag property (bottom of Other tab on property sheet). Tag is not used by Access; it is a place where you can put whatever you want. In this case, it will store the SQL statement with no Where clause for each respective control.
When the customer changes, VBA reads the customer to construct a Where clause. Both list boxes have a source with CustomerID (Long Integer). For the customer products, CustomerID is in the Orders table. For the payments, CustomerID comes from the PayGroups table.
When the focus moves to a record, it becomes the current record, and the form Current event happens. On the property sheet, this is called On Current and can be set to a macro name, a function name, or [Event Procedure].
The shortcut key to launch the Builder is Ctrl-F2.
The code shows how to use the control (With) to read the Tag (saved by developer) to get the SQL statment without criteria, add criteria, replace the Row Source with the new SQL statment, then build the list to show the latest data.
See how to create code behind the form, and how to call one procedure from another. Learn line-by-line what is happening. This knowledge will propel you to a new level, and raise the bar on database applications that you build.
have an awesome day,
crystal
Access Basics (free book)
http://www.AccessMVP.com/strive4peace
Free 100-page book that covers essentials in Access
Learn VBA (free book)
http://www.AccessMVP.com/strive4peace/VBA.htm
http://www.MsAccessGurus.com
connect to me, let's build it together