I have written code that imports excel files into a access table. As each file is imported the file name is recorded and saved to a separate table named 'FilesDownloaded'.
I would like to add vba code that prior to importing the file it will check to see if the name of the file (myfile) is already saved on the 'FilesDownloaded' table. This will prevent the same file from being imported twice.
Code:
Function Impo_allExcel()
Dim myfile
Dim mypath
Dim que As Byte
Dim rs As DAO.Recordset
que = MsgBox("This proces will import all excel items with the .xls in the folder C:\MasterCard. Please make sure that only the files you want imported are located in this folder. Do you wish to proceed?", vbYesNo + vbQuestion)
If que = 7 Then
Exit Function
Else
'do nothing and proceed with code
End If
DoCmd.SetWarnings (False)
DoCmd.RunSQL "DELETE * FROM tblMaster_Import;"
MsgBox "Please WAIT while we process this request"
mypath = "C:\Master\"
ChDir (mypath)
myfile = Dir(mypath & "*.xls")
Do While myfile <> ""
If myfile Like "*.xls" Then
'this will import ALL the excel files
'(one at a time, but automatically) in this folder.
' Make sure that's what you want.
'DoCmd.TransferSpreadsheet acImport, 8, "tblMasterCard_Import", mypath & myfile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "tblMaster_Import", mypath & myfile, 1
Set rs = CurrentDb.OpenRecordset("FilesDownloaded")
rs.AddNew
rs.Fields("Filename").Value = myfile
rs.Update
rs.Close
Set rs = Nothing
End If
myfile = Dir()
Loop
'append data to tblAll (risk of duplicates at this point)
DoCmd.RunSQL "INSERT INTO tblAll SELECT tblMaster_Import.* FROM tblMaster_Import;"
DoCmd.OpenQuery "qryUpdateDateField", acViewNormal
''this code will apend to an existing table and runs the risk of doubling data.
DoCmd.SetWarnings (True)
MsgBox "Your upload is complete"
End Function