Condividi:        

Access da SQL al VBA

Vuoi potenziare i tuoi documenti Word? Non sai come si fa una macro in Excel? Devi creare una presentazione in PowerPoint?
Oppure sei passato a OpenOffice e non sei sicuro di come lavorare al meglio?

Moderatori: Anthony47, Flash30005

Access da SQL al VBA

Postdi EnricoBanco » 24/03/18 22:36

Ciao Anthony, ciao a tutti,

mi sono sempre chiesto come come si passa dal SQL di una query Access al VBA Access, nel link il file allegato un esempio di gestione dati da modulo del tipo:

importa dati da excel
scarica dati in excel
crea vari tipi di query
esegue vari tipi di query
esegue macro excel
un esempio di macro access che esegue una macro da modulo (Modulo7: Function Esegue_Aggiorna_q)

Inoltre alcuni esempi che già conoscete tutti ma magari utili a chi comincia a utilizzare Access, quindi tanto per completare ho messo anche semplici:
maschera di data entry
maschera con sottomaschera (query che collega due tabelle)
maschera on pulsanti macro
report

http://www.filedropper.com/vbaaccess_1

Domanda: ho fatto un test con il carica dati da Modulo1, ci mette 7 minuti per caricare 6000 record mentre la stessa tabella il tool importa dati di Access ci mette 5 secondi. C'è un modo per velocizzare l'importa dati tabella excel da codice?
EnricoBanco
Utente Junior
 
Post: 77
Iscritto il: 18/07/17 06:29

Sponsor
 

Re: Access da SQL al VBA

Postdi EnricoBanco » 24/03/18 22:46

Ho trovato scartabbellando sul web:
importa dati da excel
scarica dati in excel
esegue macro excel
EnricoBanco
Utente Junior
 
Post: 77
Iscritto il: 18/07/17 06:29

Re: Access da SQL al VBA

Postdi EnricoBanco » 27/03/18 16:18

Trovato e testato, circa 100.000 records in 7 secondi, file excel che contiene dieci fogli

Codice: Seleziona tutto
Option Compare Database

Sub Carica_Dati_da_Excel_in_Acess()

Dim strPathFile As String, strFile As String, strPath As String
 Dim blnHasFieldNames As Boolean
 Dim intWorksheets As Integer

' Replace 10 with the number of worksheets to be imported
 ' from each EXCEL file
 Dim strWorksheets(1 To 10) As String

' Replace 10 with the number of worksheets to be imported
 ' from each EXCEL file (this code assumes that each worksheet
 ' with the same name is being imported into a separate table
 ' for that specific worksheet name)
 Dim strTables(1 To 10) As String

' Replace generic worksheet names with the real worksheet names;
 ' add / delete code lines so that there is one code line for
 ' each worksheet that is to be imported from each workbook file
 strWorksheets(1) = "Foglio1"
 strWorksheets(2) = "Foglio2"
 strWorksheets(3) = "Foglio3"
 strWorksheets(4) = "Foglio4"
 strWorksheets(5) = "Foglio5"
 strWorksheets(6) = "Foglio6"
 strWorksheets(7) = "Foglio7"
 strWorksheets(8) = "Foglio8"
 strWorksheets(9) = "Foglio9"
 strWorksheets(10) = "Foglio10"


' Replace generic table names with the real table names;
 ' add / delete code lines so that there is one code line for
 ' each worksheet that is to be imported from each workbook file
 strTables(1) = "Foglio1"
 strTables(2) = "Foglio2"
 strTables(3) = "Foglio3"
 strTables(4) = "Foglio4"
 strTables(5) = "Foglio5"
 strTables(6) = "Foglio6"
 strTables(7) = "Foglio7"
 strTables(8) = "Foglio8"
 strTables(9) = "Foglio9"
 strTables(10) = "Foglio10"

' Change this next line to True if the first row in EXCEL worksheet
 ' has field names
 blnHasFieldNames = True

' Replace C:\Documents\ with the real path to the folder that
 ' contains the EXCEL files
 strPath = "C:\"

' Replace 10 with the number of worksheets to be imported
 ' from each EXCEL file
 For intWorksheets = 1 To 10

       strFile = Dir(strPath & "File.xls")
       Do While Len(strFile) > 0
             strPathFile = strPath & strFile
             DoCmd.TransferSpreadsheet acImport, _
                   acSpreadsheetTypeExcel9, strTables(intWorksheets), _
                   strPathFile, blnHasFieldNames, _
                   strWorksheets(intWorksheets) & "$"
             strFile = Dir()
       Loop

 Next intWorksheets

End Sub
EnricoBanco
Utente Junior
 
Post: 77
Iscritto il: 18/07/17 06:29


Torna a Applicazioni Office Windows


Topic correlati a "Access da SQL al VBA":


Chi c’è in linea

Visitano il forum: Nessuno e 39 ospiti