Loop to run a series of queries to append a table for each record in a different table. MS Access 2010 - for-loop

Here is what I need to do:
I have a series of queries that I use to create a final report with multiple columns of statistics for an item. I have a table set up with a key for each item. What I need to do is create a loop that runs the series of queries for an item and uses the final query to append the results to "report" table.
I have all of the Make Table queries set up and the final Append query. There are two queries that require the key, the rest of the queries are dependant on the results of the first two. So, I need to use that key as a parameter in those two queries. What I need is a loop that will for each key in the "Items" table run the queries on that key, then loop.
I have a module setup to run the complete set of queries, but right now I need to run it for each item. My thinking is that I need a "for i in items..." type loop, but I do not know how to do that in VBA
Using MS Access 2010

As far as I understand you want to run multiple queries where queries are saved in a table. Some of the queries depends on parent query.
pseudo logic:
Run and Execute Parent Query
If result exists retrieve value
Start loop until parent query has values
Retrieve/construct sub queries
Execute sub query
End loop
some pseudo code:
dim MyDb as Dao.Database
dim myRs as Dao.Recordset
set mydb = currentdb
dim SQL_PARENT as string
sql_parent = "" ' Retrieve your parent query here
sql_parent = vba.replace(sql_parent, "searchkey", "replace_key") ' Replace keys
' or if you have the keys in a table then use the dlookup() function to replace keys
set myrs = mydb.openRecordSet(sql_parent)
'do error trapping here
dim iResult = nz(myrs("ResultField"),"")
'follow the parent query operation
'construct the sub queries
dim RS_SUB as DAo.Recordset
dim SQL_SUB as string
dim I as integer
for i = 1 to total of your query collection
sql_sub = query_collection(i)
sql_sub = vba.replace(sql_sub,"searchkey",iResult)
set rs_sub = mydb.openrecordset(sql_sub)
next i
note: this is just a pseudo answer to help you to understand the possibilities to your scenario.

Related

add variable field value to output file name

I have a db that imports files in to temp tables prior to exporting the data to excel and appending the temp data to the main table. Each of the files loaded to the temp tables had a column with the same unique value for each record. I would like to create a query to used to filter on the first and add that value from the query to my file naming convention for the person that has to work the output files.
I've created the query that filters on the first but I am not able to find a way to incorporate that result into the file naming for the output.
Function Export_2nd_Level_Excel()
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "2ndLevel", "\pbfsvr01\pbf\PharmNetwork\Audits_Pharmacy\Provider_Audits\Completed Audit files\2nd_Level\" & "[Insert NPI]" & Format(Now, "_ddmmmyy""_2ndLevel_to_PharmAudit") & ".xlsx", True
End Function
I would like to have the "[Insert NPI]" be the actual value from the query that pulls the first of NPI.
after playing around with this for quite some time, I was able to make this work
Function Export_2nd_Level_Excel()
Set db = CurrentDb
Set recd = db.OpenRecordset("2nd_level_NPI")
Dim NPI As String
recd.MoveFirst
NPI = recd![NPI]
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "2ndLevel", "\pbfsvr01\pbf\PharmNetwork\Audits_Pharmacy\Provider_Audits\Completed Audit files\2nd_Level\" & NPI & Format(Now, "_ddmmmyy""_2ndLevel_to_PharmAudit") & ".xlsx", True
End Function

Write query which run on array by considring sub query in Parse

I need to write a query in such way that the array(collection) is contain only sub query objects.
Suppose we have the two tables as follows:
TableA:
objectId, name
TableB:
objectId, names[array of name: parse pointer collection]
Here is my code which I tried:
// sub query
var subQuery = new Parse.Query('TableA');
subQuery.doesNotExist('name');
// main query
var query = new Parse.Query('TableB');
query.exists("names");
//query.containsAll("names", subQuery); // this means names should contain all subQuery, so this is not use full for me.
query.matchesQuery("names", subQuery);
This code is running fine, but this is not working as I want and also not showing the any error.
It seems that you don't need a subquery per se, but rather to first query your list of names, and then use that in your main query. What you seem to be looking for is: containedIn( key, values ) , as in:
query.containedIn("name", namesFromFirstQuery)

Add multiple records in ADO .Net without an iterator

