Access_codice VBA che crea report in excel.

Anonimo
2015-09-21T12:37:48+00:00

Ciao A tutti,

avrei bisogno del vostro aiuto.

ho il seguente codice, che ad ogni click, mi esporta la Export detail su un file excel detail. Però questo codice ha un limite, ad ogni click, se selezione un nuovo dato, mi cancella quello scaricato predentemente.

Private Sub Command184_Click()

    DoCmd.OpenQuery "Make EXPORT DETAIL"

    DoCmd.OutputTo acOutputTable, "EXPORT_DETAIL", acFormatXLS, "D:\Tools\EUROWEB\REALLOCATOR\Detail.xls"

    MsgBox ("Data Exported in D:\Tools\EUROWEB\REALLOCATOR\Detail.xls")

End Sub

quello che vorrei modificare è che ad ogni click, al posto di cancellarmi il file detail e mettere i nuovi dati, mi aggiunge un foglio nuovo per ogni click, nominato dal dato che gli indico.

E' possibile?

grazie

Luigi

Microsoft 365 e Office | Access | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

15 risposte

Ordina per: Più utili
  1. Anonimo
    2015-09-22T13:03:05+00:00

    Ciao Sandro,

    ti ringrazio per la tua celere risposta!

    Allora ho copiato il tuo modulo, poi ho scritto la seguente funzione sul click del botte che mi fa l'exporta.

    Private Sub multiple_button_Click()

        DoCmd.OpenQuery "Make EXPORT DETAIL"

        export2XLs2 "select * from EXPORT DETAIL"

    End Sub

    Perchè mi deve aggiornare la tabella da cui faccio l'export.

    però mi esce il problema. Allego l'immagine.

    Grazie

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2015-09-22T12:35:36+00:00

    ciao Luigi,

    >>>lo copi nell'excel o nel access,  in access.

    il 3d che ti ho consigliato era uno spunto di partenza, ma risolve un problema diverso che è l'accodamento dei dati.

    io ti consiglio di scaricare la demo che ti ho preparato, in chi ho modificato il codice in base a quanto chiedi. ( quando puoi).

    per aiutarti, ti copio incollo il codice della demo e ti indico come procedere.

    Inserisci una maschera e crea un commandButton in cui invochi :

    export2XLs2 "select * from tuaTabella"

    tua tabella è il nome di una tabella del tuo DB.

    segui quanto detto nel post precedente circa la cartella.

    copi il seguente codice un modulo standard, è il codice modificato che trovi nella demo.

    In pratica, nella cartella c:\prova generi un file di Excel chiamato provaExport in cui trovi, ogni volta che clicchi sul command button, estrai in un nuovo foglio tutti i campi di una tabella.

    ovviamente la stringa sql è personalizzabile...

    Facci sapere.

    Ciao, Sandro.

    Option Compare Database

    Option Explicit

    Sub export2XLs2(ByVal strSql As String)

    On Error GoTo errorHandler

    Dim xlApp      As Object

    Dim wbk        As Object 'as Workbook

    Dim wsh        As Object 'as Worksheet

    Dim dbp        As Access.CurrentProject

    Dim xlQry      As Object

    Dim i          As Integer

     Const cstrXlClass = "Excel.Application"

     Const cstrFullName = "c:\prova\provaExport.xlsx"

     Const cstrCnn = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0" _

                  & ";Data Source=" & cstrFullName _

                  & ";Mode=Read;"

    Dim blnNotRunning      As Boolean

    Dim strCnn             As String

    Dim extractXlsFileName As String

    With Application

         Set dbp = .CurrentProject

    End With

    If Not folderExists("C:\prova") Then MkDir "C:\prova"

    strCnn = Replace(cstrCnn, cstrFullName, dbp.FullName)

    blnNotRunning = getXlSinstance(xlApp, False, cstrXlClass)

    With xlApp

        If blnNotRunning Then

           ' .Visible = True

           .ScreenUpdating = False

        End If

    End With

    If fileExists(cstrFullName) Then

        Set wbk = xlApp.Workbooks.Open(cstrFullName)

        With wbk.Worksheets

            .Add , , Count:=1

        End With

    Else

        Set wbk = xlApp.Workbooks.Add

        With wbk

         For i = .Worksheets.Count To 2 Step -1

            .Worksheets.Item(i).Delete

         Next

        End With

    End If

    Set wsh = wbk.Worksheets.Item(1)

     With wsh

        Set xlQry = .QueryTables.Add(Connection:=strCnn _

                                         , Destination:=.Range("A1"))

    End With

    With xlQry

         .CommandType = 2      ' 2 è la costante per xlCmdSql

         .CommandText = strSql

         .AdjustColumnWidth = True

         .FieldNames = True

         .Refresh

         .Delete

    End With

    With xlApp

             .displayAlerts = False

            .ScreenUpdating = True

            .Calculation = -4105 'xlAutomatic

            '.Visible = False

             wbk.SaveAs FileName:=cstrFullName

             wbk.Close SaveChanges:=True _

                  , FileName:=cstrFullName

            .displayAlerts = True

    End With

        MsgBox "Salvato ed esportato", vbInformation, "Avviso"

    exitErrorHandler:

        Set wsh = Nothing

        Set wbk = Nothing

        xlApp.Quit

        Set xlApp = Nothing

        Exit Sub

    errorHandler:

        With Err

                MsgBox "ERR#" & CStr(.Number) _

                    & vbNewLine & .Description _

                    , vbOKOnly Or vbCritical

         End With

         Resume exitErrorHandler

    End Sub

    Private Function getXlSinstance(xlApp As Object _

                                    , isXlsRunning As Boolean _

                                    , strClass As String) As Boolean

        Err.Clear

        If isXlsRunning Then

            On Error Resume Next

            Set xlApp = GetObject(, strClass)

        End If

        If xlApp Is Nothing Then

            Set xlApp = CreateObject(strClass)

        End If

        If Err <> 0 Then

            MsgBox "Qualcosa non va controlla questo l'errore:" & Err.Description, _

                    vbCritical, "Attenzione!!!"

            getXlSinstance = False

        Else

            getXlSinstance = True

        End If

    End Function

    Private Function fileExists(strFullPath As String) As Boolean

        On Error Resume Next

        fileExists = ((GetAttr(strFullPath) And vbDirectory) = 0)

    End Function

    Private Function folderExists(strPath As String) As Boolean

        On Error Resume Next

        folderExists = ((GetAttr(strPath) And vbDirectory) = vbDirectory)

    End Function

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2015-09-22T12:01:23+00:00

    Ciao Sandro,

    ti ringrazio per la risposta. Dall'ufficio purtroppo non posso aprire i link protetti.

    Stasera controllo il data base.

    Vorrei chiederti il codice del modulo del link

    http://answers.microsoft.com/it-it/office/forum/office\_2007-access/automazione-in-excel-aggiunta-di-dati/04f09eba-a86e-4cc2-83f7-106a19ca721d

    lo copi nell'excel o nel access

    non mi è chiaro

    Grazie del tuo supporto

    La risposta è stata utile?

    0 commenti Nessun commento
  4. Anonimo
    2015-09-21T18:30:38+00:00

    ciao Luigi,

    siccome mi diverso un sacco con le automazione, ti ho preparato una demo basata sul 3d segnalato in precedenza.

    crea una cartella chiamandola prova in c:.

    poi prova questa demo : http://1drv.ms/1FbnIBg luigiDiLauro.accdb.

    clicca a più riprese sulla command button clienti e clienti1 della form maschera1.

    La demo genera un file xls nella cartella c:\prova\ estraendo in un nuovo foglio di Excel ma dello stesso file le varie il recordSource che viene passato alla sub..

    Ecco...non ho modifcato il nome dei fogli nel senso che vengono nominati e creati per default foglio1, foglio2, ect...

    ma l'estrazione più recente è quella che ha un'indice maggiore.

    facci sapere.

    ciao, Sandro.

    La risposta è stata utile?

    0 commenti Nessun commento
  5. Anonimo
    2015-09-21T13:32:22+00:00

    ciao Luigi,

    la cosa che chiedi è fattibilissima ma non con output To ma con l'automazione.

    devi aprire un'istanza su Excel da Access, utlizzare il metodo add dell'oggetto workBook e aggiungere un worksheet vuoto ad ogni estrazione di file.

    il codice da cui puoi partire è in questo  3d :  http://answers.microsoft.com/it-it/office/forum/office\_2007-access/automazione-in-excel-aggiunta-di-dati/04f09eba-a86e-4cc2-83f7-106a19ca721d

    il 3d che ti ho suggerito accoda l'estrazione dello stesso foglio, con le modifiche che ti ho suggerito invece vai ad aggiungere un nuovo foglio che accoglierà l'estrazione.

    ciao, Sandro.

    La risposta è stata utile?

    0 commenti Nessun commento