Hello,
Its been a while since I did some VBA - I am running a block of code which parses a directory, checks some rules and imports a file then runs a query on the data. Then loops to the next file to check against rules, import, run the query, etc.
I ran it on a directory which contained only 3 files (2 that matched and 1 that did not) and it was taking a little over a minute per file (2K records total). Larger files are taking over 3 minutes per file to import and run query.
I am hoping someone can take a look at the code and advise if there is a more efficient way to write it to get faster performance of the import and particularly the query.
[START VBA code]
Function MULTI_IMPORT()
Set db = CurrentDb()
Path = [Forms]![frmNavigation]![NavigationSubform].[Form]![FileName]
myfile = Dir(Path & "\*.*")
Dim TBL As String
Dim Spec As String
Dim FT As String
Dim ACT As String
Dim IE As String
Dim sql As String
DoCmd.SetWarnings False
Dim FC As Integer
Dim FN5 As String
Dim ID As Date
Dim RD As Date
ID = [Forms]![frmNavigation]![NavigationSubform].[Form]![ImportDate]
RD = [Forms]![frmNavigation]![NavigationSubform].[Form]![ReportDate]
Do Until myfile = ""
i = 0
'CSV AIR
If Left(myfile, 5) Like "CSVAX" = True Then
Spec = "CSV_AIR"
TBL = "tblCSV"
FT = "CSV AIR"
ACT = "AIR"
IE = "ALL"
FN5 = "CSVAX"
'CSV Ocean
ElseIf Left(myfile, 5) Like "CSVSX" = True Then
Spec = "CSV_SEA"
TBL = "tblCSV"
FT = "CSV SEA"
ACT = "SEA"
IE = "ALL"
FN5 = "CSVSX"
'Search Panel Export
ElseIf Left(myfile, 5) Like "SSEAE" = True Then
Spec = "SearchPanelMasterOcean"
TBL = "tblSearchPanel"
FT = "SEARCH PANEL"
ACT = "SEA"
IE = "EXPORT"
FN5 = "SSEAE"
'Search Panel Import
ElseIf Left(myfile, 5) Like "SSEAI" = True Then
Spec = "SearchPanelMasterOceanI"
TBL = "tblSearchPanel"
FT = "SEARCH PANEL"
ACT = "SEA"
IE = "IMPORT"
FN5 = "SSEAI"
'Billing Panel
ElseIf Left(myfile, 5) Like "BILLX" = True Then
Spec = "BILLINGPANEL"
TBL = "tblBilling"
FT = "BILLING"
ACT = "ALL"
IE = "ALL"
FN5 = "BILLX"
'Job Status
ElseIf Left(myfile, 5) Like "JSSTA" = True Then
Spec = "JOBSTATUS"
TBL = "tblJOBSTATUS"
qry2 = "qryCLEAN_JOBSTATUS"
FT = "JOBSTATUS"
ACT = "SEA"
IE = "IMPORT"
FN5 = "JSSTA"
'Pending Upload
ElseIf Left(myfile, 5) Like "PENDU" = True Then
Spec = "PENDINGUPLOAD"
TBL = "tblPENDINGUPLOAD"
FT = "PENDING UPLOAD"
ACT = "ALL"
IE = "ALL"
FN5 = "PENDU"
'BW RDD1005 Billing Report
ElseIf Left(myfile, 5) Like "BWRDD" = True Then
Spec = "RDD1005"
TBL = "tblBWRDD1005"
FT = "BW RDD1005"
ACT = "ALL"
IE = "ALL"
FN5 = "BWRDD"
'WF KTDL Kewill To Do List
ElseIf Left(myfile, 5) Like "WFKTD" = True Then
Spec = "WFKTDL"
TBL = "tblWFKTDL"
FT = "WF KTDL"
ACT = "ALL"
IE = "ALL"
FN5 = "WFKTD"
'Pre-alert
ElseIf Left(myfile, 5) Like "PAXXX" = True Then
Spec = "Prealert"
TBL = "tblPrealert"
FT = "PREALERT"
ACT = "ALL"
IE = "ALL"
FN5 = "PAXXX"
End If
If FN5 = Left(myfile, 5) Then
DoCmd.TransferText acImportDelim, Spec, TBL, Path & "\" & myfile
sql = "UPDATE " & TBL & " SET " & TBL & ".Import_date = """ & ID & """, " & TBL & ".Report = """ & RD & """, " & TBL & ".FileType = """ & FT & """, " & TBL & ".Activity = """ & ACT & """, " & TBL & ".ImportExport = """ & IE & """, " & TBL & ".FileName = """ & myfile & """ WHERE (((" & TBL & ".Import_date) Is Null));"
CurrentDb.QueryDefs("qrytemp").sql = sql
DoCmd.OpenQuery "qryTemp", acViewNormal, acEdit
FC = FC + 1
End If
i = i + 1
FN5 = ""
myfile = Dir
Loop
DoCmd.SetWarnings True
MsgBox FC & " Files Imported", vbOKOnly, "Import Status"
End Function
Its been a while since I did some VBA - I am running a block of code which parses a directory, checks some rules and imports a file then runs a query on the data. Then loops to the next file to check against rules, import, run the query, etc.
I ran it on a directory which contained only 3 files (2 that matched and 1 that did not) and it was taking a little over a minute per file (2K records total). Larger files are taking over 3 minutes per file to import and run query.
I am hoping someone can take a look at the code and advise if there is a more efficient way to write it to get faster performance of the import and particularly the query.
[START VBA code]
Function MULTI_IMPORT()
Set db = CurrentDb()
Path = [Forms]![frmNavigation]![NavigationSubform].[Form]![FileName]
myfile = Dir(Path & "\*.*")
Dim TBL As String
Dim Spec As String
Dim FT As String
Dim ACT As String
Dim IE As String
Dim sql As String
DoCmd.SetWarnings False
Dim FC As Integer
Dim FN5 As String
Dim ID As Date
Dim RD As Date
ID = [Forms]![frmNavigation]![NavigationSubform].[Form]![ImportDate]
RD = [Forms]![frmNavigation]![NavigationSubform].[Form]![ReportDate]
Do Until myfile = ""
i = 0
'CSV AIR
If Left(myfile, 5) Like "CSVAX" = True Then
Spec = "CSV_AIR"
TBL = "tblCSV"
FT = "CSV AIR"
ACT = "AIR"
IE = "ALL"
FN5 = "CSVAX"
'CSV Ocean
ElseIf Left(myfile, 5) Like "CSVSX" = True Then
Spec = "CSV_SEA"
TBL = "tblCSV"
FT = "CSV SEA"
ACT = "SEA"
IE = "ALL"
FN5 = "CSVSX"
'Search Panel Export
ElseIf Left(myfile, 5) Like "SSEAE" = True Then
Spec = "SearchPanelMasterOcean"
TBL = "tblSearchPanel"
FT = "SEARCH PANEL"
ACT = "SEA"
IE = "EXPORT"
FN5 = "SSEAE"
'Search Panel Import
ElseIf Left(myfile, 5) Like "SSEAI" = True Then
Spec = "SearchPanelMasterOceanI"
TBL = "tblSearchPanel"
FT = "SEARCH PANEL"
ACT = "SEA"
IE = "IMPORT"
FN5 = "SSEAI"
'Billing Panel
ElseIf Left(myfile, 5) Like "BILLX" = True Then
Spec = "BILLINGPANEL"
TBL = "tblBilling"
FT = "BILLING"
ACT = "ALL"
IE = "ALL"
FN5 = "BILLX"
'Job Status
ElseIf Left(myfile, 5) Like "JSSTA" = True Then
Spec = "JOBSTATUS"
TBL = "tblJOBSTATUS"
qry2 = "qryCLEAN_JOBSTATUS"
FT = "JOBSTATUS"
ACT = "SEA"
IE = "IMPORT"
FN5 = "JSSTA"
'Pending Upload
ElseIf Left(myfile, 5) Like "PENDU" = True Then
Spec = "PENDINGUPLOAD"
TBL = "tblPENDINGUPLOAD"
FT = "PENDING UPLOAD"
ACT = "ALL"
IE = "ALL"
FN5 = "PENDU"
'BW RDD1005 Billing Report
ElseIf Left(myfile, 5) Like "BWRDD" = True Then
Spec = "RDD1005"
TBL = "tblBWRDD1005"
FT = "BW RDD1005"
ACT = "ALL"
IE = "ALL"
FN5 = "BWRDD"
'WF KTDL Kewill To Do List
ElseIf Left(myfile, 5) Like "WFKTD" = True Then
Spec = "WFKTDL"
TBL = "tblWFKTDL"
FT = "WF KTDL"
ACT = "ALL"
IE = "ALL"
FN5 = "WFKTD"
'Pre-alert
ElseIf Left(myfile, 5) Like "PAXXX" = True Then
Spec = "Prealert"
TBL = "tblPrealert"
FT = "PREALERT"
ACT = "ALL"
IE = "ALL"
FN5 = "PAXXX"
End If
If FN5 = Left(myfile, 5) Then
DoCmd.TransferText acImportDelim, Spec, TBL, Path & "\" & myfile
sql = "UPDATE " & TBL & " SET " & TBL & ".Import_date = """ & ID & """, " & TBL & ".Report = """ & RD & """, " & TBL & ".FileType = """ & FT & """, " & TBL & ".Activity = """ & ACT & """, " & TBL & ".ImportExport = """ & IE & """, " & TBL & ".FileName = """ & myfile & """ WHERE (((" & TBL & ".Import_date) Is Null));"
CurrentDb.QueryDefs("qrytemp").sql = sql
DoCmd.OpenQuery "qryTemp", acViewNormal, acEdit
FC = FC + 1
End If
i = i + 1
FN5 = ""
myfile = Dir
Loop
DoCmd.SetWarnings True
MsgBox FC & " Files Imported", vbOKOnly, "Import Status"
End Function
Code: