我编写了将excel文件导入访问表的代码。导入每个文件时,将记录文件名并将其保存到名为“FilesDownloaded”的单独表中。
我想添加vba代码,在导入文件之前,它会检查文件名(myfile)是否已保存在'FilesDownloaded'表中。这样可以防止同一文件被导入两次。
代码:
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
答案 0 :(得分:0)
一种可能的解决方案是进行查询,该查询为您提供表FilesDownloaded
中行的结果,条件是名称等于您的文件。像这样:
countString = "SELECT COUNT(*) FROM [FilesDownloaded] WHERE [NAMEOFCOL] = " & myFile
然后运行查询,如果结果大于1,你显然已经拥有它。