Your query shouldn't pose a problem, but one slight amendment you can make is
to use a WHERE clause rather than a HAVING clause. The latter will work, but
is not really appropriate in this case; it results from using a 'group by'
the column in query design view to apply a criterion rather than applying the
criterion in a separate 'where' column in the query design grid. A HAVING
clause operates on the data after its grouped whereas a WHERE clause operates
before its grouped. The former is usually used to restrict a query on the
result of an aggregation of values. You also seem to have a join criterion
(ImportAddresses.Type =[Zips].[Type]) in the HAVING clause, which looks to me
as though it should be in the JOIN clause. Also don't use an ORDER BY clause
in a query which is used as a report's RecordSource. Instead sort the report
by means of its internal sorting and grouping mechanism (from the
View|Sorting and Grouping menu item in report design view). With these
amendments the string expression for the report's RowSource can be built like
so:
strRS = "SELECT Business.BusinessName, ImportAddresses.Name, "
strRS = strRS & "ImportAddresses.Add1, ImportAddresses.City,
ImportAddresses.State, "
strRS = strRS & "ImportAddresses.Zip, Zips.Type, Zips.CustomerNumber "
strRS = strRS & "FROM (Business INNER JOIN Zips "
strRS = strRS & "ON Business.CustomerNumber = Zips.CustomerNumber) "
strRS = strRS & "INNER JOIN ImportAddresses "
strRS = strRS & "ON Zips.ZipCode = ImportAddresses.Zip "
strRS = strRS & "AND ImportAddresses.Type =Zips.Type "
strRS = strRS & "WHERE Zips.CustomerNumber=" & lngID & " "
strRS = strRS & "GROUP BY Business.BusinessName, ImportAddresses.Name, "
strRS = strRS & "ImportAddresses.Add1, ImportAddresses.City,
ImportAddresses.State, "
strRS = strRS & "ImportAddresses.Zip, Zips.Type, Zips.CustomerNumber, "
strRS = strRS & "ImportAddresses.Type"
This assumes CustomerNumber is a long integer number data type, not a text
string.
Regarding Tony's point a.MDE file is a version specific form of the original
source .MDB file in which only compiled VBA code is present. The database is
smaller and to some extent secure from 'meddlers'. In a .MDE file object
definitions cannot be amended. What I'd probably do in a case like that is
make the RecordSource of the report a query which references as a parameter
on the CustomerNumber column a hidden control on a dialogue form from which
the snapshots are generated. You then simply need to assign the
CustomerNumber to the control in each iteration through the recordset of
customers before each call of the OututTo method. No amendment of the report
definition would be required.
Ken Sheridan
Stafford, England
vman92 said:
Thanks Ken,
It looks like it might work. I do however have a little more complex
SQL.
I copied this right from the SQL behind the report. I don't really
think it is formatted
properly for VBA.
strRS =
"SELECT Business.BusinessName, ImportAddresses.Name,
ImportAddresses.Add1, ImportAddresses.City, ImportAddresses.State,
ImportAddresses.Zip, Zips.Type, Zips.CustomerNumber" & _
"FROM (Business INNER JOIN Zips ON Business.CustomerNumber =
Zips.CustomerNumber)" & _
"INNER JOIN ImportAddresses ON Zips.ZipCode = ImportAddresses.Zip" & _
"GROUP BY Business.BusinessName, ImportAddresses.Name,
ImportAddresses.Add1, ImportAddresses.City, ImportAddresses.State,
ImportAddresses.Zip, Zips.Type, Zips.CustomerNumber,
ImportAddresses.Type" & _
"HAVING (((Zips.CustomerNumber)=" & lngID & ";"") AND
((ImportAddresses.Type)=[Zips].[Type]))" & _
"ORDER BY Business.BusinessName, ImportAddresses.Name;"