Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

Import date from Access to Excel

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
Karl001
Asked:
Karl001
  • 6
  • 3
2 Solutions
 
Rey Obrero (Capricorn1)Commented:
check the string content of

Me.RecordSource

debug.print Me.RecordSource

copy and paste here
0
 
Karl001Author Commented:
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
 
Rey Obrero (Capricorn1)Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Karl001Author Commented:
How can export the data on the form when I use a filter on it?

Thanks
0
 
Helen FeddemaCommented:
One way is to create a make-table query based on your query, run it and then export the resulting table.
0
 
Rey Obrero (Capricorn1)Commented:
<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
 
Karl001Author Commented:
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
 
Karl001Author Commented:
I found a solution.
I will complete my code and after poste it.
0
 
Karl001Author Commented:
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
 
Karl001Author Commented:
my solution is more complete
0

Featured Post

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.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now