从 Excel VBA 更新 Access 中的 table
Updating table in Access from Excel VBA
我在一个 Excel 文件中有两个 table,我需要将其导入到 Access 中,我找到了一种完美的方法,问题是它只对第一个有效时间(当 Access 中不存在此文件时导入 table)但是当我更改 Excel 文件并再次尝试此方法时,它不会更新记录并且基本上什么都不做。
Excel 和 Access 中的 table 分别称为“Messungen”和“Grundinformation”。这是名为 ExcelImport:
的模块的实现代码
Public Sub ImportExcelSpreadsheet(fileName As String, tableName As String)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Messungen", _
fileName, True, "Messung!"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Grundinformation", _
fileName, True, "Grundinformation!"
Exit Sub
End Sub
我还制作了以下对象。第一个浏览和 select 文件夹中的文件:
Private Sub btnBrowse_Click()
Dim diag As Office.FileDialog
Dim item As Variant
Set diag = Application.FileDialog(msoFileDialogFilePicker)
diag.AllowMultiSelect = False
diag.Title = "Bitte die Excel Datei 'DB_Access_Daten' wählen "
diag.Filters.Clear
diag.Filters.Add "Excel Spreadsheets", "*.xls, *.xlsx, *.xlsm"
If diag.Show Then
For Each item In diag.SelectedItems
Me.ExcelDatei = item
Next
End If
End Sub
而这个使用按钮导入 table:
Private Sub btnImportTabelle_Click()
Dim FSO As New FileSystemObject
If Nz(Me.ExcelDatei, "") = "" Then
MsgBox "Bitte eine Datei auswählen"
Exit Sub
End If
If FSO.FileExists(Nz(Me.ExcelDatei, "")) Then
ExcelImport.ImportExcelSpreadsheet Me.ExcelDatei, FSO.GetFileName(Me.ExcelDatei)
Else
MsgBox "File not found"
End If
End Sub
如前所述,不幸的是,这无法更新值,只是第一次导入 table。我曾考虑过将数据库链接到 Excel 中的 table,但遇到了一些麻烦,因为 excel 文件位于共享网络中,而我的磁盘驱动器盘符与来自我的同事们。我不知道代码是否有问题,我该如何解决。
提前感谢您的帮助!
您应该 link 电子表格。然后 运行 一个 update/append 查询,如下所示:
我在一个 Excel 文件中有两个 table,我需要将其导入到 Access 中,我找到了一种完美的方法,问题是它只对第一个有效时间(当 Access 中不存在此文件时导入 table)但是当我更改 Excel 文件并再次尝试此方法时,它不会更新记录并且基本上什么都不做。
Excel 和 Access 中的 table 分别称为“Messungen”和“Grundinformation”。这是名为 ExcelImport:
的模块的实现代码Public Sub ImportExcelSpreadsheet(fileName As String, tableName As String)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Messungen", _
fileName, True, "Messung!"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Grundinformation", _
fileName, True, "Grundinformation!"
Exit Sub
End Sub
我还制作了以下对象。第一个浏览和 select 文件夹中的文件:
Private Sub btnBrowse_Click()
Dim diag As Office.FileDialog
Dim item As Variant
Set diag = Application.FileDialog(msoFileDialogFilePicker)
diag.AllowMultiSelect = False
diag.Title = "Bitte die Excel Datei 'DB_Access_Daten' wählen "
diag.Filters.Clear
diag.Filters.Add "Excel Spreadsheets", "*.xls, *.xlsx, *.xlsm"
If diag.Show Then
For Each item In diag.SelectedItems
Me.ExcelDatei = item
Next
End If
End Sub
而这个使用按钮导入 table:
Private Sub btnImportTabelle_Click()
Dim FSO As New FileSystemObject
If Nz(Me.ExcelDatei, "") = "" Then
MsgBox "Bitte eine Datei auswählen"
Exit Sub
End If
If FSO.FileExists(Nz(Me.ExcelDatei, "")) Then
ExcelImport.ImportExcelSpreadsheet Me.ExcelDatei, FSO.GetFileName(Me.ExcelDatei)
Else
MsgBox "File not found"
End If
End Sub
如前所述,不幸的是,这无法更新值,只是第一次导入 table。我曾考虑过将数据库链接到 Excel 中的 table,但遇到了一些麻烦,因为 excel 文件位于共享网络中,而我的磁盘驱动器盘符与来自我的同事们。我不知道代码是否有问题,我该如何解决。
提前感谢您的帮助!
您应该 link 电子表格。然后 运行 一个 update/append 查询,如下所示: