Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / October 2008

Tip: Looking for answers? Try searching our database.

export form filtered data to excel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ryan.fitzpatrick3@safeway.com - 08 Oct 2008 18:36 GMT
Is there away to export a filtered selection of a table (which is on a
form) to excel? I'm reading some of the posts, and it appears I would
need to have the filtered selection dump into a query then export it.
Is that correct? What would be the best approach?
Douglas J. Steele - 08 Oct 2008 19:49 GMT
You don't dump data into queries: queries are how you extract data from
tables.

Create a query with the appropriate Where clause and export the query to
Excel. You should never be working directly with tables anyhow.

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

> Is there away to export a filtered selection of a table (which is on a
> form) to excel? I'm reading some of the posts, and it appears I would
> need to have the filtered selection dump into a query then export it.
> Is that correct? What would be the best approach?
ryan.fitzpatrick3@safeway.com - 08 Oct 2008 21:48 GMT
So i'm straight, my form pulls from a table, you're saying have a
query pull that table and have the form pull that query, then i can
export that query into excel right?

On Oct 8, 11:49 am, "Douglas J. Steele"
<NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> You don't dump data into queries: queries are how you extract data from
> tables.
[quoted text clipped - 16 lines]
>
> - Show quoted text -
ryan.fitzpatrick3@safeway.com - 09 Oct 2008 00:59 GMT
OK I got the export vba I think. this code exports the query but the
whole thing and not the filtered selection on the form.

Private Sub Command84_Click()

On Error GoTo Err_Command84_Click

   DoCmd.TransferSpreadsheet transfertype:=acExport, _
   spreadsheettype:=acSpreadsheetTypeExcel9, _
   tableName:="QryAdageVolumeSpend", FileName:= _
   "C:\Documents and Settings\rfitz03\My Documents\AdageData.xls", _
   hasfieldnames:=True

Exit_Command84_Click:

   Exit Sub

Err_Command84_Click:
   MsgBox Err.Description
   Resume Exit_Command84_Click

End Sub

I changed the form source from table to query. I made a query of the
table and made the form pull that instead. How do I link the form to
the query? When I linked up the combo boxes to the query and tried to
select an item it gave me an error. Error: You cannot add new clients
to this search form. Permission denied. What’s this mean? How do I get
the query to have the same results as the form, so when I export with
the code above it’ll give me the filtered list and not all 70k
records?

On Oct 8, 11:49 am, "Douglas J. Steele"
<NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> You don't dump data into queries: queries are how you extract data from
> tables.
[quoted text clipped - 16 lines]
>
> - Show quoted text -
Douglas J. Steele - 09 Oct 2008 12:25 GMT
One option would be to generate the appropriate SQL, save it to a temporary
query and export the temporary query.

Since you're currently using a query, you could try something like:

Private Sub Command84_Click()

On Error GoTo Err_Command84_Click

Dim dbCurr As DAO.Database
Dim qdfTemp As DAO.QueryDef
Dim lngOrderBy As Long
Dim strQueryName As String
Dim strSQL As String

' You only need to go to this effort if there's a filter
 If Len(Me.Filter) > 0 Then
   Set dbCurr = CurrentDb

' Get the SQL for the existing query
   strSQL = dbCurr.QueryDefs("QryAdageVolumeSpend").SQL

' Check whether there's an ORDER BY clause in the SQL.
' If there is, we need to put the WHERE clause in front of it.
   lngOrderBy = InStr(strSQL, "ORDER BY")
   If lngOrderBy > 0 Then
     strSQL = Left(strSQL, lngOrderBy - 1) & _
       " WHERE " & Me.Filter & " " & _
       Mid(strSQL, lngOrderBy)
   Else
' There's no ORDER BY in the SQL.
' Remove the semi-colon from the end, then append the WHERE clause
     strSQL = Left(strSQL, Len(strSQL) - 1) & _
       " WHERE " & Me.Filter
   End If

' By using the current date and time, hopefully that means
' a query by that name won't already exist
   strQueryName = "qryTemp" & Format(Now, "yyyymmddhhnnss")

' Create the temporary query
   Set qdfTemp = dbCurr.CreateQueryDef(strQueryName, strSQL)

