MS Access how can i pass my search criteria from top query to subquery? - ms-access

I have a query in a base-data table that (given that my search criteria are correct) gives back approx. 950 records.
Except of the 3 criteria fields, i want to have about 10 more fields (the Project is still at the beginning) , every single one based on sub-queries, some of them normal select queries, some are aggregated queries.
As far as i know every sub-query must give 1 and only one value back.
This value school be individual for every Record of the top query.
My Problem now is, that i don't know how to pass the search criteria from the top query (simple select query) to the sub-query in the in 10 fields i mentioned before.
Is this possible at all, or is my Approach to complicated. Is there possibly an easier way?
I have a Windows 7 System with Office 2010 installed.
Your help is much appreciated.
Thanks a lot.
PS
The sub-queries are based on the same table as the top query. Sorry, I forgot to mention.

You can pass arguments between things with a function call to set a public variable. This vba must be in a Module, not behind a Form Module. I don't use this approach very often, because the global value is in volatile memory, I prefer to save the variable in a special data Table.
Public strGlobal As String
Function Func_ReadGlobal() As String
Func_ReadGlobal = strGlobal
End Function
Function Func_WriteGlobal() As String
strGlobal = Func_WriteGlobal
End Function

In all subqueries create parameter(s) and use it as search criteria. Parameter name should be the same for same column. Now, if you use those subqueries in your main query, Access will ask only once per each parameter name, you don't need to pass them explicitly to subqueries.

Thank you guys.
I did'nt think of the most obvious solution with the Globals. I will try it out as soon as my Boss gives me the time to continue with the Project.
#Sergey
I can't use the Parameter(s) way, because the whole query, incl. Subqueries shall run completely alone in VBA, without human input at all.

Related

recordset method to replace dlookup (lazy) 'habit'?

please pardon my virgin post and if my question somehow got answered elsewhere, please kindly point me to that link (i did read related dlookup topic but didnt get what i want).
i freq use dlookup (due to newbie in access and lazy too) on forms for my textbox to hold values for my purpose (calculations, display) and knowing dlookup is random and takes time if db isnt local (which i am task to move it online) and time for me to utilize more professional approach to replace dlookup usage in general.
what i used to do is to retrieve data from 1 table (inventory) to populate textbox in a form:
txtItemName=dlookup ("ItemName","Inventory","Inv_ID=" & cboItem)
txtItemDesc=dlookup ("ItemDesc","Inventory","Inv_ID=" & cboItem)
txtItemPrice=dlookup ("ItemPrice","Inventory","Inv_ID=" & cboItem)
and such for data i need and sometimes could be more 10 dlookup(s) to lookup data for my textboxes.
i did read about recordset (DAO/ADO) but due to newbie in ms-access usage, i couldnt figure how to write (or copy) code that could perform what i need like the above dlookup(s).
i did try creating query to pull fields (say 10 fields) i need in 1 query and use dlookup (again) in recordsource to hold the value. (if this method has any advantage compared to 10x original dlookup i did before...)
i am willing to learn the right method to do the right thing in ms-access and seek all seniors to show me the way and kick my bad/newbie habit of abusing dlookup.
i thank you all in advance.
Actually, using dlookup() or using VBA code and a reocrdset?
The performance is 100% the SAME.
I repeat the performance is 100% the SAME.
You gain ZERO benefits by writing some VBA code that creates a recordset, and then creates the sql, and then pulls the one record. It is in fact a waste of time to pursue such a road and concept. It does NOT HELP performance at all, and using VBA code + a reocrdset to pull the one record and value is NOT faster.
They both perform the SAME speed!!!
What can make a difference? If you have dlookup() over and over in the sql, and you execute dlooup() for EACH ROW of the sql. Then that is slow.
So, say we had a combo box, and we have say the "id, HotelName".
So, in a query you might do this:
select FirstName, LastName, dlookup("HotelName","tblHotels","ID = " & Hotel_ID) from bookings
In above, we used dlookup() to get the Hotel name, since I did not want to display the "hotel_id". Above is going to be VERY slow. The faster way is to use a sql join like this:
SELECT FistName, LastName, Hotel_id, tblHotels.HotelName
LEFT JOIN tblHotels on tblBookings.Hotel_id = tblHotels.id
FROM tblBookings.
So, in above, we dumped the dlookup() (slow), to above, which will run very fast.
So, running ONE dlookup() vs VBA + reocrdset to get the ONE value? Same speed, no need to use VBA code to replace the dlookup() function.
But, if you run the function over and over - one time for each row? Then BOTH use of dlookup(), and use of a custom VBA function to do that for each row will BOTH be slow.

