我需要找到一种方法将新数据导入到一个数据库中,该数据库包含一个SQL Server后端和一个MS Access 2010前端,后端的链接表从前端开始。
理想情况下,这将涉及用户单击前端表单上的按钮并选择要导入新数据的Excel电子表格,然后将其保存在后端SQL Server表中。
我在要导入的Excel电子表格中创建了一个vba模块。代码是:
Public Function ExcelPicker(Optional strFileName As String, _
Optional ByVal strWindowTitle As String = "Select an excel file") As String
Dim fd As Office.FileDialog
'Set fd = Application.FileDialog(msoFileDialogFolderPicker)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = strWindowTitle
.Filters.Add "All files", "*.*", 1
.Filters.Add "Excel Workbooks", "*.xls;*.xlsx;*.xlsm", 2
.AllowMultiSelect = False
If .Show = -1 Then
FilePicker = (.SelectedItems(1))
Else
FilePicker = vbNullString
End If
End With
Set fd = Nothing
End Function
然后我在MS Access 2010前端创建了一个导入按钮,在OnClick事件中我输入了代码:
Private Sub cmdImportFilterResults_Click()
Dim sExcelFile As String
Set sExcelFile = Application.FileDialog(msoFileDialogFilePicker)
'' if sExcelFile is not blank then
'' import the file to MSSQL linked table
If sExcelFile <> "" Then
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "[Database].Table_Result", sExcelFile, True
End If
End Sub
但是,当我点击前端的导入按钮时,什么也没发生。我的代码或解决方案有什么问题?
答案 0 :(得分:0)
这是一个有效的例子:
Private Sub cmdImportFilterResults_Click()
Dim strWindowTitle As String
Dim objDialog As Object
Dim sExcelFile As String
strWindowTitle = "Select an excel file"
Set objDialog = Application.FileDialog(msoFileDialogFilePicker)
With objDialog
.Title = strWindowTitle
.Filters.Add "Excel Workbooks", "*.xls;*.xlsx;*.xlsm", 1
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then
MsgBox "No File Selected."
Else
sExcelFile = Dir(.SelectedItems(1))
End If
End With
' if sExcelFile is not blank then
' import the file to MSSQL linked table
If sExcelFile <> "" Then
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "[Database].Table_Result", sExcelFile, True
End If
End Sub