Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export Subform results to Excel 3

Status
Not open for further replies.

pinkpoppy

Programmer
Jul 6, 2011
75
0
0
US
Hello,

I am modifying a database that was created by someone else. The database has a form which a user can choose from a combobox and the result is then display in a subform.

Combobox1 has a query behind it. when the user clicks on it, it shows in the dropdown the available last,first name searchable information.

There is a form called "frmSubContact" which runs a query from 2 tables and it is a one to many relationship. This frmSubContact form is placed n the main form. But on the main form, this frmSubContact also runs a query to display data when the user selects from the combobox. The Link Master Field is bound to the combobox1 and the link child field is bound to 'last name'.

I've searched on how to export subform results to excel but the codes will export all of the query data from the frmSubContact, not the data that was selected from the combobox.

For example, frmSubContact has a total of 200 records. When I select the last name "Adams", the subform on the main form will display 3 records of the last name Adams. When I export, I only want to see in excel, the 3 Adam records. The excel just needs to pop up on the screen.

How can I export the results from the subform on the main form when a user selects from a combobox??

Any help or suggestions is much appreciated!

 
Create your own query definition based off of the link as the filter for the where statement. Something like.

Code:
Dim strWhere As String
    Dim strFile As String
    Const strcStub = "SELECT Table1.* FROM Table1 " & vbCrLf
    Const strcTail = "ORDER BY SomeField;"
    Const strcExportQuery = "Query1"    'Name of the query for exports.

    strWhere = "WHERE LastName ='" & me.parent.combo1 & "'" &  vbCrLf
    dbEngine(0)(0).QueryDefs(strcExportQuery).SQL = strcStub & strWhere & 
strcTail

    strFile = "C:\Data\MyExport.xls"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
        strcExportQuery, strFile
 
Thank you for the suggestion. I've tried your suggestion and ran into an issue.

I implement the code behind the command for the export button correct?
 
The problem will likely depend on where you run the code.
If the code is on the main form and the combo is on the main form the code would be
strWhere = "WHERE LastName ='" & me.combo1 & "'" & vbCrLf
if on the subform then
strWhere = "WHERE LastName ='" & me.parent.combo1 & "'" & vbCrLf

What is the problem? Error message, nothing happens, etc?

Also this code is an example and will have to be edited for your means.
 
Thanks!

The combo box and the subform is both on the main form. Do I need to put the code behind the export button?
 
Normally I put the majority of my code in a separate procedure unless it is really really basic. That way I can call it from multiple places. Maybe I want to call it when I push a button but later I want something else or some additional events to trigger the code. So I just write

Public ExportSubToExcel ()
code here
end Sub

Then you could have lots of events that call ExportSubToExcel

Private SomeButton_Click()
ExportSubToExcel
end Sub

Private SomeControl_SomeEvent()
ExportSubToExcel
end sub

"Do I need to put the code behind the export button?" That is therefore not really correct terminology. Code is placed in the module of the form. In this case you could place it in the mainform module or subform module. If the command button is on the main form the code goes in the mainform module. If the command button is on the subform the code would go in the subform module. I assume the button is on the main form. So on the click button event of the command button type

Private SomeButton_Click()
ExportSubToExcel
end Sub

Then in that same module add your procedure

Public ExportSubToExcel ()
code here
end Sub

You do not have to divide it up like this and all the code could go in the command button event procedure, but it is good technique so that you can reuse code and call from anywhere.
 
Just an update, I was able to solve my issue with the sample above, thanks!
 
@pinkpoppy, you've been here at Tek-Tips nearly 5 years, started 30 threads and received many good tips, like those submitted by MajP here.

Although you just posted a "thanks", you have never thanked anyone with a little purple star. You've undoubtedly noticed these as you browse and not hey are a means of identifying threads that contain good stuff.

You might want to do that for MajP here.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top