Solved

Import date from Access to Excel

Posted on 2014-07-30
10
369 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 119

Expert Comment

by:Rey Obrero
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 119

Assisted Solution

by:Rey Obrero
Rey Obrero 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
 

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 119

Expert Comment

by:Rey Obrero
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now