' Export the temporary query
   DoCmd.TransferSpreadsheet transfertype:=acExport, _
     spreadsheettype:=acSpreadsheetTypeExcel9, _
     tableName:=strQueryName, FileName:= _
     "C:\Documents and Settings\rfitz03\My Documents\AdageData.xls", _
     hasfieldnames:=True

' Delete the temporary query
   dbCurr.QueryDefs.Delete strQueryName

 Else

   DoCmd.TransferSpreadsheet transfertype:=acExport, _
     spreadsheettype:=acSpreadsheetTypeExcel9, _
     tableName::="QryAdageVolumeSpend", FileName:= _
     "C:\Documents and Settings\rfitz03\My Documents\AdageData.xls", _
     hasfieldnames:=True

 End If

Exit_Command84_Click:
 Set dbCurr = Nothing
 Exit Sub

Err_Command84_Click:
 MsgBox Err.Description
 Resume Exit_Command84_Click

End Sub

Note that I've only handled simple queries there: a SELECT with no WHERE
clause but (possibly) an ORDER BY clause. You know what QryAdageVolumeSpend
looks like: you may have to modify the code. Of course, you also have the
option of simply putting the SQL there, as opposed to retrieving the SQL
from QryAdageVolumeSpend and working with it.

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

OK I got the export vba I think. this code exports the query but the
whole thing and not the filtered selection on the form.

Private Sub Command84_Click()

On Error GoTo Err_Command84_Click

   DoCmd.TransferSpreadsheet transfertype:=acExport, _
   spreadsheettype:=acSpreadsheetTypeExcel9, _
   tableName:="QryAdageVolumeSpend", FileName:= _
   "C:\Documents and Settings\rfitz03\My Documents\AdageData.xls", _
   hasfieldnames:=True

Exit_Command84_Click:

   Exit Sub

Err_Command84_Click:
   MsgBox Err.Description
   Resume Exit_Command84_Click

End Sub

I changed the form source from table to query. I made a query of the
table and made the form pull that instead. How do I link the form to
the query? When I linked up the combo boxes to the query and tried to
select an item it gave me an error. Error: You cannot add new clients
to this search form. Permission denied. What’s this mean? How do I get
the query to have the same results as the form, so when I export with
the code above it’ll give me the filtered list and not all 70k
records?

On Oct 8, 11:49 am, "Douglas J. Steele"
<NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> You don't dump data into queries: queries are how you extract data from
> tables.
[quoted text clipped - 16 lines]
>
> - Show quoted text -
ryan.fitzpatrick3@safeway.com - 09 Oct 2008 17:07 GMT
I copied your code in and got a error. "Characters found at end of

On Oct 9, 4:25 am, "Douglas J. Steele"
<NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> One option would be to generate the appropriate SQL, save it to a temporary
> query and export the temporary query.
[quoted text clipped - 139 lines]
>
> - Show quoted text -
ryan.fitzpatrick3@safeway.com - 09 Oct 2008 17:09 GMT
I got an error saying "characters found at end of SQL statement".

On Oct 9, 4:25 am, "Douglas J. Steele"
<NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> One option would be to generate the appropriate SQL, save it to a temporary
> query and export the temporary query.
[quoted text clipped - 139 lines]
>
> - Show quoted text -
Douglas J. Steele - 10 Oct 2008 01:02 GMT
Try changing

Else
' There's no ORDER BY in the SQL.
' Remove the semi-colon from the end, then append the WHERE clause
 strSQL = Left(strSQL, Len(strSQL) - 1) & _
   " WHERE " & Me.Filter
End If

to

Else
' There's no ORDER BY in the SQL.
' Remove the semi-colon from the end, then append the WHERE clause
 strSQL = Left(strSQL, InStr(strSQL, ";") - 1) & _
   " WHERE " & Me.Filter
End If

Or, better yet, look at the actual SQL for your query and see what's
required.

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)

I got an error saying "characters found at end of SQL statement".

On Oct 9, 4:25 am, "Douglas J. Steele"
<NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> One option would be to generate the appropriate SQL, save it to a
> temporary
[quoted text clipped - 141 lines]
>
> - Show quoted text -
ryan.fitzpatrick3@safeway.com - 10 Oct 2008 17:22 GMT
You are the man, it worked!!!!!!!!!1

On Oct 9, 5:02 pm, "Douglas J. Steele"
<NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> Try changing
>
[quoted text clipped - 176 lines]
>
> - Show quoted text -
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.