Solved

Import date from Access to Excel

Posted on 2014-07-30
10
377 Views
Last Modified: 2014-08-11
Hi,
I would like to export data from an Access filter form to excel.
I want to use "RecordSource" instruction.

Here my code:

Set ExportQuery = CurrentDb.CreateQueryDef("ExportQuery", Me.RecordSource & " WHERE " & Me.Filter)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "ExportQuery", "c:\Report\test.xlsx", False

Any idea why that doesn't work?

Thanks
0
Comment
Question by:Karl001
  • 6
  • 3
10 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40228818
check the string content of

Me.RecordSource

debug.print Me.RecordSource

copy and paste here
0
 

Author Comment

by:Karl001
ID: 40228849
SELECT tb_Client1_Group.idGroupClient1, tb_Client1_Group.Group, tb_Client1_Group.no, IIf([tb_Client1_Group].[idGroupClient1]=1,Null,[tb_Client1_Group].[Group] & "-" & [tb_Client1_Group].[no]) AS instTrans, tb_Client1_Group.instNom, [tb_Client1_Group].[instCodePostal] & ",   " & [tb_Client1_Group].[instAdresse1] & ",   " & [tb_Client1_Group].[instVille] & ",   " & [tb_Client1_Group].[instAdresse2] AS instAdresse, tb_Client1_Group.instAdresse1, tb_Client1_Group.instAdresse2, tb_Client1_Group.instVille, tb_Client1_Group.instCodePostal, tb_Client1_Group.instTelephone, tb_Client1_Group.idREGION, tb_Client1_REGION.REGIONNo, IIf([tb_Client1_REGION].[REGIONNo]=1,Null,[tb_Client1_REGION].[REGIONNo]) AS Region, tb_Client1_VPR.idVPR, tb_Client1_VPR.VPRNo, IIf([tb_Client1_VPR].[VPRNo]=1,Null,[tb_Client1_VPR].[
VPRNo]) AS VPR, tb_Client1_Group.instInactif, IIf([tb_Client1_Group].[instInactif]=True,"Inactive",Null) AS statut, IIf(IsNull([Inst]),"Oui","Non") AS etabCamp, rq_InstAvecInstCamp_02.MaxDeInstCamp AS InstCamp, rq_InstSansEtablissement.Inst, IIf([tb_Client1_Group_1].[idGroupClient1]=1,"",[tb_Client1_Group_1].[Group] & "-" & [tb_Client1_Group_1].[no]) AS fusion, tb_Client1_Group.idGroupFusion FROM ((tb_Client1_Group AS tb_Client1_Group_1 INNER JOIN (tb_Client1_REGION INNER JOIN (tb_Client1_Group LEFT JOIN rq_InstSansEtablissement ON tb_Client1_Group.idGroupClient1 = rq_InstSansEtablissement.idGroupClient1) ON tb_Client1_REGION.idREGION = tb_Client1_Group.idREGION) ON tb_Client1_Group_1.idGroupClient1 = tb_Client1_Group.idGroupFusion) INNER JOIN tb_Client1_VPR ON tb_Client1_REGION.idVPR = tb_
Client1_VPR.idVPR) LEFT JOIN rq_InstAvecInstCamp_02 ON tb_Client1_Group.idGroupClient1 = rq_InstAvecInstCamp_02.idInstC1 WHERE (((tb_Client1_Group.idGroupClient1)<>1)) ORDER BY tb_Client1_Group.Group, tb_Client1_Group.no;
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 40228874
your problem is this part of the RecordSource

"WHERE (((tb_Client1_Group.idGroupClient1)<>1)) ORDER BY tb_Client1_Group.Group, tb_Client1_Group.no; "

there is already a Where Clause and you are adding another "WHERE clause at the end.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:Karl001
ID: 40229168
How can export the data on the form when I use a filter on it?

Thanks
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 40229379
One way is to create a make-table query based on your query, run it and then export the resulting table.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40229420
<How can export the data on the form when I use a filter on it?>


if you don't want to create a make table query,

you will need more VBA codes to accomplish this.
0
 

Author Comment

by:Karl001
ID: 40239417
I tried 2 different ways and it didn't work.

FIRST
I modified the code, to consider the "Where" in the recordsource. ref. Rey Obrero comment.
Here the new code:
Set ExportQuery = CurrentDb.CreateQueryDef("ExportQuery", Replace(Me.RecordSource, ";", "") & " AND " & Me.Filter & ";")

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "ExportQuery", "c:\AccessTest\test.xlsx", False


Result:
WHERE (((tb_Client1_Group.idGroupClient1)<>1)) AND [filter1] = 'Yes' AND [filter2] = 'nd';

filter1 and filter2 it's not recognized. Asking to enter a parameter value for those filters form.
The excel file include all records. The filter was not applied


SECOND WAY

I took a different way. I tried to use the “recorset”.
I used a code from an other question.
http://www.experts-exchange.com/Database/MS_Access/Q_22528539.html

Dim sSQL As String
Dim objXL As Object
Dim objWB As Object
Dim rstFull As Recordset
Set rstFull = CurrentDb.OpenRecordset("rptRRDataALL")
Set objXL = CreateObject("Excel.Application")
Set objWB = objXL.Workbooks.Add
objWB.Sheets(1).Range("A1").CopyFromRecordset rstFull
objXL.Visible = True
rstFull.Close
Set rstFull = Nothing


I changed the code
Set rstFull = CurrentDb.OpenRecordset("rptRRDataALL")
To
Set rstFull = me.recordset

I got error message: Error 13 Type mismatch

Which direction should I take?
0
 

Author Comment

by:Karl001
ID: 40242226
I found a solution.
I will complete my code and after poste it.
0
 

Accepted Solution

by:
Karl001 earned 0 total points
ID: 40244316
I had two problems, duplicate "where" instruction when I wanted to apply the filter form (me.filter), and I had problem with calculated field in the query form.

It’s possible with VBA  to add  "me.filter" after the existing "Where", but I didn't take that direction
I used a query form without  "where"  condition and calculate fields.

Here my solution  

    Dim strSQL As String
    Dim qdFormQuery As QueryDef 'form query definition
    Dim qdFormFilter As QueryDef 'form and filter form query definition
 
    If QueryExists("qdFormFilterQuery") Then
          DoCmd.DeleteObject acQuery, "qdFormFilterQuery"
    End If

    Set qdFormQuery = CurrentDb().QueryDefs("rq_frm_Client_List")
   
    'Applied filter form to SQL record source form
    strSQL = Replace(qdFormQuery.SQL, ";", "") & " WHERE " & Me.Filter & ";"
                 
    ' new Query definion (Form record source with filter form)
    Set qdFormFilter = CurrentDb().CreateQueryDef("qdFormFilterQuery", strSQL)
 
    ' transfert to Excel
    DoCmd.OutputTo acOutputQuery, "qdFormFilterQuery", "ExcelWorkbook(*.xlsx)"
   

Function QueryExists = From Jeff Willy
http://www.experts-exchange.com/Database/Miscellaneous/Q_22612217.html


Function QueryExists(qname$) As Boolean
  On Error Resume Next
    Dim db As Database, rs As Recordset, I%
    Set db = CurrentDb
    For I = 0 To db.QueryDefs.Count - 1
        If db.QueryDefs(I).Name = qname Then
            QueryExists = True
            Exit For
        End If
    Next
End Function
0
 

Author Closing Comment

by:Karl001
ID: 40252672
my solution is more complete
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question