Option Compare Database
option Explicit <----- ALWAYS ALWAYS ALWAYS
add this to your code...
Do While rstDist.EOF = False
rstDist???? Where did you define this variable?????
(you have to define all varabiles..
eg:
dim rstDist as dao.RecordSet
strSql = "SELECT MAIN_DATA_FORMAT.* FROM Disti_List LEFT JOIN
MAIN_DATA_FORMAT ON
(Disti_List.Distributor=MAIN_DATA_FORMAT.Distributor) AND
(Disti_List.RES_COUNTRY=MAIN_DATA_FORMAT.RES_COUNTRY) Where
RES_Country = MyCountry() and Distributor = MyDist()"
Do you really need a join in the above? Furthermore, the above is WHERE WE
ARE TO GET the "list" OF LEGAL countries and distributor names. This is NOT
our query we going to use for the final output (data export).
So ALL WE want here is a SIMPLE LIST of dist and country names to "process".
I don't rally see the need for the "join" here....do you?? The above
"conditions" = MyDist() etc. is to be placed in the ACTUAL query we going to
use for export.
So, our the pseudo code is:
1) Build a simple list using SQL of our countries and distributors which we
want to export for.
2) for each iteration of the above loop we will set the distributor +
country, and then execute a transfer spreadsheet.
So, we likely will build this list of distributor + countries in SQL. This
SQL will not have any conditions in it, and most likey will not be joined to
other tables. Also, as a note in place of pasting that big messy junk of SQL
into your code, simply use the query builder and execute code to grab the
data from the query builder
eg:
set rst = currentdb.QueryDefs("name of query").Execute
The above means you don't have to have all that messy sql in your code.
If you don't plan to use a query as above, then in your example code you
left out the loading of the reocrdset. eg, you must go:
set rstDist = currentdb.OpenrecordSet(strSql)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"qryIJoined", strOUtfile, True
The above looks ok. Note in the above, it is assumed you built a query
called "qryIJoined". It is this query that will have the above conditions
that were feeding via the looping code. For each iteration of the loop, we
will send(set) the distributor, and country, and then execute a
TransferSpreadsheet that has the sql based on these conditions. This goes
back to your original question as to how you put variables in the SQL
example. We are placing (setting) the distributor + country for each loop,
and the transferSpreadsheet will thus use this "new" sql with the
conditions.
I assume that you built a query called "qryIJoined" in the query builder?
(and, it has the two condstions it it????).
also keep mind that the two functions we make must go in a standard code
module, and cannot be placed in a form's module
eg:
Option Compare Database
Public gblDist As String
Public gblCountry As String
Public Function MyDist() As String
MyDist = gblDist
End Function
Public Function MyCountry() As String
MyCountry = gblCountry
End Function
the other code you have can be placed behind a button on a form.....but, the
above funcitons are GLOBAL and must be placed in a standard code module
before the SQL will see these values.