INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Export Subform results to Excel

Export Subform results to Excel

(OP)
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!

RE: Export Subform results to Excel

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 

RE: Export Subform results to Excel

(OP)
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?

RE: Export Subform results to Excel

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.

RE: Export Subform results to Excel

(OP)
Thanks!

The combo box and the subform is both on the main form. Do I need to put the code behind the export button?

RE: Export Subform results to Excel

2
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.

RE: Export Subform results to Excel

(OP)
Just an update, I was able to solve my issue with the sample above, thanks!

RE: Export Subform results to Excel

@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,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close