dbTalk Databases Forums  

Exporting Form Filtered Results

comp.databases.ms-access comp.databases.ms-access


Discuss Exporting Form Filtered Results in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
musicloverlch
 
Posts: n/a

Default Exporting Form Filtered Results - 12-19-2011 , 12:46 PM






So, I'm using my favorite search code from here:
http://allenbrowne.com/ser-62code.html

Once I have everything filtered on the form, the users want the
ability to export that data to Excel. Unfortunately that exports
everything.

How can I export the filtered results? I'm at a loss.

Thanks in advance,
Laura

Reply With Quote
  #2  
Old   
Patrick Finucane
 
Posts: n/a

Default Re: Exporting Form Filtered Results - 12-20-2011 , 03:15 PM






On Dec 19, 12:46*pm, musicloverlch <lho... (AT) gmail (DOT) com> wrote:
Quote:
So, I'm using my favorite search code from here:http://allenbrowne.com/ser-62code.html

Once I have everything filtered on the form, the users want the
ability to export that data to Excel. *Unfortunately that exports
everything.

How can I export the filtered results? I'm at a loss.

Thanks in advance,
Laura
Maybe this will get you started. Drop the following code in a Module.

In your form's code module enter
ExportFilteredTest Me.Name
to call the code. This will create an Excel file called Text.Xlsx

Public Sub ExportFilteredTest(frm As Form)
Dim q As QueryDef
Dim s As String
Dim d As Database

On Error GoTo 0
On Error Resume Next

Set d = CurrentDb
Set q = d.QueryDefs("MyRecordSource")

'get rid of existing queries
DoCmd.DeleteObject acQuery, "MyRecordSource"
DoCmd.DeleteObject acQuery, "MyRecordSourceResult"

'recreate the queries that becomes the Excel recordsource
s = frm.RecordSource
If Left(s, 7) <> "Select " Then
'the form;s recordsource is a table. Make it into a query
s = "Select " & s & ".* From " & s
End If
Set q = d.CreateQueryDef("MyRecordSource", s)

'next, create a query with the recordsource's filters and sorts
s = "Select MyRecordSource.* From MyRecordSource "
If frm.FilterOn Then
'now add the filter
s = s & "Where " & frm.Filter
End If
If frm.OrderByOn Then
'now add the sort order
s = s & " Order By " & frm.Filter
End If
Set q = d.CreateQueryDef("MyRecordSourceResult", s)
d.close
set d = Nothing

On Error GoTo 0 'if you want, turn on error checking here.
Modify result folder/file name
DoCmd.TransferSpreadsheet acExport, , "MyRecordSourceResult", "C:
\Test\Test.xlsx", True

Msgbox "Done exporting"
End Sub

Reply With Quote
  #3  
Old   
musicloverlch
 
Posts: n/a

Default Re: Exporting Form Filtered Results - 01-12-2012 , 11:22 AM



OMG!!! It works like a charm!!!

Thank you soooo much!

Eternally grateful,

Laura





On Dec 20 2011, 3:15*pm, Patrick Finucane
<patrickfinucan... (AT) gmail (DOT) com> wrote:
Quote:
On Dec 19, 12:46*pm, musicloverlch <lho... (AT) gmail (DOT) com> wrote:

So, I'm using my favorite search code from here:http://allenbrowne.com/ser-62code.html

Once I have everything filtered on the form, the users want the
ability to export that data to Excel. *Unfortunately that exports
everything.

How can I export the filtered results? I'm at a loss.

Thanks in advance,
Laura

Maybe this will get you started. *Drop the following code in a Module.

In your form's code module enter
* ExportFilteredTest Me.Name
to call the code. *This will create an Excel file called Text.Xlsx

Public Sub ExportFilteredTest(frm As Form)
* * Dim q As QueryDef
* * Dim s As String
* * Dim d As Database

* * On Error GoTo 0
* * On Error Resume Next

* * Set d = CurrentDb
* * Set q = d.QueryDefs("MyRecordSource")

* * 'get rid of existing queries
* * DoCmd.DeleteObject acQuery, "MyRecordSource"
* * DoCmd.DeleteObject acQuery, "MyRecordSourceResult"

