Exporter resultat d'un sous formulaire dans excel

Résolu/Fermé
jujubas Messages postés 13 Date d'inscription mercredi 17 juillet 2013 Statut Membre Dernière intervention 20 août 2013 - 8 août 2013 à 10:13
 G-no - 29 nov. 2013 à 15:23
Bonjour,

J'ai un formulaire avec un sous formualire qui m'affiche ma liste de résultats, ce sous formulaire et filtré à l'aide filtre situés dans le formulaire principale. J'aimerais exporter dans excel le résultat de mon sous formulaire filtré. J'ai une petite idée déjà de comment faire, je vous montre le début de mon code

Private Sub cmdTestXcl_Click()

    Dim sqlChamp As QueryDef
    CurrentDb.CreateQueryDef "sqlChamp"
    Dim filt As String
    Dim str As String
    Dim var As String
    Dim qry As QueryDef
    
    xclTool = Forms![frmMain]![sfmToolsList].Form.RecordSource
    Set qry = CurrentDb.QueryDefs(xclTool)
    str = qry.SQL
    CurrentDb.QueryDefs("sqlChamp").SQL = str
'Ici j'ai donc sqlchamp avec le sql du sous formulaire non filtré, je veux maintenant "filtré cette requête", c'est à dire lui appliqué ls filtres du sous formulaire (c'est la ou ça coince...)
 
    filt = Forms![frmMain]![sfmToolsList].Form.Filter
    CurrentDb.QueryDefs("sqlChamp").SQL = filt    
'Cette formulation ne marche pas...

    Dim outputFileName As String  
    outputFileName = CurrentProject.Path & "\Export_" & Format(Date, "yyyyMMdd") & ".xls"

    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "sqlChamp", outputFileName , True
    DoCmd.DeleteObject acQuery, "sqlChamp"
   
End Sub


Voila si quelqu'un pouvait m'aider ou avait une autre solution qui marche ça m'aiderait beaucoup.

Merci d'avance
A voir également:

2 réponses

jujubas Messages postés 13 Date d'inscription mercredi 17 juillet 2013 Statut Membre Dernière intervention 20 août 2013
8 août 2013 à 16:47
Bon, En fait j'ai trouvé, je poste mon code si un jour ça permet d'aider quelqu'un...

Private Sub cmdTestXcl_Click()

    Dim sqlChamp As QueryDef
    Dim test As QueryDef
    CurrentDb.CreateQueryDef "sqlChamp"
    CurrentDb.CreateQueryDef "test"
    Dim filt As String
    Dim ord As String
    Dim str As String
    Dim det As String
    Dim var As String
    Dim qry As QueryDef
    
    xclTool = Forms![frmMain]![sfmToolsList].Form.RecordSource
    Set qry = CurrentDb.QueryDefs(xclTool)
    str = qry.SQL
    
    CurrentDb.QueryDefs("sqlChamp").SQL = str
    filt = Forms![frmMain]![sfmToolsList].Form.Filter
    ord = Forms![frmMain]![sfmToolsList].Form.OrderBy
    det = "SELECT sqlChamp.ToolName, sqlChamp.ToolSupplier, sqlChamp.ToolSheet From sqlChamp Where " + filt + " Order By " + ord + ";"
    CurrentDb.Execute "Update sqlChamp Set ToolSheet = 'Ok' WHERE ToolSheet <> '' ;"
    CurrentDb.QueryDefs("test").SQL = det
    
    Dim ter As String
    ter = EnregistrerUnFichier(Me.hwnd, "Enregistrer sous", "Test.xls", "C:\Documents and Settings\" & login & "\")
    If Not IsNull(ter) And ter <> "" Then
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "test", ter, True
        
        Dim xlApp As Excel.Application
        Dim xlSheet As Excel.Worksheet
        Dim xlBook As Excel.Workbook
       
        ' Initialiser les variables
        Set xlApp = CreateObject("Excel.Application")
        Set xlBook = xlApp.Workbooks.Open(ter)
        Set xlSheet = xlBook.Worksheets("test")
        xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 10)).ColumnWidth = 38
        xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 10)).Font.Bold = True
        xlBook.SAve
        xlApp.Quit
        Set xlSheet = Nothing
        Set xlBook = Nothing
        Set xlApp = Nothing
    End If
    
    DoCmd.DeleteObject acQuery, "test"
    DoCmd.DeleteObject acQuery, "sqlChamp"
    
End Sub
0
Bonjour à tous, je me permet de relancer le sujet mais j'aimerai comment peut on adapter ce code à ACCESS 2010 ?

Merci beaucoup,

G-no
0