繁体   English   中英

如何使用SQL Server后端从MS Access前端导入新数据

[英]How to Import New Data from MS Access Front End with SQL Server back end

  • 你好,

我需要找到一种将新数据导入数据库的方法,该数据库包括一个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

但是,当我单击前端的导入按钮时,没有任何反应。 我的代码或解决方案出了什么问题?

这是一个工作示例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM