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