I am able to fetch a sql query from excel using the odbc connection:
But the issue is that, instead of just give a stack query setup and make user can only click the refresh button, I wish I could have some fields before the result, which user can enter some custom variables(e.g date from, date to)
so when they click refresh button, excel will take the variables of what user inputted into the pre-defined sql query and fetch the result from my sql server.
To do this sort of thing you need to run one query to fetch the initial values of your custom variables from the DBMS. For example, if you want to let the user change the date range, do this.
Use a query fetch the minimum and maximum date from the date column of your table.
SELECT MIN(date) mindate, MAX(date) maxdate
FROM table
Display those dates and let the user edit them on your form.
Put the results of those edited items into your second query, and retrieve the data the user asked for.
SELECT (whatever)
FROM table
WHERE date BETWEEN user-min-date AND user-max-date
The same kind of steps will work to populate a dropdown or listbox with a category
SELECT DISTINCT category
FROM table
ORDER BY category
Then
SELECT (whatever)
FROM table
WHERE category = user-selected-category
I think this addresses your question. Please clarify if it doesn't.
Related
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
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
PostPosted: 09 May 2014 22:26
Post subject: Determine table based on prompt
Hello,
I have three fact tables. First table holds current data, FACT_CUSTOMER_CURRENT. Other two tables hold historical snapshots. For example, one of these table holds last 60 days' records- FACT_CUSTOMER_DAILY. The other table holds data for the last day of the months.-FACT_CUSTOMER_MONTHLY
I want to add a date prompt. If the user selects yesterday as a prompt value, report should bring value from first table which holds current data (FACT_CUSTOMER_CURRENT). If user enters 28.02.2014, the report should retrieve data from FACT_CUSTOMER_MONTHLY. I tried to use context and aggregate awareness, but I could not be successful.
Can you help me?
Kind regards
There's no direct, easy way to do what you want.
Aggregate Awareness is useful for selecting a table based on the selection of objects in a query, but it does not support dynamic selection of tables based on values in a prompt.
If yesterday's data will only exist in fact_customer_current, then you can use this method: In your report, create a UNION query. One query includes objects from fact_customer_current, and the other from fact_customer_monthly. They both have an identical prompt on the appropriate date field. When a user enters yesterday's date, the first UNION query will return data but the second one won't. Likewise for date before yesterday, the first UNION will return no data but the second one will. This solution requires that the tables are correctly indexed such that a query on a date that isn't in the table will return quickly.
Hey I'm working on a database in access. Is there anyway in access I can be able to change the criteria for a query using either a report or a form so that the result set changes every time I change the criteria but the query remains the same e.g.
SELECT customer FROM customers WHERE id = 2 this would be the query and the criteria is 2 I want to be able to change the id every time to get a different result set either via a report or a form or any other way other than the query itself
You can refer to a form in a query:
SELECT customer FROM customers WHERE id = Forms!MyForm!txtID
You can open a report using a WHERE statement.
guys
' i am beginner in access 2007 and i want to create a form that contains many fields (product id, product name, etc and date of transaction) and use a query to search for data
in other words for example i want to enter in this form a date range i.e from 1/1/2013 to 1/03/2013 and search for product x ( attention ) my basic table contains only date of transacton field and not the from, to fields ( the from , to fields i want only to add them in the search form and them to search based on the value or date of transaction field ) please help me
Short but sweet answer;
Create a new "Query" using the GUI. Add your table to the top part. Double-click columns in the table to add them to the output of the Query.
Next, in the query conditions (grid below the name of each field) use brackets "[]" around your from/to dates. E.g. the query condition underneath your date field may be;
"between [MyStartDate] and [MyEndDate]".
Now "run" the query to test it; Access will prompt you for 'MyStartDate' and 'MyEndDate'.
Finally; if you save this query and set it as the "Data Source" for a Form, you may alter the above condition slightly to pull the values automatically from the form itself, so that you are not prompted every time the query runs. E.g. this condition may be;
"between Forms![MyTableForm]![MyStartDate] and Forms![MyTableForm][MyEndDate]".
You will have to play with it a little, but those are the basics.
I want to retrieve of particular date. For example my database has data from 1st-Jan to 31-AUG. I want to fetch data of 1st two months. Please suggest me helping query for this purpose thanks.
Open the query design window, add your table, double-click fields to add them to the grid. Type a date into the criteria line underneath the date field you added.
SQL query:
select *
from table
where table.my_date <= #29/02/2012#
will filter date before 29 feb. In query window you can type <=#29/02/2012#