Siamo giunti all’ultima puntata di questa serie di articoli, tutorial dedicati alla creazione di un applicativo in Access per gestire il magazzino. Dopo aver visto come creare le finestre per clienti, fornitori ed articoli abbiamo realizzato delle maschere per il movimento della nostra merce di magazzino, come ultimo processo abbiamo inserito le stampe dei totali sia come quantità che come valore. Ora vedremo come esportare i dati inseriti nel nostro database su un foglio di Excel in modo da poterci fare qualsiasi calcolo statistico. Microsoft Excel™ infatti è uno strumento di Office molto potente per fare le analisi dei dati ed in questo caso ce ne serviremo per esportare i valori aggregati dei movimenti.

I dati che richiederemo all’utilizzatore sono: la data iniziale e finale da considerare, se un articolo in particolare oppure tutti, il tipo di movimento oppure tutti ed infine se aggregare i dati sui totali oppure sui parziali. Ecco allora il codice che permette di prelevare i dati dalla form e poi effettuare tutte le query necessarie sul database utilizzando questi ultimi.
Option Compare Database
Private Sub Export_Click()
On Error Resume Next
If IsNull(Me.LastInit) Then
MsgBox "Immettere la data iniziale!",
vbExclamation, "Ricerca"
Exit Sub
End If
If IsNull(Me.LastEnd) Then
MsgBox "Immettere la data finale!", vbExclamation,
"Ricerca"
Exit Sub
End If
If Me.LastInit >= Me.LastEnd Then
MsgBox "La data finale è prima della iniziale!",
vbExclamation, "Ricerca"
Exit Sub
End If
DoCmd.Hourglass True
Dim myData As dao.Database, myQry As dao.QueryDef,
myRec As dao.Recordset, myDst As dao.Recordset
Set myData = CurrentDb
myData.Execute "DELETE * FROM [Ricerca Dati]"
myData.Execute "DELETE * FROM [Ricerca Totali]"
myData.Execute "DELETE * FROM [Ricerca Avanzata]"
Dim valType As Integer
If Me.CarSca.Enabled = True Then
If IsNull(Me!CarSca) Then
valType = 4
Else
If Me!CarSca = "Carichi" Then
valType = 1
ElseIf Me!CarSca = "Scarichi" Then
valType = 2
Else
valType = 3
End If
End If
Else
valType = 5
End If
If valType = 5 Then
If Err 0 Then
MsgBox Err.Description
MsgBox "Impossibile eseguire l'operazione
richiesta!", vbCritical
DoCmd.Hourglass False
Exit Sub
Else
DoCmd.OutputTo acOutputTable, "Ricerca Avanzata",
acFormatXLS, "Magazzino.xls", True
End If
End If
DoCmd.Hourglass False
Exit Sub
End If
If Not Me!Totali Then
Set myDst = myData.OpenRecordset("Ricerca Dati")
If valType = 1 Or valType = 4 Then
Set myQry = myData.QueryDefs("Cronologia Carichi")
myQry.Parameters![DataInit] = Me!LastInit
myQry.Parameters![DataEnd] = Me!LastEnd
If IsNull(Me.LastArt) Then
myQry.Parameters![Articolo] = "*"
Else
myQry.Parameters![Articolo] = Me!LastArt
End If
Set myRec = myQry.OpenRecordset()
While Not myRec.EOF
With myDst
.AddNew
![Data Movimento] = myRec.Fields(0).Value
![Matricola Articolo] = myRec.Fields(1).Value
!Articolo = myRec.Fields(2).Value
![Tipo Movimento] = "Carico"
![Unità di Misura] = myRec.Fields(3).Value
!Quantità = myRec.Fields(4).Value
!Codice = myRec.Fields(5).Value
!Note = myRec.Fields(6).Value
![Codice a Barre] = myRec.Fields(7).Value
!Valore = -myRec.Fields(8).Value
!Numero = myRec.Fields(9).Value
.Update
End With
myRec.MoveNext
Wend
myRec.Close
myQry.Close
Set myQry = Nothing
Set myRec = Nothing
End If
If valType = 2 Or valType = 4 Then
Set myQry = myData.QueryDefs("Cronologia Scarichi")
myQry.Parameters![DataInit] = Me.LastInit
myQry.Parameters![DataEnd] = Me.LastEnd
If IsNull(Me.LastArt) Then
myQry.Parameters![Articolo] = "*"
Else
myQry.Parameters![Articolo] = Me!LastArt
End If
Set myRec = myQry.OpenRecordset()
While Not myRec.EOF
With myDst
.AddNew
![Data Movimento] = myRec.Fields(0).Value
![Matricola Articolo] = myRec.Fields(1).Value
!Articolo = myRec.Fields(2).Value
![Tipo Movimento] = "Scarico"
![Unità di Misura] = myRec.Fields(3).Value
!Quantità = -myRec.Fields(4).Value
!Codice = myRec.Fields(5).Value
!Note = myRec.Fields(6).Value
![Codice a Barre] = myRec.Fields(7).Value
!Valore = myRec.Fields(8).Value
!Numero = myRec.Fields(9).Value
.Update
End With
myRec.MoveNext
Wend
myRec.Close
myQry.Close
Set myQry = Nothing
Set myRec = Nothing
End If
If valType = 3 Or valType = 4 Then
Set myQry = myData.QueryDefs("Cronologia Rese")
myQry.Parameters![DataInit] = Me.LastInit
myQry.Parameters![DataEnd] = Me.LastEnd
If IsNull(Me.LastArt) Then
myQry.Parameters![Articolo] = "*"
Else
myQry.Parameters![Articolo] = Me!LastArt
End If
Set myRec = myQry.OpenRecordset()
While Not myRec.EOF
With myDst
.AddNew
![Data Movimento] = myRec.Fields(0).Value
![Matricola Articolo] = myRec.Fields(1).Value
!Articolo = myRec.Fields(2).Value
![Tipo Movimento] = "Resa"
![Unità di Misura] = myRec.Fields(3).Value
!Quantità = myRec.Fields(4).Value
!Codice = myRec.Fields(5).Value
!Note = myRec.Fields(6).Value
![Codice a Barre] = myRec.Fields(7).Value
!Valore = -myRec.Fields(8).Value
!Numero = myRec.Fields(9).Value
.Update
End With
myRec.MoveNext
Wend
myRec.Close
myQry.Close
Set myQry = Nothing
Set myRec = Nothing
End If
If Err > 0 Then
MsgBox "Impossibile eseguire l'operazione richiesta!",
vbCritical
DoCmd.Hourglass False
Exit Sub
Else
DoCmd.OutputTo acOutputTable, "Ricerca Dati",
acFormatXLS, "Magazzino.xls", True
End If
myDst.Close
Set myDst = Nothing
Else
Set myDst = myData.OpenRecordset("Ricerca Totali")
If valType = 1 Or valType = 4 Then
Set myQry = myData.QueryDefs("Carichi Totali")
myQry.Parameters![DataInit] = Me!LastInit
myQry.Parameters![DataEnd] = Me!LastEnd
If IsNull(Me.LastArt) Then
myQry.Parameters![Articolo] = "*"
Else
myQry.Parameters![Articolo] = Me!LastArt
End If
Set myRec = myQry.OpenRecordset()
While Not myRec.EOF
With myDst
.AddNew
![Matricola Articolo] = myRec.Fields(0).Value
!Articolo = myRec.Fields(1).Value
![Tipo Movimento] = "Carico"
![Unità di Misura] = myRec.Fields(2).Value
!Quantità = myRec.Fields(3).Value
![Codice a Barre] = myRec.Fields(4).Value
!Valore = -myRec.Fields(5).Value
.Update
End With
myRec.MoveNext
Wend
myRec.Close
myQry.Close
Set myQry = Nothing
Set myRec = Nothing
End If
If valType = 2 Or valType = 4 Then
Set myQry = myData.QueryDefs("Scarichi Totali")
myQry.Parameters![DataInit] = Me.LastInit
myQry.Parameters![DataEnd] = Me.LastEnd
If IsNull(Me.LastArt) Then
myQry.Parameters![Articolo] = "*"
Else
myQry.Parameters![Articolo] = Me!LastArt
End If
Set myRec = myQry.OpenRecordset()
While Not myRec.EOF
With myDst
.AddNew
![Matricola Articolo] = myRec.Fields(0).Value
!Articolo = myRec.Fields(1).Value
![Tipo Movimento] = "Scarico"
![Unità di Misura] = myRec.Fields(2).Value
!Quantità = -myRec.Fields(3).Value
![Codice a Barre] = myRec.Fields(4).Value
!Valore = myRec.Fields(5).Value
.Update
End With
myRec.MoveNext
Wend
myRec.Close
myQry.Close
Set myQry = Nothing
Set myRec = Nothing
End If
If valType = 3 Or valType = 4 Then
Set myQry = myData.QueryDefs("Rese Totali")
myQry.Parameters![DataInit] = Me.LastInit
myQry.Parameters![DataEnd] = Me.LastEnd
If IsNull(Me.LastArt) Then
myQry.Parameters![Articolo] = "*"
Else
myQry.Parameters![Articolo] = Me!LastArt
End If
Set myRec = myQry.OpenRecordset()
While Not myRec.EOF
With myDst
.AddNew
![Matricola Articolo] = myRec.Fields(0).Value
!Articolo = myRec.Fields(1).Value
![Tipo Movimento] = "Resa"
![Unità di Misura] = myRec.Fields(2).Value
!Quantità = myRec.Fields(3).Value
![Codice a Barre] = myRec.Fields(4).Value
!Valore = -myRec.Fields(5).Value
.Update
End With
myRec.MoveNext
Wend
myRec.Close
myQry.Close
Set myQry = Nothing
Set myRec = Nothing
End If
If Err > 0 Then
MsgBox "Impossibile eseguire l'operazione richiesta!",
vbCritical
DoCmd.Hourglass False
Exit Sub
Else
DoCmd.OutputTo acOutputTable, "Ricerca Totali",
acFormatXLS, "Magazzino.xls", True
End If
myDst.Close
Set myDst = Nothing
End If
DoCmd.Hourglass False
Set myData = Nothing
End Sub
Private Sub Form_Load()
Me.Avanzata = Not Me!Normale
Me.Totali.Enabled = Me!Normale
Me.CarSca.Enabled = Me!Normale
End Sub
Private Sub Form_Unload(Cancel As Integer)
Forms![Pannello comandi].Visible = True
End Sub
Private Sub Normale_Click()
If Me.Normale Then
Me.Normale = True
Me.Avanzata = False
Else
Me.Normale = True
End If
Me.Totali.Enabled = True
Me.CarSca.Enabled = True
End Sub
Private Sub Avanzata_Click()
If Me.Avanzata Then
Me.Normale = False
Me.Avanzata = True
Else
Me.Avanzata = True
End If
Me.Totali.Enabled = False
Me.CarSca.Enabled = False
End Sub
A prima vista il codice potrebbe sembrare un poco difficile da interpretare, invece è semplicissimo: con i dati richiesti all’utente e dopo la loro verifica si passa a svuotare delle tabelle d’appoggio nel database e quindi si riempiono con i dati richiesti, alla fine del processo si esporta la tabella su un foglio di Excel creato a runtime.
Ecco spiegati tutti i segreti di Calus 2012, il mio piccolo ma efficiente programma in Office per magazzino che sta riscuotendo molto successo in rete. Se avete seguito l’intero tutorial con attenzione ora potrete anche crearvi il vostro programma per magazzino da soli, basta utilizzare le tecniche apprese per gli altri compiti del programma.