Using Switch Function or nested iif in an Access Query

I have a small little Access program that sums hours of employees, with queries for Daily, Weekly, Monthly, and from the start of the year, all accessed from an easy to read form.
I have been asked to make an option group to filter employees by fulltime, seasonal, or both. But an issue I have is that I can only access the PAYRULENAME from the table which begins 'FT' for Fulltime such as FTADBRK1 but Seasonal can be a whole mishmash of possibilities, just not beginning with "FT". Still, I did not think this to be an issue.
Since I already have the original queries done, I thought I could simply add an additional column and constraint for each query instead of making additional queries. So I tried some various methods. Such as:
Switch([Forms]![MyForm]![EmpType].Value="FT",Left([PAYRULENAME],2)="FT",[Forms]![MyForm]![EmpType].Value="S",Left([PAYRULENAME],2)<>"FT"[Forms]![MyForm]![EmpType].Value="All",[PAYRULENAME])
Where EmpType is a textbox that programmatically gives a value for the option group instead of 1,2, or 3. This works and I don't think this is where the issue is.
But using the above switch function gives a -1 for Fulltime or a 0 for Seasonal (S) and doesn't filter the query. (The last option is fine - but it would be nice to give an FT or S in a column instead of the entire PAYRULENAME).
I have also tried nested iif statements to no avail. Here is one: IIF([Forms]![MyForm]![EmpType].Value="All",[PAYRULENAME],IIF([Forms]![MyForm]![EmpType].Value="FT",Left([PAYRULENAME],2)="FT",Left([PAYRULENAME],2)<>"FT"))
Any suggestions would be appreciated!

Creating a global variable in Talend to use as a filter in another component

