1
votes

I have a table that contains all the weeks of the year (using the client's numbering, so Week 1 is in June), and the dates they start. There is a form where they can choose which week they want to look at, so I've used a ComboBox that grabs all the week numbers for which they've entered data in the WeeklyHours table, using SELECT Format(WeeklyHours.Week,"0") AS Expr1 FROM WeeklyHours GROUP BY WeeklyHours.Week;

This combobox is then supposed to be used as the week filter for a couple queries I've built, using the combobox value as the matching criteria. The problem is that when the form is closed, those queries can't run, and give me the Enter Parameter Value error for the combobox value.

To fix this, I tried to create a new table called SelectedWeek with a single entry called Week_Number. There is then some AfterUpdate code that saves the selected combobox value to the Week_Number field of SelectedWeek.

I then changed the queries to point to [SelectedWeek]![Week_Number], so that the queries will always use whatever the most recently selected week was. However, I keep getting the Enter Parameter Value error for SelectedWeek!Week_Number, and I can't figure out why. Any help would be most appreciated.

Thanks, Joel

1

1 Answers

0
votes

Reason the user is prompted for Parameter Value (by the way, it is not an error) is in both cases the Access SQL engine cannot see either referenced values. In first case, the form is closed and in second case column is not properly aligned to a lookup.

In first scenario, simply keep form open which user selects value from combobox when running other queries. Otherwise, all content on form is not callable since it is closed from memory:

SELECT * FROM TableName WHERE weeknumber = Forms!FormName!WeekNumber

In second scenario, use a DLookUp() part of the Domain Function Family.

SELECT * FROM TableName WHERE weeknumber = DLookUp("Week_Number", "SelectedWeek")

And really, domain functions can be generalized as subqueries in SQL:

SELECT * FROM TableName 
WHERE weeknumber IN (SELECT Week_Number FROM SelectedWeek)

Even more, you can run a cross join query (tables separated with commas in FROM clause) of the two tables and avoid lookups. Below assumes SelectedWeek is a one-row, one-column table but with the WHERE condition, length is handled and you can explicitly declare columns in either table:

SELECT * 
FROM TableName, SelectedWeek
WHERE TableName.weeknumber = SelectedWeek.Week_Number