Unfortunately I was unable to get it to work still. I have no idea what I’ve
done wrong. In my main form (Form1). I got the following event procedure
codes in the after update and On current fields: (note my subform is named
Form2 with the combo box in it named Product, the combo box in Form 1 is
named Category)
Private Sub Category_AfterUpdate()
Me.Form2.Form.Product = Null
Me.Form2.Form.Product.Requery
Me.Form2.Form.Product = Me.Form2.Form.Product.ItemData(0)
End Sub
Private Sub Form_Current()
Me.Form2.Form.Product.Requery
End Sub
I get the following error messages when I enter the form view.
The expression AfterUpdate you entered as the event property setting
produced the following error: Invalid outside procedure.
*The expression may not result in the name of a macro, the name of a user
defined function, or [Event Procedure]
*There may have been an error evaluating the function, event or macro.
The expression on current you entered as the event property setting produced
the following error: Invalid outside procedure.
*The expression may not result in the name of a macro, the name of a user
defined function, or [Event Procedure]
*There may have been an error evaluating the function, event or macro.
Here is some additional information about my combo boxes in case this has
something to do with why its not working properly:
Both Combo boxes are unbound and in the product combo box in the subform I
have the row source set as: SELECT [Sub Categories Table].[Sub Category ID],
[Sub Categories Table].[Category ID], [Sub Categories Table].[Sub Category]
FROM [Sub Categories Table] WHERE ((([Sub Categories Table].[Category
ID])=Forms!Form1!Category));
Note my subcategories table is where the products are found. The subcategory
table has subcategory id (product id), category id, and subcategory name
(product name).
The product combo box in the subformhas no event procedures in it.
In the category combo box in the main form I have the row source set as:
SELECT Categories.[Category ID], Categories.Category FROM Categories;
Where the categories table has category id and category name.
I don’t have problems with what is listed in the combo boxes but I just
can’t get the product from combo box two to show up based on the category in
comb box one on the main form.
Karl wrote:
I have two combo boxes. On the main form (Form 1) is a Category combo box.
On the subform (Form 2) is a Product combo box. Using a where query I have
set it up so the Product 2 combo box is based on the category combo box. This
works fine for the first choice. I.e. the first time i open the form and
choose a category the appropriate products appear in the combo box but when i
choose a different categories the products combo box doesn't update. I've got
it to work when both combo boxes are the main form using this AfterUpdate
event:
Private Sub CategoryAfterUpdate()
Me.Product = Null
Me.Product.Requery
Me.Product = Me.Product.ItemData(0)
End Sub
However I can't figure out how to adjust this event too look for product in
the subform (Form2) (instead of on the main form).
At this time both of these combo boxes are unbound. However it would be
better for me if they were bound (i.e Bind Categories to the Categories table
(which contains Category ID and Category Name and Bind Products to the
Products table (which contains product id, product name and category ID). Is
it possible to have these combo boxes bound and still use this method for
looking up products based on the categories.
:
The corresponding code for main form to subform is:
Private Sub CategoryAfterUpdate()
With Me.subformcontrol.Form
.Product = Null
.Product.Requery
.Product = .Product.ItemData(0)
End With
End Sub
You will also need to do the Requery in the main form's
Current event to keep the Category and products synchronized
when the main form navigates from one rcord to another.
Private Sub Form_Current()
Me.subformcontrol.Form.Product.Requery
End Sub
Bound or unbound makes a big difference in that your code is
dirtying the subform's current record, which something that
has important consequeces. OTOH, if the subform can have
more than one record, you should invalidate the product
field in every record when the Category is changed.
I suggest that you get rid of the two assignment statements
and, for now, only use the Requery. Once you get that part
to work correctly, then think long and hard about what you
need to happen when the Category is changed and the child
table now contains records with the wrong product. I
suspect that more than one field in the subform records will
become invalid.
Karl wrote:
Thanks for helping. I am new to Access and Programming language so this
problem has been driving me nuts. Unfortunately your solution did not quite
work properly. I got an error message for both the AfterUpdate and OnCurrent
that said:
The expression On Current you entered as the event property setting produced
the following error: Invalid Outside procedure.
* The expression may not result in the name of a macro, the name of a
user-defined function, or [event procedure]
* there may have been an error evaluating the function event or macro.
I put your code in the AfterUpdate of the main form (Form1) and current
requrry in main form as well. One question i have too is that In your code
you wrote Form does that refer to the main form or my subform (Form2). I
tried it both ways and niether worked.
I am also a little unsure about what you were talking about in the bound
explanation as I am unfamilar with some of the programing lingo I.e dirtying&
invalidate.
Marshall Barton said:
The code goes in the appropriate event **procedure**, not
in an event **property**.
Select [Event Procedure] for the event property. Then
click on the builder button in the right margin to bring up
the event procedure and add the requery statement between
the Sub and End Sub lines.