I have job in Talend that is designed to bring together some data from different databases: one is a MySQL database and the other a MSSQL database.
What I want to do is match a selection of loan numbers from the MySQL database (about 82,000 loan numbers) to the corresponding information we have housed in the MSSQL database.
However, the tables in MSSQL to which I am joining the data from MySQL are much larger (~ 2 million rows), are quite wide, and thus cost much more time to query. Ideally I could perform an inner join between the two tables based on the loan number, but since they are in different databases this is not possible. The inner join that is performed inside a tMap occurs after the Lookup input has already returned its data set, which is quite large (especially since this particular MSSQL query will execute a user-defined function for each loan number).
Is there any way to create a global variable out of the output from the MySQL query (namely, the loan numbers selected by the MySQL query) and use that global variable as an IN clause in the MSSQL query?
This should be possible. I'm not working in MySQL but I have something roughly equivalent here that I think you should be able to adapt to your needs.
I've never actually answered a Stackoverflow question and while I was typing this the page started telling me I need at least 10 reputation to post more than 2 pictures/links here and I think I need 4 pics, so I'm just going to write it out in words here and post the whole thing complete with illustrations on my blog in case you need more info (quite likely, I should think!)
As you can see, I've got some data coming out of the table and getting filtered by tFilterRow_1 to only show the rows I'm interested in.
The next step is to limit it to just the field I want to use in the variable. I've used tMap_3 rather than a tFilterColumns because the field I'm using is a string and I wanted to be able to concatenate single quotes around it but if you're using an integer you might not need to do that. And of course if you have a lot of repetition you might also want to get a tUniqueRows in there as well to save a lot of unnecessary repetition
The next step is the one that does the magic. I've got a list like this:
'A1'
'A2'
'B1'
'B2'
etc, and I want to turn it into 'A1','A2','B1','B2' so I can slot it into my where clause. For this, I've used tAggregateRow_1, selecting "list" as the aggregate function to use.
Next up, we want to take this list and put it into a context variable (I've already created the context variable in the metadata - you know how to do that, right?). Use another tMap component, feeding into a tContextLoad widget. tContextLoad always has two columns in its schema, so map the output of the tAggregateRows to the "value" column and enter the name of the variable in the "key". In this example, my context variable is called MyList
Now your list is loaded as a text string and stored in the context variable ready for retrieval. So open up a new input and embed the variable in the sql code like this
"SELECT distinct MY_COLUMN
from MY_SECOND_TABLE where the_selected_row in ("+
context.MyList+")"
It should be as easy as that, and when I whipped it up it worked first time, but let me know if you have any trouble and I'll see what I can do.

Linq-to-Sql Count

I need to do a count on the items in a joined result set where a condition is true. I thus have a "from join where where" type of expression. This expression must end with a select or groupby. I do not need the column data actually and figure it is thus faster not to select it:
count = (from e in dc.entries select new {}).Count();
I have 2 questions:
Is there a faster way to do this in terms of the db load?
I have to duplicate my entire copy of the query. Is there a way to structure my query where I can have it one place for both counts and for getting say a list with all fields?
Thanks.
Please pay especial attention:
The query is a join and not a simple table thus I must use a select statement.
I will need 2 different query bodies because I do not need to load all the actual fields for the count but will for the list.
I assume when I use the select query it is filling up with data when I use query.Count vs Table.Count. Look forward to those who understand what I'm asking for possible better ways to do this and some detailed knowledge of what actually happens. I need to pull out the logging to look into this deeper.
Queryable.Count
The query behavior that occurs as a
result of executing an expression tree
that represents calling
Count(IQueryable)
depends on the implementation of the
type of the source parameter. The
expected behavior is that it counts
the number of items in source.
In fact, if you use LinqToSql or LinqToEntities, Queryable.Count() is sent into the database. No columns are loaded to memory. Check the generated sql to confirm.
I assume when I use the select query it is filling up with data when I use query.Count vs Table.Count
This is not true. Check the generated sql to confirm.
I have to duplicate my entire copy of the query. Is there a way to structure my query where I can have it one place for both counts and for getting say a list with all fields
If you need both the count and the list, get the list and count it.
If you need the count sometimes and other times you need the list... write a method that returns the complex IQueryable, and sometimes call .Count() and other times call .ToList();
I do not need the column data actually and figure it is thus faster not to select it.
This is basically false in your scenario. It can be true in a scenario where an index covers the result columns, but you don't have any result columns.
In your scenario, whatever index is chosen by the query optimizer, that index can be used to make the count.
Sum up: Query optimizer will perform the optimization you desire.
//you can put a where condition here
var queryEntries = from e in dc.entries select e;
//Get count
queryEntries.Count();
//Loop through Entries, so you basically returned all entries
foreach(entry en in queryEntries)
{}

send-object command not to send if no records in query

I want to use the macro' "send-object command" to email the records of a bunch of queries on a daily basis, but I only want it to send the given email if there are records in the given query. if the query doesnt output any records, then I dont want that email to be sent. I know how to accomplish this by using an Acces report by setting the "on no data event", but I would prefer that my macro doenst run the Access reports and just runs the queries , because outputting the data in reoprt format uses to much resources on my computer. please note that I do not know VBA, so I would like to know if I can accomplish my objective without any VB programming. thank you very much for your advi
In a macro, you can use the CONDITIONS column to test a DCount() expression to see if the results are >0 and then your SendObject will run.
To do this, open your Macro with the SendObject line.
From the VIEW menu, select CONDITIONS. This will add a column to the left.
In that column, put something like this:
DCount("*", "MyQuery", "[conditions that are being tested, if necessary]")>0
This is a DCount() to see how many records the query returns. You'd leave the third argument out (along with it's leading comma) if your query is already appropriately filtered (e.g., it has a reference to a form control as criterion). If the query returns 0, the CONDITION returns FALSE and your SendObject command won't execute.
You'll have to figure out how to construct the DCount(), but the point is that by testing how many records will display in the query, you can conditionally execute the SendObject.
Unfortunately you need VBA to accomplish this. Are you willing to put some code in ?