I am working on a legacy ADO .Net code in VB6. The current code iterates through a dictionary and creates a new record for the RecordSet, rs, for each key in the dictionary. This is the current code (rs is a record set):
For Each k In dictionary.Keys
rs.AddNew
rs(field1) = value1
rs(field2) = dictionary(k)
rs.Update
Next
I want to optimize the above code and add the records in a batch. Something like this:
Dim fieldOne As New Collection
Dim fieldTwo As New Collection
Dim valueOne As New Collection
Dim valueTwo As New Collection
For Each k In dictionary.Keys
fieldOne.Add (field1)
fieldTwo.Add (field2)
valueOne.Add (value1)
valueTwo.Add (dictionary(k))
Next
rs.AddNew Array(fieldOne, fieldTwo), Array(valueOne, valueTwo)
rs.UpdateBatch
Is it possible to create multiple records in one Add statement in ADO .Net RecordSet? How can I bypass the iterator in such a situation, where not only I have multiple fields, but the fields have multiple values as well?

Query Disconnected RecordSet

Background: a LOB app we use has the ability to use macros written in VBScript, but no access to WScript, etc., as far as I know.
I have successfully received user input, passed it to a stored procedure on a SQL Server, and returned a recordset to the VBScript macro in the application.
What I want to do now, is write a function or loop or something, that for as long as there is a record left in the recordset, accept additional user input, and check this against the returned recordset.
The recordset returned from SQL Server contains two columns: PART_ID and PART_QTY. For as many number of entries there are, I want to accept additional user input, lets say PART_ID_INPUT and PART_QTY_INPUT, and validate it against the in-memory recordset.
My biggest problem is working with the disconnected recordset.
When in doubt, read the documentation. You can use the Filter and RecordCount properties to determine if the recordset contains matching records:
part_id_input = InputBox("Enter part ID:")
If part_id_input <> "" Then
rs.Filter = "PART_ID = '" & part_id_input & "'"
If rs.RecordCount > 0 Then WScript.Echo "Found matching record."
End If
The filter is cleared by setting it to an empty string:
rs.Filter = ""
The current record can be removed from the recordset using the Delete method:
rs.Delete
Navigate through records via MoveFirst/MoveLast/MoveNext/MovePrevious.

"Query is not understandable" - Full text searching where field types have changed

A client have a long lived IBM Notes application where someone along the line changed the type of a field from number to text.
So, now when we're trying to do an FT search like: [myField] = "1234" receive the error message: "Query is not understandable".
If I do: [myField] = 1234 it works but won't return any hits. Even though there's a document where myField = "1234".
The field is of type text in the design.
I've created a new view for testing, only allowing documents from one form.
Deleted the full text index (even on the file system)
updall -X
Fixup -r
Created full text index
In my test view I've got one column that shows if the field content being searched is of type text #IsText(myField) and all rows shows: 1 (so it's field content must be text)
None of the above worked so I created a new database copy locally.
Same problem.
Created an entirely new database (for testing only), form, view and full text index and that works.
Feels like the existing database design somewhere stores the old field type...
Any ideas appreciated.
Thanks!
/J
Datatypes and field names are stored in the UNK table. There is just one entry per field name, so it's critical not to use the same field name more than once in an application with different datatypes.
You need to rebuild the UNK table, as I blogged here http://www.intec.co.uk/full-text-search-musings/
Note, it must be an offline compact, as Duffbert says here http://www.duffbert.com/duffbert/blog.nsf/d6plinks/TDUF-5SMHV4. If anyone is in the database when you do the compact, it will fail and the UNK table will not be rebuilt.
Links are useful, but if you don't want to remove data from documents - for me such steps worked (and there was no need in removing fields from forms in designer):
Run from designer with manager access with such code inside
Sub Initialize
Dim s As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim i As Integer
Dim nc As NotesNoteCollection
Dim noteid As String
Dim nextnoteid As string
Dim itemArr As Variant
Dim NeedSave As Boolean
Const ITEM_NAME = "itemName1|itemName2"
itemArr = Split( ITEM_NAME, "|" )
'погромист-кун не должен забывать про наличие итемов в формах...
Set db = s.Currentdatabase
Set nc = db.CreateNoteCollection(False)
nc.SelectForms = true
Call nc.BuildCollection
noteid = nc.Getfirstnoteid()
For i = 1 To nc.Count
Set doc = db.Getdocumentbyid( noteid )
noteid = nc.Getnextnoteid( noteid )
NeedSave = false
ForAll IA In itemArr
If doc.Hasitem( IA ) Then
Call doc.Removeitem( IA )
NeedSave = true
End If
End ForAll
If NeedSave Then
Call doc.Save( True, False )
End If
Print CStr( i ) & "\" & CStr( nc.Count )
Next
End Sub
Remove database index
Run from administrator command lo compact database.nsf -c , like mentioned in links above
Create index

Resources