Take a look at the example, Heidi
It assumes:
a) You added a command button to your form for the export, and set the
button's Name to cmdExport.
b) You set the button's On Click property to:
[Event Procedure]
Then you click the Build button (...) beside this.
Access opens the code window.
c) You set up the code as in the example.
A QueryDef is just a saved query (i.e. the definition of the of the query,
not the actual data it returns.) It is defined as a SQL statement, so the
task of your code is to put the right WHERE clause into the middle of the
SQL statement.
To do this, you will look at the SQL statement in SQL View. The stub of the
SQL statement (everything before the WHERE) goes into the constant we named
strcStub at the top. The tail of the SQL statement (everything after the
WHERE clause), goes in the constant we named strcTail. Then you take
whatever is in the subform's Filter property, and use that as the WHERE
clause in the middle of the SQL statement.
Here's the example again:
Private Sub cmdExport_Click()
Dim strWhere As String
Dim strFile As String
Const strcStub = "SELECT F1, F2, F3 ... " & vbCrLf & _
"FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID " & vbCrLf
Const strcTail = " ORDER BY F1, F2;"
With Me.Sub1.Form
If .FilterOn Then
strWhere = "WHERE " & .Filter & vbCrLf
End If
End With
CurrentDb.QueryDefs("qryExport").SQL = strcStub & strWhere & strcTail
strFile = "C:\MyFolder\MyFile.xls"
DoCmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel8, _
"qryExport", strFile
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Reply to group, rather than allenbrowne at mvps dot org.
heidii said:
Hi Allen:
I don't know how to do this part
3. In the Click event of the command button, concatenate the
subform's
Filter property into the WHERE clause, and assign the whole thing to
the SQL
property of the QueryDef you saved.
Heidi