使用表格检查导入 Excel - access 2016
Posted
技术标签:
【中文标题】使用表格检查导入 Excel - access 2016【英文标题】:Import Excels with table checking - access 2016 【发布时间】:2020-09-09 17:42:45 【问题描述】:我到处搜索,但如果不重写所有代码,我无法找到这样做,是否有此代码检查文件名是否与表名匹配,如果匹配则清除该表并重新导入或如果没有,那么创建一个新表?
Option Compare Database
Option Explicit
Private Sub btnBrowse_Click()
Dim diag As Office.FileDialog
Dim item As Variant
Set diag = Application.FileDialog(msoFileDialogFilePicker)
diag.AllowMultiSelect = False
diag.Title = "Please select an Excel Spreadsheet"
diag.Filters.Clear
diag.Filters.Add "Excel Spreadsheets", "*.xls, *.xlsx, *.xlsm"
If diag.Show Then
For Each item In diag.SelectedItems
Me.txtFileName = item
Next
End If
End Sub
Private Sub btnImportSpreadsheet_Click()
Dim FSO As New FileSystemObject
If FSO.FileExists(Nz(Me.txtFileName, "")) Then
ImportExcelSpreadsheet Me.txtFileName, FSO.GetFileName(Me.txtFileName)
ElseIf Nz(Me.txtFileName, "") = "" Then
MsgBox "Please select a file!", vbExclamation
Else
MsgBox "File not found!", vbExclamation
End If
End Sub
Public Sub ImportExcelSpreadsheet(Filename As String, TableName As String)
On Error Resume Next
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, TableName, Filename, True
If Err.Number = 3125 Then
If vbOK = MsgBox(Err.Description & vbNewLine & vbNewLine & "Skip column header and continue?", vbExclamation + vbOKCancel, "Error with Excel Column header") Then
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, TableName, Filename, False
MsgBox "Done", vbInformation
End If
Exit Sub
ElseIf Err.Number <> 0 Then
MsgBox Err.Number & ":" & Err.Description, vbCritical
Exit Sub
End If
MsgBox "Upload Complete", vbInformation
End Sub
感谢您的帮助
【问题讨论】:
【参考方案1】:你必须重写一些。如果不循环遍历 Tables 集合并针对每个名称进行测试,每个方法似乎都涉及处理错误。这是一个:
Function TableExists(strTableName As String) As Boolean
On Error Resume Next
TableExists = IsObject(CurrentDb.TableDefs(strTableName))
End Function
调用函数:If TableExists("YourTableName") = True Then
How to check if a table exists in MS Access for vb macros中的更多示例
【讨论】:
以上是关于使用表格检查导入 Excel - access 2016的主要内容,如果未能解决你的问题,请参考以下文章
导入 Excel 电子表格时 MS Access 2013 类型转换失败