Ok - I have a database with 19 date fields each of which through their field names need to be incorporated into a report only if they are between certain dates - selected via a clever calendar (found on this forum) and temporarily held as unbound fields on the form CARinterim along with the customer name and 19 field name labels (read from an Array as hard coded text in a module) - too much like hard work to caption each individual label, also messy to change.
There was added requirement of a from .. to dates filter.
This was straightforward to do with 19 Select queries designed in QBE with the results then Appended to a new table. Each Query then appends to the table - up to 19 times in all if the date criteria is passed.
The "Customer Audit" report is then produced in the normal way from this Table.
What I wanted to do was avoid having 19 select Queries and 19 Append Queries each only differing by the field name number e.g. SSI1date, SSI2date, SSI3date etc. By using the Docmd.RunSQL command in VB I hoped to be able to progressively Loop the code 19 times with the date field name being in the syntax [SSI "& iVar & date"]etc.
Here is the code that works:
For iVar = 1 to 19
mySQL = "INSERT INTO [Customer Audit report] ( ClientNumber, nbCustomer, ClientType, JobDate, JobID )" & _
"SELECT Contacts.number, Contacts.nbCustomer, Contacts.ClientType, Contacts.[SSI" & iVar & "date], Forms.[F-CARinterim].[text" & iVar & "] AS [SSItask]" & _
"FROM [Contacts]" & _
"WHERE (((Contacts.nbCustomer) = [Forms].[F-CARinterim].[txtnbCustomer]) And ((Contacts.ClientType) = 'S') And ((Contacts.[SSI" & iVar & "date]) >= [Forms].[F-CARinterim].[txtStartDate] And (Contacts.[SSI" & iVar & "date]) <= [Forms].[F-CARinterim].[txtEndDate]))" & _
"ORDER BY Contacts.number;"
DoCmd.RunSQL mySQL
Next iVar
As mentioned in my earlier post, I thought I needed to do a Union query but found a solution by not having the Append Query getting its source data from a Select query as two separate queries. By converting the Select directly into an Append, I could get the SQL syntax to then copy into mySQL VB statement.
Any comments would be welcome.