* * 'recreate the queries that becomes the Excel recordsource
* * s = frm.RecordSource
* * If Left(s, 7) <> "Select " Then
* * * * 'the form;s recordsource is a table. *Make it into a query
* * * * s = "Select " & s & ".* From " & s
* * End If
* * Set q = d.CreateQueryDef("MyRecordSource", s)

* * *'next, create a query with the recordsource's filters and sorts
* * s = "Select MyRecordSource.* From MyRecordSource "
* * If frm.FilterOn Then
* * * * 'now add the filter
* * * * s = s & "Where " & frm.Filter
* * End If
* * If frm.OrderByOn Then
* * * * 'now add the sort order
* * * * s = s & " Order By " & frm.Filter
* * End If
* * Set q = d.CreateQueryDef("MyRecordSourceResult", s)
* * d.close
* * set d = Nothing

* * On Error GoTo 0 *'if you want, turn on error checking here.
Modify result folder/file name
* * DoCmd.TransferSpreadsheet acExport, , "MyRecordSourceResult", "C:
\Test\Test.xlsx", True

* * Msgbox "Done exporting"
End Sub

Reply With Quote
  #4  
Old   
Patrick Finucane
 
Posts: n/a

Default Re: Exporting Form Filtered Results - 01-13-2012 , 09:16 AM



On Jan 12, 11:22*am, musicloverlch <lho... (AT) gmail (DOT) com> wrote:
Quote:
OMG!!! It works like a charm!!!

Thank you soooo much!

Eternally grateful,

Laura

On Dec 20 2011, 3:15*pm, Patrick Finucane



patrickfinucan... (AT) gmail (DOT) com> wrote:
On Dec 19, 12:46*pm, musicloverlch <lho... (AT) gmail (DOT) com> wrote:

So, I'm using my favorite search code from here:http://allenbrowne.com/ser-62code.html

Once I have everything filtered on the form, the users want the
ability to export that data to Excel. *Unfortunately that exports
everything.

How can I export the filtered results? I'm at a loss.

Thanks in advance,
Laura

Maybe this will get you started. *Drop the following code in a Module..

In your form's code module enter
* ExportFilteredTest Me.Name
to call the code. *This will create an Excel file called Text.Xlsx

Public Sub ExportFilteredTest(frm As Form)
* * Dim q As QueryDef
* * Dim s As String
* * Dim d As Database

* * On Error GoTo 0
* * On Error Resume Next

* * Set d = CurrentDb
* * Set q = d.QueryDefs("MyRecordSource")

* * 'get rid of existing queries
* * DoCmd.DeleteObject acQuery, "MyRecordSource"
* * DoCmd.DeleteObject acQuery, "MyRecordSourceResult"

* * 'recreate the queries that becomes the Excel recordsource
* * s = frm.RecordSource
* * If Left(s, 7) <> "Select " Then
* * * * 'the form;s recordsource is a table. *Make it into a query
* * * * s = "Select " & s & ".* From " & s
* * End If
* * Set q = d.CreateQueryDef("MyRecordSource", s)

* * *'next, create a query with the recordsource's filters and sorts
* * s = "Select MyRecordSource.* From MyRecordSource "
* * If frm.FilterOn Then
* * * * 'now add the filter
* * * * s = s & "Where " & frm.Filter
* * End If
* * If frm.OrderByOn Then
* * * * 'now add the sort order
* * * * s = s & " Order By " & frm.Filter
* * End If
* * Set q = d.CreateQueryDef("MyRecordSourceResult", s)
* * d.close
* * set d = Nothing

* * On Error GoTo 0 *'if you want, turn on error checking here.
Modify result folder/file name
* * DoCmd.TransferSpreadsheet acExport, , "MyRecordSourceResult", "C:
\Test\Test.xlsx", True

* * Msgbox "Done exporting"
End Sub- Hide quoted text -

- Show quoted text -
In my OrderBy code change
s = s & " Order By " & frm.Filter
to
s = s & " Order By " & frm.OrderBy

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.