?
Solved

Import date from Access to Excel

Posted on 2014-07-30
10
Medium Priority
?
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

752 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