Exporter resultat d'un sous formulaire dans excel

Résolu
jujubas Messages postés 13 Date d'inscription   Statut Membre Dernière intervention   -  
 G-no -
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

2 réponses

  1. jujubas Messages postés 13 Date d'inscription   Statut Membre Dernière intervention  
     
    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
  2. G-no
     
    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